Skip to content

Commit 099b8e0

Browse files
committed
Support default field
1 parent 1eaa28c commit 099b8e0

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

schema_salad/dlang_codegen.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import datetime
44
import functools
5+
import json
56
import textwrap
67
from typing import IO, Any, Dict, List, Optional, Set, Tuple, Union, cast
78

@@ -65,7 +66,7 @@ def prologue(self) -> None:
6566
import salad.meta.dumper : genDumper;
6667
import salad.meta.impl : genCtor_, genIdentifier, genOpEq;
6768
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;
6970
import salad.primitives : SchemaBase;
7071
import salad.type : None, Union;
7172
@@ -167,6 +168,7 @@ def parse_record_field_type(
167168
type_: Any,
168169
jsonld_pred: Union[None, str, Dict[str, Any]],
169170
parent_has_idmap: bool = False,
171+
has_default: bool = False,
170172
) -> Tuple[str, str]:
171173
"""Return an annotation string and a type string."""
172174
annotations: List[str] = []
@@ -206,13 +208,18 @@ def parse_record_field_type(
206208
else:
207209
type_str = stype
208210
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})"
214221
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]
216223
type_str = f"{item_type}[]"
217224
elif shortname(type_["type"]) == "record":
218225
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] =
234241
else:
235242
value = cast(str, parent_name)
236243
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 = ""
237250

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};"
240253

241254
def parse_record_schema(self, stype: Dict[str, Any]) -> str:
242255
"""Return a declaration string for a given record schema."""

0 commit comments

Comments
 (0)