def container_constructor_expr(self) -> Alt[Expression]: container_con: Alt[Expression] = self.ts_decon.get_repeated( ) and Alt.lift(import_expr(self.idol_py.idol_py_file.list)) container_con ^= self.ts_decon.get_map() and Alt.lift( import_expr(self.idol_py.idol_py_file.map)) def container_expression(container_expr: Expression, scalar_expression: Expression) -> Expression: def inner(state: GeneratorAcc, path: Path): return scripter.invocation( scripter.prop_access(container_expr(state, path), "of"), scalar_expression(state, path), scripter.invocation( "dict", atleast_one=scripter.literal( includes_tag(self.ts_decon.context.type_tags, "atleast_one")), ), ) return inner return Alt( container_expression(container_con_expr, scalar_con_expr) for container_con_expr in container_con for scalar in self.inner_scalar for scalar_con_expr in scalar.constructor_expr)
def map_field_composer(self) -> Alt["FieldExpressionComposer"]: if self.ts_decon.get_map(): return Alt.lift( FieldExpressionComposer( import_expr( self.idol_mar.marshmallow_field_exported("Dict")))) return Alt.empty()
def declared_dataclass(self) -> Alt[Exported]: return Alt.lift( self.export( self.default_dataclass_name, scripter.nameable_class_dec( [], [ line for field_name, field in self.fields for typing_expr in field.typing_expr for default_factory_expr in field.default_factory_expr for line in (scripter.comments( get_tag_values(field.ts_decon.context.field_tags, "description") ) + [ scripter.assignment( get_safe_ident(field_name), self.apply_expr( self.dataclass_field_expr( default_factory=default_factory_expr)), typing=self.apply_expr(typing_expr), ) ]) ], doc_str=get_tag_values(self.codegen_file.t.tags, "description"), decorators=[ self.import_ident( Exported(Path("dataclasses"), "dataclass")) ], ), ))
def default_factory_expr(self) -> Alt[Expression]: if includes_tag(self.ts_decon.context.field_tags, "optional"): return Alt.lift(as_expression(scripter.thunk("None"))) return ( Alt(as_expression("list") for _ in self.ts_decon.get_repeated()) ^ Alt(as_expression("dict") for _ in self.ts_decon.get_map()) ^ Alt(expr for scalar in self.inner_scalar for expr in scalar.default_factory_expr if self.ts_decon.get_scalar()))
def declared_schema(self) -> Alt[Exported]: for scalar in self.ts_decon.get_scalar(): for ref in scalar.get_alias(): schema: Alt[Exported] = ( self.codegen_file.idol_mar.scaffold_file( ref).declared_schema if ref.qualified_name in self.config.params.scaffold_types.obj else self. codegen_file.idol_mar.codegen_file(ref).declared_schema) for exported_schema in schema: return Alt.lift( self.export( self.codegen_file.default_schema_name, scripter.assignable( self.import_ident(exported_schema)), )) return Alt.lift( self.export(self.codegen_file.default_schema_name, scripter.assignable(scripter.literal(None))))
def default_factory_expr(self) -> Alt[Expression]: for declared_enum in self.codegen_file.declared_field_constructor: def inner(state: GeneratorAcc, path: Path) -> str: return scripter.thunk( scripter.invocation( "next", scripter.invocation( "iter", state.import_ident(path, declared_enum)))) return Alt.lift(inner) return Alt.empty()
def declared_enum(self) -> Alt[Exported]: return Alt.lift( self.export( self.default_enum_name, scripter.nameable_class_dec( [self.import_ident(Exported(Path("enum"), "Enum"))], [ scripter.assignment(name.upper(), scripter.literal(name)) for name in self.options ], doc_str=get_tag_values(self.codegen_file.t.tags, "description"), ), ))
def declared_ident(self) -> Alt[Exported]: return Alt.lift( self.export( self.codegen_file.default_type_name, scripter.nameable_class_dec( [ self.import_ident( self.codegen_file.idol_py.idol_py_file.struct) ], [ line for field_name, field in self.fields for typing_expr in field.typing_expr for line in (scripter.comments( get_tag_values(field.ts_decon.context.field_tags, "description") ) + [ scripter.typing(get_safe_ident(field_name), self.apply_expr(typing_expr)), "", ]) ] + [ scripter.assignment( "__field_constructors__", scripter.array( scripter.tuple( scripter.literal(field_name), scripter.literal(get_safe_ident( field_name)), self.apply_expr(field_expr), scripter.invocation( "dict", optional=scripter.literal( includes_tag( field.ts_decon.context. field_tags, "optional")), ), ) for field_name, field in self.fields for field_expr in field.constructor_expr), ) ], doc_str=get_tag_values(self.codegen_file.t.tags, "description"), ), ))
def declared_field(self) -> Alt[Exported]: for declared_schema in self.declared_schema: schema_expr: Expression = import_expr(declared_schema) if self.codegen_file.t.named.qualified_name in self.config.params.scaffold_types.obj: for scaffold_declared_schema in self.codegen_file.idol_mar.scaffold_file( self.codegen_file.t.named).declared_schema: def lazy_import(state: GeneratorAcc, path: Path) -> str: import_module = state.import_ident( path, Exported(Path("importlib"), "import_module")) return scripter.prop_access( scripter.invocation( import_module, scripter.literal( ImportPath.as_python_module_path( path.import_path_to( scaffold_declared_schema.path). rel_path)), "__package__", ), scaffold_declared_schema.ident, ) schema_expr = lazy_import return Alt.lift( self.export( self.codegen_file.default_field_name, scripter.assignable( self.apply_expr( FieldExpressionComposer( import_expr( self.codegen_file.idol_mar. marshmallow_field_exported("Nested")), [self.apply_expr(schema_expr)], ).curried_field_declaration_expr( self.codegen_file.default_field_name, self.codegen_file.idol_mar))), )) return Alt.empty()
def declared_schema(self) -> Alt[Exported]: return Alt.lift( self.export( self.codegen_file.default_schema_name, scripter.nameable_class_dec( [ self.import_ident( Exported(Path("marshmallow"), "Schema")) ], [ line for field_name, field in self.fields for composer in field.field_composer for line in (scripter.comments( get_tag_values(field.ts_decon.context.field_tags, "description") ) + [ scripter.assignment( get_safe_ident(field_name), self.apply_expr( composer.with_more_kwds( dict( dump_to=scripter.literal( field_name), load_from=scripter.literal( field_name), allow_none=scripter.literal( includes_tag( field.ts_decon.context. field_tags, "optional", )), )).field_instance_expr()), ) ]) ], doc_str=get_tag_values(self.codegen_file.t.tags, "description"), ), ))
def declared_schema(self) -> Alt[Exported]: return Alt.lift( self.export(self.codegen_file.default_schema_name, scripter.assignable(scripter.literal(None))))