def emit(g: Graph) -> Module: i = 0 m = Module() variables: t.Dict[int, Symbol] = {} # TODO: with type # TODO: name original_args = [ f"{node.name} string" for node in g.nodes if node.is_primitive ] with m.func("run", *original_args, return_="error"): for node in topological_sorted(g): if node.is_primitive: variables[node.uid] = m.symbol(node.name) continue args = [] for dep in node.depends: args.append(variables[dep.uid]) m.letN return_type = node.metadata.get("return_type", "") if return_type == "": [variables[node.uid]] = m.letN([f"v{i}"], m.symbol(node.name)(*args)) elif return_type == "with-err": variables[node.uid], err = m.letN((f"v{i}", "err"), m.symbol(node.name)(*args)) with m.if_("err != nil"): m.return_("err") elif return_type == "with-cleanup": variables[node.uid], cleanup = m.letN( (f"v{i}", "cleanup"), m.symbol(node.name)(*args)) m.stmt("defer cleanup()") elif return_type == "with-cleanup-err": variables[node.uid], cleanup, err = m.letN( (f"v{i}", "cleanup", "err"), m.symbol(node.name)(*args)) with m.if_("err != nil"): m.return_("err") m.stmt("defer cleanup()") else: raise ValueError(f"unexpected return_type {return_type}") i += 1 m.return_("nil") return m
def emit(self, m: Module, err: Symbol) -> None: with m.if_(f"{err} != nil"): m.return_(err)
def emit_unmarshalJSON(m: Module, item: Item, *, resolver: Resolver) -> Definition: this = m.symbol(f"{item.type_.__name__[0].lower()}") this_type = f"{resolver.resolve_gotype(item.type_)}" this_type_pointer = f"*{this_type}" # func (ob *Ob) UnmarshalJSON(b []byte) error { b = m.symbol("b") m.stmt( f"func ({this} {this_type_pointer}) UnmarshalJSON({b} []byte) error {{" ) with m.scope(): # var err *maperr.Error err = m.symbol("err") maperr_pkg = m.import_("github.com/podhmo/maperr") m.stmt(f"var {err} *{maperr_pkg}.Error") m.sep() # var inner struct { # ... # } m.stmt("// loading internal data") inner = m.symbol("inner") m.stmt(f"var {inner} struct {{") with m.scope(): for name, info, metadata in item.fields: if name.startswith("_"): continue # xxx: if "_override_type" in metadata: gotype: str = metadata["_override_type"] elif has_class_object(info): json_pkg = m.import_("encoding/json") gotype: str = str(json_pkg.RawMessage) else: gotype: str = resolver.resolve_gotype(info.raw) m.append(f'{goname(name)} *{gotype} `json:"{name}"`') m.stmt("// required" if metadata["required"] else "") m.stmt("}") # if rawErr := json.Unmarshal(b, &inner); rawErr != nil { # ... # } json_pkg = m.import_("encoding/json") raw_err = m.symbol("rawErr") with m.if_( f"{raw_err} := {json_pkg}.Unmarshal(b, &{inner}); {raw_err} != nil" ): m.return_(err.AddSummary(raw_err.Error())) m.sep() # if <field> != nil { # ob.<field> = *<field> # } else { # m.add(<field>, "required") # } rawerr = m.symbol("rawerr") m.stmt("// binding field value and required check") with m.block(): for name, info, metadata in item.fields: field = m.symbol(goname(name)) with m.if_(f"{inner}.{field} != nil"): if has_class_object(info): # pointer if info.is_optional: gotype: str = resolver.resolve_gotype( info.normalized) m.stmt(f"{this}.{goname(name)} = &{gotype}{{}}") ref = f"{this}.{field}" elif hasattr(info, "args"): # xxx gotype: str = resolver.resolve_gotype( info.normalized) m.stmt(f"{this}.{goname(name)} = {gotype}{{}}") ref = f"&{this}.{field}" else: ref = f"&{this}.{field}" with m.if_( f"{rawerr} := json.Unmarshal(*{inner}.{field}, {ref}); {rawerr} != nil" ): m.stmt( f'{err} = {err}.Add("{name}", {maperr_pkg}.Message{{Error: {rawerr}}})' ) else: m.stmt(f"{this}.{field} = *{inner}.{field}") if metadata["required"]: with m.else_(): m.stmt( f'{err} = err.Add("{name}", {maperr_pkg}.Message{{Text: "required"}})' ) m.sep() # NOTE: for injecting code from extrnal area code_module = m.submodule("", newline=False) # return err.Untyped() m.return_(err.Untyped()) m.stmt("}") return Definition(name="UnmarshalJSON", code_module=code_module)