2
2
3
3
import datetime
4
4
import functools
5
+ import json
5
6
import textwrap
6
7
from typing import IO , Any , Dict , List , Optional , Set , Tuple , Union , cast
7
8
@@ -65,7 +66,7 @@ def prologue(self) -> None:
65
66
import salad.meta.dumper : genDumper;
66
67
import salad.meta.impl : genCtor_, genIdentifier, genOpEq;
67
68
import salad.meta.parser : import_ = importFromURI;
68
- import salad.meta.uda : documentRoot, id, idMap, link, LinkResolver, secondaryFilesDSL, typeDSL;
69
+ import salad.meta.uda : defaultValue, documentRoot, id, idMap, link, LinkResolver, secondaryFilesDSL, typeDSL;
69
70
import salad.primitives : SchemaBase;
70
71
import salad.type : None, Union;
71
72
@@ -167,6 +168,7 @@ def parse_record_field_type(
167
168
type_ : Any ,
168
169
jsonld_pred : Union [None , str , Dict [str , Any ]],
169
170
parent_has_idmap : bool = False ,
171
+ has_default : bool = False ,
170
172
) -> Tuple [str , str ]:
171
173
"""Return an annotation string and a type string."""
172
174
annotations : List [str ] = []
@@ -206,13 +208,18 @@ def parse_record_field_type(
206
208
else :
207
209
type_str = stype
208
210
elif isinstance (type_ , list ):
209
- t_str = [self .parse_record_field_type (t , None , has_idmap )[1 ] for t in type_ ]
210
- if are_dispatchable (type_ , has_idmap ):
211
- t_str += ["Any" ]
212
- union_types = ", " .join (t_str )
213
- type_str = f"Union!({ union_types } )"
211
+ t_str = [self .parse_record_field_type (t , None , parent_has_idmap = has_idmap )[1 ] for t in type_ ]
212
+ if has_default :
213
+ t_str = [t for t in t_str if t != "None" ]
214
+ if len (t_str ) == 1 :
215
+ type_str = t_str [0 ]
216
+ else :
217
+ if are_dispatchable (type_ , has_idmap ):
218
+ t_str += ["Any" ]
219
+ union_types = ", " .join (t_str )
220
+ type_str = f"Union!({ union_types } )"
214
221
elif shortname (type_ ["type" ]) == "array" :
215
- item_type = self .parse_record_field_type (type_ ["items" ], None , has_idmap )[1 ]
222
+ item_type = self .parse_record_field_type (type_ ["items" ], None , parent_has_idmap = has_idmap )[1 ]
216
223
type_str = f"{ item_type } []"
217
224
elif shortname (type_ ["type" ]) == "record" :
218
225
return annotate_str , shortname (type_ .get ("name" , "record" ))
@@ -234,9 +241,15 @@ def parse_record_field(self, field: Dict[str, Any], parent_name: Optional[str] =
234
241
else :
235
242
value = cast (str , parent_name )
236
243
return f'{ doc_comment } static immutable { fname } = "{ value } ";' # noqa: B907
244
+
245
+ if field .get ("default" , None ):
246
+ default_value = json .dumps (field ["default" ])
247
+ default_str = f'@defaultValue(q"<{ default_value } >") '
248
+ else :
249
+ default_str = ""
237
250
238
- annotate_str , type_str = self .parse_record_field_type (type_ , jsonld_pred )
239
- return f"{ doc_comment } { annotate_str } { type_str } { fname } ;"
251
+ annotate_str , type_str = self .parse_record_field_type (type_ , jsonld_pred , has_default = "default" in field )
252
+ return f"{ doc_comment } { default_str } { annotate_str } { type_str } { fname } ;"
240
253
241
254
def parse_record_schema (self , stype : Dict [str , Any ]) -> str :
242
255
"""Return a declaration string for a given record schema."""
0 commit comments