Example #1
0
 def scalar_defaults_mapping(self) -> Dict[PrimitiveType, str]:
     return {
         PrimitiveType.BOOL: scripter.literal(False),
         PrimitiveType.INT: scripter.literal(0),
         PrimitiveType.STRING: scripter.literal(""),
         PrimitiveType.DOUBLE: scripter.literal(0.0),
         PrimitiveType.ANY: scripter.literal(0),
     }
Example #2
0
    def default_factory_expr(self) -> Alt[Expression]:
        for type_decon in self.material_type_decon:
            codegen_file = self.idol_data.codegen_file(type_decon.t.named)
            return codegen_file.default_factory_expr

        return Alt(
            as_expression(scripter.thunk(scripter.literal(val)))
            for _, val in self.scalar_dec.get_literal()) ^ Alt(
                as_expression(
                    scripter.thunk(
                        scripter.literal(
                            self.scalar_primitive_default_map[prim])))
                for prim in self.scalar_dec.get_primitive()
                if prim in self.scalar_primitive_default_map)
Example #3
0
 def literal_field_expr(self) -> Alt[FieldExpressionComposer]:
     return Alt(
         self.FieldExpressionComposer(
             import_expr(
                 self.idol_mar.marshmallow_field_exported("Constant")),
             [scripter.literal(val)],
         ) for _, val in self.scalar_dec.get_literal())
Example #4
0
 def inner(state: GeneratorAcc, path: Path) -> str:
     return scripter.invocation(
         scripter.prop_access(
             state.import_ident(path,
                                self.idol_py.idol_py_file.literal),
             "of"),
         scripter.literal(value),
     )
Example #5
0
 def expr(state: GeneratorAcc, path: Path) -> str:
     args, kwds = self.resolve_args_and_kwds(state, path)
     return scripter.invocation(
         state.import_ident(path, idol_mar.idol_mar_file.wrap_field),
         self.constructor_ident(state, path),
         scripter.literal(name),
         *list(scripter.thunk(arg) for arg in args),
         **kwds,
     )
Example #6
0
 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"),
             ),
         ))
Example #7
0
 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")),
         ),
     )
Example #8
0
 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"),
             ),
         ))
Example #9
0
 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"),
             ),
         ))
Example #10
0
 def declared_field(self) -> Alt[Exported]:
     return Alt(
         self.export(
             self.codegen_file.default_field_name,
             scripter.assignable(
                 self.apply_expr(
                     FieldExpressionComposer(
                         import_expr(
                             Exported(Path("marshmallow_enum"),
                                      "EnumField")),
                         [self.import_ident(declared_enum)],
                         dict(by_value=scripter.literal(True)),
                     ).curried_field_declaration_expr(
                         self.codegen_file.default_field_name,
                         self.codegen_file.idol_mar))),
         ) for declared_enum in self.declared_enum)
Example #11
0
                    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,
                        )
Example #12
0
    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))))
Example #13
0
 def declared_schema(self) -> Alt[Exported]:
     return Alt.lift(
         self.export(self.codegen_file.default_schema_name,
                     scripter.assignable(scripter.literal(None))))