|
| 1 | +import uuid |
| 2 | +from typing import Any, Callable, Type |
| 3 | + |
| 4 | +from google.protobuf.json_format import ( # type: ignore |
| 5 | + _WKTJSONMETHODS, |
| 6 | + ParseError, |
| 7 | + _Parser, |
| 8 | + _Printer, |
| 9 | +) |
| 10 | + |
| 11 | +from feast.protos.feast.serving.ServingService_pb2 import FeatureList |
| 12 | +from feast.protos.feast.types.Value_pb2 import RepeatedValue, Value |
| 13 | + |
| 14 | +ProtoMessage = Any |
| 15 | +JsonObject = Any |
| 16 | + |
| 17 | + |
| 18 | +def _patch_proto_json_encoding( |
| 19 | + proto_type: Type[ProtoMessage], |
| 20 | + to_json_object: Callable[[_Printer, ProtoMessage], JsonObject], |
| 21 | + from_json_object: Callable[[_Parser, JsonObject, ProtoMessage], None], |
| 22 | +) -> None: |
| 23 | + """Patch Protobuf JSON Encoder / Decoder for a desired Protobuf type with to_json & from_json methods.""" |
| 24 | + to_json_fn_name = "_" + uuid.uuid4().hex |
| 25 | + from_json_fn_name = "_" + uuid.uuid4().hex |
| 26 | + setattr(_Printer, to_json_fn_name, to_json_object) |
| 27 | + setattr(_Parser, from_json_fn_name, from_json_object) |
| 28 | + _WKTJSONMETHODS[proto_type.DESCRIPTOR.full_name] = [ |
| 29 | + to_json_fn_name, |
| 30 | + from_json_fn_name, |
| 31 | + ] |
| 32 | + |
| 33 | + |
| 34 | +def _patch_feast_value_json_encoding(): |
| 35 | + """Patch Protobuf JSON Encoder / Decoder with a Feast Value type. |
| 36 | +
|
| 37 | + This allows encoding the proto object as a native type, without the dummy structural wrapper. |
| 38 | +
|
| 39 | + Here's a before example: |
| 40 | +
|
| 41 | + { |
| 42 | + "value_1": { |
| 43 | + "int64_val": 1 |
| 44 | + }, |
| 45 | + "value_2": { |
| 46 | + "double_list_val": [1.0, 2.0, 3.0] |
| 47 | + }, |
| 48 | + } |
| 49 | +
|
| 50 | + And here's an after example: |
| 51 | +
|
| 52 | + { |
| 53 | + "value_1": 1, |
| 54 | + "value_2": [1.0, 2.0, 3.0] |
| 55 | + } |
| 56 | + """ |
| 57 | + |
| 58 | + def to_json_object(printer: _Printer, message: ProtoMessage) -> JsonObject: |
| 59 | + which = message.WhichOneof("val") |
| 60 | + # If the Value message is not set treat as null_value when serialize |
| 61 | + # to JSON. The parse back result will be different from original message. |
| 62 | + if which is None or which == "null_val": |
| 63 | + return None |
| 64 | + elif "_list_" in which: |
| 65 | + value = list(getattr(message, which).val) |
| 66 | + else: |
| 67 | + value = getattr(message, which) |
| 68 | + return value |
| 69 | + |
| 70 | + def from_json_object( |
| 71 | + parser: _Parser, value: JsonObject, message: ProtoMessage |
| 72 | + ) -> None: |
| 73 | + if value is None: |
| 74 | + message.null_val = 0 |
| 75 | + elif isinstance(value, bool): |
| 76 | + message.bool_val = value |
| 77 | + elif isinstance(value, str): |
| 78 | + message.string_val = value |
| 79 | + elif isinstance(value, int): |
| 80 | + message.int64_val = value |
| 81 | + elif isinstance(value, float): |
| 82 | + message.double_val = value |
| 83 | + elif isinstance(value, list): |
| 84 | + if len(value) == 0: |
| 85 | + # Clear will mark the struct as modified so it will be created even if there are no values |
| 86 | + message.int64_list_val.Clear() |
| 87 | + elif isinstance(value[0], bool): |
| 88 | + message.bool_list_val.val.extend(value) |
| 89 | + elif isinstance(value[0], str): |
| 90 | + message.string_list_val.val.extend(value) |
| 91 | + elif isinstance(value[0], (float, int, type(None))): |
| 92 | + # Identify array as ints if all of the elements are ints |
| 93 | + if all(isinstance(item, int) for item in value): |
| 94 | + message.int64_list_val.val.extend(value) |
| 95 | + # If any of the elements are floats or nulls, then parse it as a float array |
| 96 | + else: |
| 97 | + # Convert each null as NaN. |
| 98 | + message.double_list_val.val.extend( |
| 99 | + [item if item is not None else float("nan") for item in value] |
| 100 | + ) |
| 101 | + else: |
| 102 | + raise ParseError( |
| 103 | + "Value {0} has unexpected type {1}.".format( |
| 104 | + value[0], type(value[0]) |
| 105 | + ) |
| 106 | + ) |
| 107 | + else: |
| 108 | + raise ParseError( |
| 109 | + "Value {0} has unexpected type {1}.".format(value, type(value)) |
| 110 | + ) |
| 111 | + |
| 112 | + _patch_proto_json_encoding(Value, to_json_object, from_json_object) |
| 113 | + |
| 114 | + |
| 115 | +def _patch_feast_repeated_value_json_encoding(): |
| 116 | + """Patch Protobuf JSON Encoder / Decoder with a Feast RepeatedValue type. |
| 117 | +
|
| 118 | + This allows list of lists without dummy field name "val". |
| 119 | +
|
| 120 | + Here's a before example: |
| 121 | +
|
| 122 | + { |
| 123 | + "repeated_value": [ |
| 124 | + {"val": [1,2,3]}, |
| 125 | + {"val": [4,5,6]} |
| 126 | + ] |
| 127 | + } |
| 128 | +
|
| 129 | + And here's an after example: |
| 130 | +
|
| 131 | + { |
| 132 | + "repeated_value": [ |
| 133 | + [1,2,3], |
| 134 | + [4,5,6] |
| 135 | + ] |
| 136 | + } |
| 137 | + """ |
| 138 | + |
| 139 | + def to_json_object(printer: _Printer, message: ProtoMessage) -> JsonObject: |
| 140 | + return [printer._MessageToJsonObject(item) for item in message.val] |
| 141 | + |
| 142 | + def from_json_object( |
| 143 | + parser: _Parser, value: JsonObject, message: ProtoMessage |
| 144 | + ) -> None: |
| 145 | + array = value if isinstance(value, list) else value["val"] |
| 146 | + for item in array: |
| 147 | + parser.ConvertMessage(item, message.val.add()) |
| 148 | + |
| 149 | + _patch_proto_json_encoding(RepeatedValue, to_json_object, from_json_object) |
| 150 | + |
| 151 | + |
| 152 | +def _patch_feast_feature_list_json_encoding(): |
| 153 | + """Patch Protobuf JSON Encoder / Decoder with a Feast FeatureList type. |
| 154 | +
|
| 155 | + This allows list of lists without dummy field name "features". |
| 156 | +
|
| 157 | + Here's a before example: |
| 158 | +
|
| 159 | + { |
| 160 | + "feature_list": { |
| 161 | + "features": [ |
| 162 | + "feature-1", |
| 163 | + "feature-2", |
| 164 | + "feature-3" |
| 165 | + ] |
| 166 | + } |
| 167 | + } |
| 168 | +
|
| 169 | + And here's an after example: |
| 170 | +
|
| 171 | + { |
| 172 | + "feature_list": [ |
| 173 | + "feature-1", |
| 174 | + "feature-2", |
| 175 | + "feature-3" |
| 176 | + ] |
| 177 | + } |
| 178 | + """ |
| 179 | + |
| 180 | + def to_json_object(printer: _Printer, message: ProtoMessage) -> JsonObject: |
| 181 | + return list(message.val) |
| 182 | + |
| 183 | + def from_json_object( |
| 184 | + parser: _Parser, value: JsonObject, message: ProtoMessage |
| 185 | + ) -> None: |
| 186 | + array = value if isinstance(value, list) else value["val"] |
| 187 | + message.val.extend(array) |
| 188 | + |
| 189 | + _patch_proto_json_encoding(FeatureList, to_json_object, from_json_object) |
| 190 | + |
| 191 | + |
| 192 | +def patch(): |
| 193 | + """Patch Protobuf JSON Encoder / Decoder with all desired Feast types.""" |
| 194 | + _patch_feast_value_json_encoding() |
| 195 | + _patch_feast_repeated_value_json_encoding() |
| 196 | + _patch_feast_feature_list_json_encoding() |
0 commit comments