comparison Resources/CodeGeneration/template.in.ts.j2 @ 515:1dbf2d9ed1e4 bgo-commands-codegen

Added .j2 extension to the Jinja2 template files to allow for a better syntax highlighting experience (a.o. in vscode)
author Benjamin Golinvaux <bgo@osimis.io>
date Mon, 11 Mar 2019 14:39:31 +0100
parents Resources/CodeGeneration/template.in.ts@dea3787a8f4b
children 17106b29ed6d
comparison
equal deleted inserted replaced
514:381144d2434f 515:1dbf2d9ed1e4
1 /*
2 1 2 3 4 5 6 7
3 12345678901234567890123456789012345678901234567890123456789012345678901234567890
4
5 Generated on {{currentDatetime}} by stonegentool
6
7 */
8
9 function StoneCheckSerializedValueType(value: any, typeStr: string)
10 {
11 StoneCheckSerializedValueTypeGeneric(value);
12
13 if (value['type'] != typeStr)
14 {
15 throw new Error(
16 `Cannot deserialize type ${value['type']} into ${typeStr}`);
17 }
18 }
19
20 function isString(val: any) :boolean
21 {
22 return ((typeof val === 'string') || (val instanceof String));
23 }
24
25 function StoneCheckSerializedValueTypeGeneric(value: any)
26 {
27 // console.//log("+-------------------------------------------------+");
28 // console.//log("| StoneCheckSerializedValueTypeGeneric |");
29 // console.//log("+-------------------------------------------------+");
30 // console.//log("value = ");
31 // console.//log(value);
32 if ( (!('type' in value)) || (!isString(value.type)) )
33 {
34 throw new Error(
35 "Cannot deserialize value ('type' key invalid)");
36 }
37 }
38
39 // end of generic methods
40 {% for enum in enums%}
41 export enum {{enum['name']}} {
42 {% for key in enum['fields']%}{{key}},
43 {%endfor%}
44 };
45 {%endfor%}
46
47 {% for struct in structs%}export class {{struct['name']}} {
48 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} {{key}}:{{CanonToTs(struct['fields'][key])}};
49 {% endfor %}{% endif %}{% endif %}
50 constructor() {
51 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%}{% if NeedsTsConstruction(enums,CanonToTs(struct['fields'][key])) %} this.{{key}} = new {{CanonToTs(struct['fields'][key])}}();
52 {% endif %}{% endfor %}{% endif %}{% endif %} }
53
54 public StoneSerialize(): string {
55 let container: object = {};
56 container['type'] = '{{rootName}}.{{struct['name']}}';
57 container['value'] = this;
58 return JSON.stringify(container);
59 }
60
61 public static StoneDeserialize(valueStr: string) : {{struct['name']}}
62 {
63 let value: any = JSON.parse(valueStr);
64 StoneCheckSerializedValueType(value, '{{rootName}}.{{struct['name']}}');
65 let result: {{struct['name']}} = value['value'] as {{struct['name']}};
66 return result;
67 }
68 }
69 {% endfor %}
70 export interface IHandler {
71 {% for struct in structs%}{% if struct['__meta__'].handleInTypescript %} Handle{{struct['name']}}(value: {{struct['name']}}): boolean;
72 {% endif %}{% endfor %}};
73
74 /** Service function for StoneDispatchToHandler */
75 export function StoneDispatchJsonToHandler(
76 jsonValue: any, handler: IHandler): boolean
77 {
78 StoneCheckSerializedValueTypeGeneric(jsonValue);
79 let type: string = jsonValue["type"];
80 if (type == "")
81 {
82 // this should never ever happen
83 throw new Error("Caught empty type while dispatching");
84 }
85 {% for struct in structs%}{% if struct['__meta__'].handleInTypescript %} else if (type == "{{rootName}}.{{struct['name']}}")
86 {
87 let value = jsonValue["value"] as {{struct['name']}};
88 return handler.Handle{{struct['name']}}(value);
89 }
90 {% endif %}{% endfor %} else
91 {
92 return false;
93 }
94 }
95
96 /** Takes a serialized type and passes this to the handler */
97 export function StoneDispatchToHandler(
98 strValue: string, handler: IHandler): boolean
99 {
100 // console.//log("+------------------------------------------------+");
101 // console.//log("| StoneDispatchToHandler |");
102 // console.//log("+------------------------------------------------+");
103 // console.//log("strValue = ");
104 // console.//log(strValue);
105 let jsonValue: any = JSON.parse(strValue)
106 return StoneDispatchJsonToHandler(jsonValue, handler);
107 }