Patch Protobuf JSON Encoder / Decoder with a Feast Value type. This allows encoding the proto object as a native type, without the dummy structural wrapper. Here's a before example: { "value_1": { "int64_val": 1 }, "value_2": { "double_l
()
| 34 | |
| 35 | |
| 36 | def _patch_feast_value_json_encoding(): |
| 37 | """Patch Protobuf JSON Encoder / Decoder with a Feast Value type. |
| 38 | |
| 39 | This allows encoding the proto object as a native type, without the dummy structural wrapper. |
| 40 | |
| 41 | Here's a before example: |
| 42 | |
| 43 | { |
| 44 | "value_1": { |
| 45 | "int64_val": 1 |
| 46 | }, |
| 47 | "value_2": { |
| 48 | "double_list_val": [1.0, 2.0, 3.0] |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | And here's an after example: |
| 53 | |
| 54 | { |
| 55 | "value_1": 1, |
| 56 | "value_2": [1.0, 2.0, 3.0] |
| 57 | } |
| 58 | """ |
| 59 | |
| 60 | def to_json_object(printer: _Printer, message: ProtoMessage) -> JsonObject: |
| 61 | which = message.WhichOneof("val") |
| 62 | # If the Value message is not set treat as null_value when serialize |
| 63 | # to JSON. The parse back result will be different from original message. |
| 64 | if which is None or which == "null_val": |
| 65 | return None |
| 66 | elif which in ("list_val", "set_val"): |
| 67 | # Nested collection: RepeatedValue containing Values |
| 68 | repeated = getattr(message, which) |
| 69 | value = [ |
| 70 | printer._MessageToJsonObject(inner_val) for inner_val in repeated.val |
| 71 | ] |
| 72 | elif "_list_" in which: |
| 73 | value = list(getattr(message, which).val) |
| 74 | else: |
| 75 | value = getattr(message, which) |
| 76 | return value |
| 77 | |
| 78 | def from_json_object( |
| 79 | parser: _Parser, value: JsonObject, message: ProtoMessage |
| 80 | ) -> None: |
| 81 | if value is None: |
| 82 | message.null_val = 0 |
| 83 | elif isinstance(value, bool): |
| 84 | message.bool_val = value |
| 85 | elif isinstance(value, str): |
| 86 | message.string_val = value |
| 87 | elif isinstance(value, int): |
| 88 | message.int64_val = value |
| 89 | elif isinstance(value, float): |
| 90 | message.double_val = value |
| 91 | elif isinstance(value, list): |
| 92 | if len(value) == 0: |
| 93 | # Clear will mark the struct as modified so it will be created even if there are no values |
no test coverage detected