MCPcopy
hub / github.com/danielgtaylor/python-betterproto / _postprocess_single

Method _postprocess_single

src/betterproto/__init__.py:1201–1243  ·  view source on GitHub ↗

Adjusts values after parsing.

(
        self, wire_type: int, meta: FieldMetadata, field_name: str, value: Any
    )

Source from the content-addressed store, hash-verified

1199 return t
1200
1201 def _postprocess_single(
1202 self, wire_type: int, meta: FieldMetadata, field_name: str, value: Any
1203 ) -> Any:
1204 """Adjusts values after parsing."""
1205 if wire_type == WIRE_VARINT:
1206 if meta.proto_type in (TYPE_INT32, TYPE_INT64):
1207 bits = int(meta.proto_type[3:])
1208 value = value & ((1 << bits) - 1)
1209 signbit = 1 << (bits - 1)
1210 value = int((value ^ signbit) - signbit)
1211 elif meta.proto_type in (TYPE_SINT32, TYPE_SINT64):
1212 # Undo zig-zag encoding
1213 value = (value >> 1) ^ (-(value & 1))
1214 elif meta.proto_type == TYPE_BOOL:
1215 # Booleans use a varint encoding, so convert it to true/false.
1216 value = value > 0
1217 elif meta.proto_type == TYPE_ENUM:
1218 # Convert enum ints to python enum instances
1219 value = self._betterproto.cls_by_field[field_name].try_value(value)
1220 elif wire_type in (WIRE_FIXED_32, WIRE_FIXED_64):
1221 fmt = _pack_fmt(meta.proto_type)
1222 value = struct.unpack(fmt, value)[0]
1223 elif wire_type == WIRE_LEN_DELIM:
1224 if meta.proto_type == TYPE_STRING:
1225 value = str(value, "utf-8")
1226 elif meta.proto_type == TYPE_MESSAGE:
1227 cls = self._betterproto.cls_by_field[field_name]
1228
1229 if cls == datetime:
1230 value = _Timestamp().parse(value).to_datetime()
1231 elif cls == timedelta:
1232 value = _Duration().parse(value).to_timedelta()
1233 elif meta.wraps:
1234 # This is a Google wrapper value message around a single
1235 # scalar type.
1236 value = _get_wrapper(meta.wraps)().parse(value).value
1237 else:
1238 value = cls().parse(value)
1239 value._serialized_on_wire = True
1240 elif meta.proto_type == TYPE_MAP:
1241 value = self._betterproto.cls_by_field[field_name]().parse(value)
1242
1243 return value
1244
1245 def _include_default_value_for_oneof(
1246 self, field_name: str, meta: FieldMetadata

Callers 1

loadMethod · 0.95

Calls 8

_pack_fmtFunction · 0.85
_TimestampClass · 0.85
_DurationClass · 0.85
_get_wrapperFunction · 0.85
try_valueMethod · 0.80
to_datetimeMethod · 0.80
parseMethod · 0.80
to_timedeltaMethod · 0.80

Tested by

no test coverage detected