From ed4c81def097467988d4984732773dda549561b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fra=C3=9F?= Date: Tue, 11 Jul 2023 20:40:12 +0200 Subject: [PATCH] [mod] output:typescript --- source/outputs/typescript.ts | 122 ++++++++++++++++++----------------- 1 file changed, 63 insertions(+), 59 deletions(-) diff --git a/source/outputs/typescript.ts b/source/outputs/typescript.ts index a7af22b..3887bc5 100644 --- a/source/outputs/typescript.ts +++ b/source/outputs/typescript.ts @@ -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 = { + "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", - { - "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", - { - "name": domain.name, - } - ) - ), - "collection_value": ( - (domain.key_field === null) - ? "[]" - : "{}" - ), - } + ) + ]) ) + .reduce((x, y) => x.concat(y), []) ) - .map(x => (x + "\n")) - .join("\n") ); }, };