|
1 | 1 | import uuid |
2 | 2 | from typing import Any, Callable, Type |
3 | 3 |
|
| 4 | +import pkg_resources |
4 | 5 | from google.protobuf.json_format import ( # type: ignore |
5 | 6 | _WKTJSONMETHODS, |
6 | 7 | ParseError, |
7 | 8 | _Parser, |
8 | 9 | _Printer, |
9 | 10 | ) |
| 11 | +from packaging import version |
10 | 12 |
|
11 | 13 | from feast.protos.feast.serving.ServingService_pb2 import FeatureList |
12 | 14 | from feast.protos.feast.types.Value_pb2 import RepeatedValue, Value |
|
15 | 17 | JsonObject = Any |
16 | 18 |
|
17 | 19 |
|
18 | | -# TODO: These methods need to be updated when bumping the version of protobuf. |
19 | | -# https://github.com/feast-dev/feast/issues/2484 |
20 | 20 | def _patch_proto_json_encoding( |
21 | 21 | proto_type: Type[ProtoMessage], |
22 | 22 | to_json_object: Callable[[_Printer, ProtoMessage], JsonObject], |
@@ -70,7 +70,7 @@ def to_json_object(printer: _Printer, message: ProtoMessage) -> JsonObject: |
70 | 70 | return value |
71 | 71 |
|
72 | 72 | def from_json_object( |
73 | | - parser: _Parser, value: JsonObject, message: ProtoMessage, path: str |
| 73 | + parser: _Parser, value: JsonObject, message: ProtoMessage |
74 | 74 | ) -> None: |
75 | 75 | if value is None: |
76 | 76 | message.null_val = 0 |
@@ -111,7 +111,18 @@ def from_json_object( |
111 | 111 | "Value {0} has unexpected type {1}.".format(value, type(value)) |
112 | 112 | ) |
113 | 113 |
|
114 | | - _patch_proto_json_encoding(Value, to_json_object, from_json_object) |
| 114 | + def from_json_object_updated( |
| 115 | + parser: _Parser, value: JsonObject, message: ProtoMessage, path: str |
| 116 | + ): |
| 117 | + from_json_object(parser, value, message) |
| 118 | + |
| 119 | + # https://github.com/feast-dev/feast/issues/2484 Certain feast users need a higher version of protobuf but the |
| 120 | + # parameters of `from_json_object` changes in feast 3.20.1. This change gives users flexibility to use earlier versions. |
| 121 | + current_version = pkg_resources.get_distribution("protobuf").version |
| 122 | + if version.parse(current_version) < version.parse("3.20"): |
| 123 | + _patch_proto_json_encoding(Value, to_json_object, from_json_object) |
| 124 | + else: |
| 125 | + _patch_proto_json_encoding(Value, to_json_object, from_json_object_updated) |
115 | 126 |
|
116 | 127 |
|
117 | 128 | def _patch_feast_repeated_value_json_encoding(): |
@@ -141,14 +152,29 @@ def _patch_feast_repeated_value_json_encoding(): |
141 | 152 | def to_json_object(printer: _Printer, message: ProtoMessage) -> JsonObject: |
142 | 153 | return [printer._MessageToJsonObject(item) for item in message.val] |
143 | 154 |
|
144 | | - def from_json_object( |
| 155 | + def from_json_object_updated( |
145 | 156 | parser: _Parser, value: JsonObject, message: ProtoMessage, path: str |
146 | 157 | ) -> None: |
147 | 158 | array = value if isinstance(value, list) else value["val"] |
148 | 159 | for item in array: |
149 | 160 | parser.ConvertMessage(item, message.val.add(), path) |
150 | 161 |
|
151 | | - _patch_proto_json_encoding(RepeatedValue, to_json_object, from_json_object) |
| 162 | + def from_json_object( |
| 163 | + parser: _Parser, value: JsonObject, message: ProtoMessage |
| 164 | + ) -> None: |
| 165 | + array = value if isinstance(value, list) else value["val"] |
| 166 | + for item in array: |
| 167 | + parser.ConvertMessage(item, message.val.add()) |
| 168 | + |
| 169 | + # https://github.com/feast-dev/feast/issues/2484 Certain feast users need a higher version of protobuf but the |
| 170 | + # parameters of `from_json_object` changes in feast 3.20.1. This change gives users flexibility to use earlier versions. |
| 171 | + current_version = pkg_resources.get_distribution("protobuf").version |
| 172 | + if version.parse(current_version) < version.parse("3.20"): |
| 173 | + _patch_proto_json_encoding(RepeatedValue, to_json_object, from_json_object) |
| 174 | + else: |
| 175 | + _patch_proto_json_encoding( |
| 176 | + RepeatedValue, to_json_object, from_json_object_updated |
| 177 | + ) |
152 | 178 |
|
153 | 179 |
|
154 | 180 | def _patch_feast_feature_list_json_encoding(): |
@@ -183,12 +209,25 @@ def to_json_object(printer: _Printer, message: ProtoMessage) -> JsonObject: |
183 | 209 | return list(message.val) |
184 | 210 |
|
185 | 211 | def from_json_object( |
186 | | - parser: _Parser, value: JsonObject, message: ProtoMessage, path: str |
| 212 | + parser: _Parser, value: JsonObject, message: ProtoMessage |
187 | 213 | ) -> None: |
188 | 214 | array = value if isinstance(value, list) else value["val"] |
189 | 215 | message.val.extend(array) |
190 | 216 |
|
191 | | - _patch_proto_json_encoding(FeatureList, to_json_object, from_json_object) |
| 217 | + def from_json_object_updated( |
| 218 | + parser: _Parser, value: JsonObject, message: ProtoMessage, path: str |
| 219 | + ) -> None: |
| 220 | + from_json_object(parser, value, message) |
| 221 | + |
| 222 | + # https://github.com/feast-dev/feast/issues/2484 Certain feast users need a higher version of protobuf but the |
| 223 | + # parameters of `from_json_object` changes in feast 3.20.1. This change gives users flexibility to use earlier versions. |
| 224 | + current_version = pkg_resources.get_distribution("protobuf").version |
| 225 | + if version.parse(current_version) < version.parse("3.20"): |
| 226 | + _patch_proto_json_encoding(FeatureList, to_json_object, from_json_object) |
| 227 | + else: |
| 228 | + _patch_proto_json_encoding( |
| 229 | + FeatureList, to_json_object, from_json_object_updated |
| 230 | + ) |
192 | 231 |
|
193 | 232 |
|
194 | 233 | def patch(): |
|
0 commit comments