14
14
IllegalArgumentException ,
15
15
IllegalStateException ,
16
16
ProtobufException ,
17
- ProtobufParserRuntimeException ,
18
17
ProtobufUnresolvedDependencyException ,
19
18
SchemaParseException as ProtobufSchemaParseException ,
20
19
)
@@ -114,7 +113,6 @@ def parse(
114
113
TypeError ,
115
114
SchemaError ,
116
115
AssertionError ,
117
- ProtobufParserRuntimeException ,
118
116
IllegalStateException ,
119
117
IllegalArgumentException ,
120
118
ProtobufError ,
@@ -155,11 +153,10 @@ def __init__(
155
153
self .schema_type = schema_type
156
154
self .references = references
157
155
self .dependencies = dependencies
158
- self .schema_str = TypedSchema .normalize_schema_str (schema_str , schema_type , references , dependencies )
156
+ self .schema_str = TypedSchema .normalize_schema_str (schema_str , schema_type , schema )
159
157
self .max_id : Optional [SchemaId ] = None
160
158
self ._fingerprint_cached : Optional [str ] = None
161
159
self ._str_cached : Optional [str ] = None
162
- self ._schema_cached : Optional [Union [Draft7Validator , AvroSchema , ProtobufSchema ]] = schema
163
160
164
161
def to_dict (self ) -> Dict [str , Any ]:
165
162
if self .schema_type is SchemaType .PROTOBUF :
@@ -175,8 +172,9 @@ def fingerprint(self) -> str:
175
172
def normalize_schema_str (
176
173
schema_str : str ,
177
174
schema_type : SchemaType ,
178
- references : Optional [List [Reference ]] = None ,
179
- dependencies : Optional [Dict [str , Dependency ]] = None ,
175
+ schema : Optional [Union [Draft7Validator , AvroSchema , ProtobufSchema ]] = None ,
176
+ # references: Optional[List[Reference]] = None,
177
+ # dependencies: Optional[Dict[str, Dependency]] = None,
180
178
) -> str :
181
179
if schema_type is SchemaType .AVRO or schema_type is SchemaType .JSONSCHEMA :
182
180
try :
@@ -185,11 +183,15 @@ def normalize_schema_str(
185
183
LOG .error ("Schema is not valid JSON" )
186
184
raise e
187
185
elif schema_type == SchemaType .PROTOBUF :
188
- try :
189
- schema_str = str (parse_protobuf_schema_definition (schema_str , references , dependencies , False ))
190
- except InvalidSchema as e :
191
- LOG .exception ("Schema is not valid ProtoBuf definition" )
192
- raise e
186
+ if schema :
187
+ schema_str = str (schema )
188
+ else :
189
+ try :
190
+ schema_str = str (parse_protobuf_schema_definition (schema_str , None , None , False ))
191
+ except InvalidSchema as e :
192
+ LOG .exception ("Schema is not valid ProtoBuf definition" )
193
+ raise e
194
+
193
195
else :
194
196
_assert_never (schema_type )
195
197
return schema_str
@@ -205,10 +207,16 @@ def __str__(self) -> str:
205
207
def __repr__ (self ) -> str :
206
208
return f"TypedSchema(type={ self .schema_type } , schema={ str (self )} )"
207
209
210
+ def __eq__ (self , other : Any ) -> bool :
211
+ return (
212
+ isinstance (other , (TypedSchema ))
213
+ and self .schema_type is other .schema_type
214
+ and str (self ) == str (other )
215
+ and self .references == other .references
216
+ )
217
+
208
218
@property
209
219
def schema (self ) -> Union [Draft7Validator , AvroSchema , ProtobufSchema ]:
210
- if self ._schema_cached is not None :
211
- return self ._schema_cached
212
220
parsed_typed_schema = parse (
213
221
schema_type = self .schema_type ,
214
222
schema_str = self .schema_str ,
@@ -217,19 +225,7 @@ def schema(self) -> Union[Draft7Validator, AvroSchema, ProtobufSchema]:
217
225
references = self .references ,
218
226
dependencies = self .dependencies ,
219
227
)
220
- self ._schema_cached = parsed_typed_schema .schema
221
- return self ._schema_cached
222
-
223
- def get_references (self ) -> Optional [List [Reference ]]:
224
- return self .references
225
-
226
- def __eq__ (self , other : Any ) -> bool :
227
- return (
228
- isinstance (other , (TypedSchema ))
229
- and self .schema_type is other .schema_type
230
- and str (self ) == str (other )
231
- and self .references == other .references
232
- )
228
+ return parsed_typed_schema .schema
233
229
234
230
235
231
class ParsedTypedSchema (TypedSchema ):
@@ -259,6 +255,8 @@ def __init__(
259
255
references : Optional [List [Reference ]] = None ,
260
256
dependencies : Optional [Dict [str , Dependency ]] = None ,
261
257
):
258
+ self ._schema_cached : Optional [Union [Draft7Validator , AvroSchema , ProtobufSchema ]] = schema
259
+
262
260
super ().__init__ (
263
261
schema_type = schema_type , schema_str = schema_str , references = references , dependencies = dependencies , schema = schema
264
262
)
@@ -284,6 +282,16 @@ def __str__(self) -> str:
284
282
return str (self .schema )
285
283
return super ().__str__ ()
286
284
285
+ @property
286
+ def schema (self ) -> Union [Draft7Validator , AvroSchema , ProtobufSchema ]:
287
+ if self ._schema_cached is not None :
288
+ return self ._schema_cached
289
+ self ._schema_cached = super ().schema
290
+ return self ._schema_cached
291
+
292
+ def get_references (self ) -> Optional [List [Reference ]]:
293
+ return self .references
294
+
287
295
288
296
class ValidatedTypedSchema (ParsedTypedSchema ):
289
297
"""Validated schema resource.
0 commit comments