[mod] output:typescript
This commit is contained in:
parent
e25cb5915b
commit
ed4c81def0
|
|
@ -1,73 +1,77 @@
|
|||
const output_typescript : type_output = {
|
||||
"render": function (input_data) {
|
||||
return (
|
||||
input_data["domains"]
|
||||
.map(
|
||||
(domain) => lib_plankton.string.coin(
|
||||
"type type_{{name}} = {\n{{fields}}\n};\nlet collection_{{name}} : {{collection_type}} = {{collection_value}};\n",
|
||||
{
|
||||
"name": domain.name,
|
||||
"fields": (
|
||||
domain.data_fields
|
||||
.map(
|
||||
(data_field) => lib_plankton.string.coin(
|
||||
"\t{{name}} : {{type}};{{macro_comment}}",
|
||||
{
|
||||
const map_primitive_type = function (typename : string) : lib_plankton.prog.struct_type {
|
||||
const mymap : Record<string, lib_plankton.prog.struct_type> = {
|
||||
"boolean": new lib_plankton.prog.struct_type_boolean(),
|
||||
"integer": new lib_plankton.prog.struct_type_integer(),
|
||||
// "float": new lib_plankton.prog.struct_type_integer(),
|
||||
"string_short": new lib_plankton.prog.struct_type_string(),
|
||||
"string_medium": new lib_plankton.prog.struct_type_string(),
|
||||
"string_long": new lib_plankton.prog.struct_type_string(),
|
||||
};
|
||||
return mymap[typename];
|
||||
};
|
||||
const name_type : string = ("type_" + domain.name);
|
||||
const name_collection : string = ("collection_" + domain.name);
|
||||
const prog_output : lib_plankton.prog.type_output = lib_plankton.prog.output_typescript();
|
||||
return prog_output.render_program(
|
||||
new lib_plankton.prog.struct_program(
|
||||
input_data["domains"]
|
||||
.map(
|
||||
(domain) => ([
|
||||
new lib_plankton.prog.struct_statement_type_definition(
|
||||
name_type,
|
||||
new lib_plankton.prog.struct_type_record(
|
||||
domain.data_fields
|
||||
.map(
|
||||
(data_field) => ({
|
||||
"name": data_field.name,
|
||||
"type": lib_plankton.string.coin(
|
||||
(data_field.nullable ? "(null | {{core}})" : "{{core}}"),
|
||||
{
|
||||
"core": {
|
||||
"boolean": "boolean",
|
||||
"integer": "number",
|
||||
"float": "number",
|
||||
"string_short": "string",
|
||||
"string_medium": "string",
|
||||
"string_long": "string",
|
||||
}[data_field["type"]],
|
||||
}
|
||||
),
|
||||
"macro_comment": (
|
||||
(data_field.description !== null)
|
||||
? lib_plankton.string.coin(
|
||||
" // {{comment}}",
|
||||
{
|
||||
"comment": data_field.description,
|
||||
}
|
||||
"type": (
|
||||
data_field.nullable
|
||||
? new lib_plankton.prog.struct_type_union(
|
||||
new lib_plankton.prog.struct_type_literal(
|
||||
new lib_plankton.prog.struct_expression_literal(
|
||||
null
|
||||
)
|
||||
),
|
||||
map_primitive_type(data_field["type"])
|
||||
)
|
||||
: ""
|
||||
: map_primitive_type(data_field["type"])
|
||||
),
|
||||
}
|
||||
"mandatory": true,
|
||||
})
|
||||
)
|
||||
)
|
||||
// .map(x => ("\t" + x))
|
||||
.join("\n")
|
||||
),
|
||||
"collection_type": (
|
||||
(domain.key_field === null)
|
||||
? lib_plankton.string.coin(
|
||||
"Array<type_{{name}}>",
|
||||
{
|
||||
"name": domain.name,
|
||||
}
|
||||
new lib_plankton.prog.struct_statement_declaration(
|
||||
false,
|
||||
name_collection,
|
||||
(
|
||||
(domain.key_field === null)
|
||||
? new lib_plankton.prog.struct_type_list(
|
||||
new lib_plankton.prog.struct_type_construction(
|
||||
name_type,
|
||||
null
|
||||
)
|
||||
)
|
||||
: new lib_plankton.prog.struct_type_map(
|
||||
new lib_plankton.prog.struct_type_integer(),
|
||||
new lib_plankton.prog.struct_type_construction(
|
||||
name_type,
|
||||
null
|
||||
)
|
||||
)
|
||||
),
|
||||
(
|
||||
(domain.key_field === null)
|
||||
? new lib_plankton.prog.struct_expression_literal([])
|
||||
: new lib_plankton.prog.struct_expression_literal({})
|
||||
)
|
||||
: lib_plankton.string.coin(
|
||||
"Record<number, type_{{name}}>",
|
||||
{
|
||||
"name": domain.name,
|
||||
}
|
||||
)
|
||||
),
|
||||
"collection_value": (
|
||||
(domain.key_field === null)
|
||||
? "[]"
|
||||
: "{}"
|
||||
),
|
||||
}
|
||||
)
|
||||
])
|
||||
)
|
||||
.reduce((x, y) => x.concat(y), [])
|
||||
)
|
||||
.map(x => (x + "\n"))
|
||||
.join("\n")
|
||||
);
|
||||
},
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue