comparison Deprecated/Resources/CodeGeneration/template.in.ts.j2 @ 1401:f6a2d46d2b76

moved CodeGeneration into Deprecated
author Alain Mazy <alain@mazy.be>
date Wed, 29 Apr 2020 20:48:18 +0200
parents Resources/CodeGeneration/template.in.ts.j2@84af39146e76
children
comparison
equal deleted inserted replaced
1400:419d0320c344 1401:f6a2d46d2b76
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}} = "{{key}}"{% if not loop.last %},{%endif%}
43 {%endfor%}};
44
45 export function {{enum['name']}}_FromString(strValue:string) : {{enum['name']}}
46 {
47 {% for key in enum['fields'] %} if( strValue == "{{key}}" )
48 {
49 return {{enum['name']}}.{{key}};
50 }
51 {%endfor%}
52 let msg : string = `String ${strValue} cannot be converted to {{enum['name']}}. Possible values are: {% for key in enum['fields']%}{{key}}{% if not loop.last %}, {%endif%}{% endfor %}`;
53 throw new Error(msg);
54 }
55
56 export function {{enum['name']}}_ToString(value:{{enum['name']}}) : string
57 {
58 {% for key in enum['fields'] %} if( value == {{enum['name']}}.{{key}} )
59 {
60 return "{{key}}";
61 }
62 {%endfor%}
63 let msg : string = `Value ${value} cannot be converted to {{enum['name']}}. Possible values are: `;
64 {% for key in enum['fields']%} {
65 let _{{key}}_enumValue : string = {{enum['name']}}.{{key}}; // enums are strings in stonecodegen, so this will work.
66 let msg_{{key}} : string = `{{key}} (${_{{key}}_enumValue}){% if not loop.last %}, {%endif%}`;
67 msg = msg + msg_{{key}};
68 }
69 {%endfor%} throw new Error(msg);
70 }
71 {%endfor%}
72
73
74 {% for struct in structs%}export class {{struct['name']}} {
75 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} {{key}}:{{CanonToTs(struct['fields'][key]['type'])}};
76 {% endfor %}{% endif %}{% endif %}
77 constructor() {
78 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%}{% if NeedsTsConstruction(enums,CanonToTs(struct['fields'][key]['type'])) %} this.{{key}} = new {{CanonToTs(struct['fields'][key]['type'])}}();
79 {% endif %}
80 {% if struct['fields'][key]['defaultValue'] %} this.{{key}} = {{DefaultValueToTs(enums,struct['fields'][key])}};
81 {% endif %}{% endfor %}{% endif %}{% endif %} }
82
83 public StoneSerialize(): string {
84 let container: object = {};
85 container['type'] = '{{rootName}}.{{struct['name']}}';
86 container['value'] = this;
87 return JSON.stringify(container);
88 }
89
90 public static StoneDeserialize(valueStr: string) : {{struct['name']}}
91 {
92 let value: any = JSON.parse(valueStr);
93 StoneCheckSerializedValueType(value, '{{rootName}}.{{struct['name']}}');
94 let result: {{struct['name']}} = value['value'] as {{struct['name']}};
95 return result;
96 }
97 }
98 {% endfor %}
99 export interface IHandler {
100 {% for struct in structs%}{% if struct['__meta__'].handleInTypescript %} Handle{{struct['name']}}(value: {{struct['name']}}): boolean;
101 {% endif %}{% endfor %}};
102
103 /** Service function for StoneDispatchToHandler */
104 export function StoneDispatchJsonToHandler(
105 jsonValue: any, handler: IHandler): boolean
106 {
107 StoneCheckSerializedValueTypeGeneric(jsonValue);
108 let type: string = jsonValue["type"];
109 if (type == "")
110 {
111 // this should never ever happen
112 throw new Error("Caught empty type while dispatching");
113 }
114 {% for struct in structs%}{% if struct['__meta__'].handleInTypescript %} else if (type == "{{rootName}}.{{struct['name']}}")
115 {
116 let value = jsonValue["value"] as {{struct['name']}};
117 return handler.Handle{{struct['name']}}(value);
118 }
119 {% endif %}{% endfor %} else
120 {
121 return false;
122 }
123 }
124
125 /** Takes a serialized type and passes this to the handler */
126 export function StoneDispatchToHandler(
127 strValue: string, handler: IHandler): boolean
128 {
129 // console.//log("+------------------------------------------------+");
130 // console.//log("| StoneDispatchToHandler |");
131 // console.//log("+------------------------------------------------+");
132 // console.//log("strValue = ");
133 // console.//log(strValue);
134 let jsonValue: any = JSON.parse(strValue)
135 return StoneDispatchJsonToHandler(jsonValue, handler);
136 }