38
38
39
39
40
40
# Helper functions
41
- def camel_case (s : str ) -> str :
41
+ def to_pascal_case (s : str ) -> str :
42
42
"""
43
43
Convert "bclconvert_settings" to "BclconvertSettings"
44
44
:param s:
45
45
:return:
46
46
"""
47
- s = re .sub (r"[_| -]+" , " " , s ).title ().replace (" " , "" )
47
+ s = re .sub (r"[_-]+" , " " , s ).title ().replace (" " , "" )
48
48
return '' .join (s )
49
49
50
50
@@ -141,9 +141,13 @@ def sanitise_schema_types(schema_json: Dict, schema_path: Path, schema_name: Uni
141
141
142
142
# Other local vars
143
143
if schema_name is None :
144
- schema_name : str = camel_case (schema_path .stem .split ("__" , 1 )[0 ])
144
+ schema_name : str = to_pascal_case (schema_path .stem .split ("__" , 1 )[0 ])
145
145
sanitised_schema : Dict = {}
146
146
147
+ if schema_json .get ("fields" ) is None :
148
+ print ("Warning - No fields found in schema" )
149
+ return sanitised_schemas , imported_interfaces , new_enum_classes
150
+
147
151
field : Dict
148
152
for field in schema_json .get ("fields" ):
149
153
# Get basics
@@ -207,11 +211,16 @@ def sanitise_schema_types(schema_json: Dict, schema_path: Path, schema_name: Uni
207
211
# Collect symbols
208
212
symbols = [
209
213
Path (symbol ).name
214
+ if isinstance (symbol , str )
215
+ else symbol
210
216
for symbol in field_type .get ("symbols" )
211
217
]
212
218
213
219
# Collect new field type name
214
- new_field_type = camel_case (Path (field_type .get ("symbols" )[0 ]).parent .name )
220
+ if field_type .get ("name" ) is not None :
221
+ new_field_type = to_pascal_case (Path (field_type .get ("name" )).name )
222
+ else :
223
+ new_field_type = to_pascal_case (Path (field_type .get ("symbols" )[0 ]).parent .name )
215
224
216
225
field_type = new_field_type
217
226
@@ -232,14 +241,14 @@ def sanitise_schema_types(schema_json: Dict, schema_path: Path, schema_name: Uni
232
241
re_new_enum_classes : List
233
242
re_sanitised_schemas , re_imported_interfaces , re_new_enum_classes = \
234
243
sanitise_schema_types (field_type , schema_path ,
235
- schema_name = camel_case (Path (field_type .get ("name" )).name ))
244
+ schema_name = to_pascal_case (Path (field_type .get ("name" )).name ))
236
245
sanitised_schemas .update (re_sanitised_schemas )
237
246
imported_interfaces .update (re_imported_interfaces )
238
247
new_enum_classes .extend (re_new_enum_classes )
239
- field_type = camel_case (Path (field_type .get ("name" )).name )
248
+ field_type = to_pascal_case (Path (field_type .get ("name" )).name )
240
249
else :
241
250
# Add to list of interfaces to import
242
- new_interface_name = camel_case (Path (field_type .get ("name" )).name )
251
+ new_interface_name = to_pascal_case (Path (field_type .get ("name" )).name )
243
252
imported_interfaces [new_interface_name ] = field_type .get ("name" )
244
253
field_type = new_interface_name
245
254
@@ -309,7 +318,7 @@ def write_interface_file(schema_dicts: Dict, imported_interfaces: Dict,
309
318
for _enum in enum_classes :
310
319
interface_h .write ("export enum %s {\n " % _enum .get ("name" ))
311
320
for index , symbol in enumerate (_enum .get ("symbols" )):
312
- interface_h .write ("\t \" %s\" = \" %s \" " % (symbol , symbol ))
321
+ interface_h .write ("\t \" %s\" = %s" % (symbol , json . dumps ( symbol ) ))
313
322
if not index == len (_enum .get ("symbols" )) - 1 :
314
323
# Add comma
315
324
interface_h .write ("," )
0 commit comments