comparison Resources/CodeGeneration/template.in.ts @ 494:fc17251477d6 bgo-commands-codegen

TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
author bgo-osimis
date Sat, 23 Feb 2019 10:18:13 +0100
parents 6fbf2eae7c88
children 6405435480ae
comparison
equal deleted inserted replaced
493:6fbf2eae7c88 494:fc17251477d6
1 /* 1 /*
2 1 2 3 4 5 6 7 2 1 2 3 4 5 6 7
3 12345678901234567890123456789012345678901234567890123456789012345678901234567890 3 12345678901234567890123456789012345678901234567890123456789012345678901234567890
4 */ 4 */
5 5
6 namespace {{rootName}} 6 function StoneCheckSerializedValueType(value: any, typeStr: string)
7 { 7 {
8 function StoneCheckSerializedValueType(value: any, typeStr: string) 8 StoneCheckSerializedValueTypeGeneric(value);
9
10 if (value['type'] != typeStr)
9 { 11 {
10 StoneCheckSerializedValueTypeGeneric(value); 12 throw new Error(
13 `Cannot deserialize type ${value['type']} into ${typeStr}`);
14 }
15 }
11 16
12 if (value['type'] != typeStr) 17 function isString(val: any) :boolean
13 { 18 {
14 throw new Error( 19 return ((typeof val === 'string') || (val instanceof String));
15 `Cannot deserialize type ${value['type']} into ${typeStr}`); 20 }
16 } 21
22 function StoneCheckSerializedValueTypeGeneric(value: any)
23 {
24 // console.//log("+-------------------------------------------------+");
25 // console.//log("| StoneCheckSerializedValueTypeGeneric |");
26 // console.//log("+-------------------------------------------------+");
27 // console.//log("value = ");
28 // console.//log(value);
29 if ( (!('type' in value)) || (!isString(value.type)) )
30 {
31 throw new Error(
32 "Cannot deserialize value ('type' key invalid)");
17 } 33 }
18 34 }
19 function isString(val: any) :boolean
20 {
21 return ((typeof val === 'string') || (val instanceof String));
22 }
23
24 function StoneCheckSerializedValueTypeGeneric(value: any)
25 {
26 if ( (!('type' in value)) || (!isString(value)) )
27 {
28 throw new Error(
29 "Cannot deserialize value ('type' key invalid)");
30 }
31 }
32 35
33 // end of generic methods 36 // end of generic methods
34 {% for enum in enums%} 37 {% for enum in enums%}
35 export enum {{enum['name']}} { 38 export enum {{enum['name']}} {
36 {% for key in enumDict.keys()%} 39 {% for key in enum['fields']%}{{key}},
37 {{key}}, 40 {%endfor%}
38 {%endfor%} 41 };
39 };
40 {%endfor%} 42 {%endfor%}
41 43
42
43 """ // end of generic methods
44 {% for struct in structs%} export class {{struct['name']}} { 44 {% for struct in structs%} export class {{struct['name']}} {
45 {% for key in struct['fields']%} {{key}}:{{CanonToTs(struct['fields'][key])}}; 45 {% for key in struct['fields']%} {{key}}:{{CanonToTs(struct['fields'][key])}};
46 {% endfor %} 46 {% endfor %}
47 constructor() { 47 constructor() {
48 {% for key in struct['fields']%} {{key}} = new {{CanonToTs(struct['fields'][key])}}(); 48 {% for key in struct['fields']%}{% if NeedsTsConstruction(enums,CanonToTs(struct['fields'][key])) %} this.{{key}} = new {{CanonToTs(struct['fields'][key])}}();
49 {% endfor %} } 49 {% endif %}{% endfor %} }
50 50
51 public StoneSerialize(): string { 51 public StoneSerialize(): string {
52 let container: object = {}; 52 let container: object = {};
53 container['type'] = '{{rWholootName}}.{{struct['name']}}'; 53 container['type'] = '{{rWholootName}}.{{struct['name']}}';
54 container['value'] = this; 54 container['value'] = this;
55 return JSON.stringify(container); 55 return JSON.stringify(container);
56 } 56 }
57 57
58 public static StoneDeserialize(valueStr: string) : {{struct['name']}} 58 public static StoneDeserialize(valueStr: string) : {{struct['name']}}
59 { 59 {
60 let value: any = JSON.parse(valueStr); 60 let value: any = JSON.parse(valueStr);
61 StoneCheckSerializedValueType(value, '{{rootName}}.{{struct['name']}}'); 61 StoneCheckSerializedValueType(value, '{{rootName}}.{{struct['name']}}');
62 let result: {{struct['name']}} = value['value'] as {{struct['name']}}; 62 let result: {{struct['name']}} = value['value'] as {{struct['name']}};
63 return result; 63 return result;
64 }
65
66 } 64 }
65 }
67 66
68 {% endfor %} 67 {% endfor %}
69 68
70 }; 69 export interface IDispatcher {
70 {% for struct in structs%} Handle{{struct['name']}}(value: {{struct['name']}}): boolean;
71 {% endfor %}
72 };
71 73
72 export interface IDispatcher 74 /** Service function for StoneDispatchToHandler */
75 export function StoneDispatchJsonToHandler(
76 jsonValue: any, dispatcher: IDispatcher): boolean
77 {
78 StoneCheckSerializedValueTypeGeneric(jsonValue);
79 let type: string = jsonValue["type"];
80 if (type == "")
73 { 81 {
74 {% for struct in structs%} HandleMessage1(value: {{struct['name']}}): boolean; 82 // this should never ever happen
75 {% endfor %} 83 throw new Error("Caught empty type while dispatching");
76 }; 84 }
77 85 {% for struct in structs%} else if (type == "{{rootName}}.{{struct['name']}}")
78 /** Service function for StoneDispatchToHandler */
79 export function StoneDispatchJsonToHandler(
80 jsonValueStr: string, dispatcher: IDispatcher): boolean
81 { 86 {
82 let jsonValue: any = JSON.parse(jsonValueStr); 87 let value = jsonValue["value"] as {{struct['name']}};
83 StoneCheckSerializedValueTypeGeneric(jsonValue); 88 return dispatcher.Handle{{struct['name']}}(value);
84 let type: string = jsonValue["type"];
85 if (type == "")
86 {
87 // this should never ever happen
88 throw new Error("Caught empty type while dispatching");
89 }
90 {% for struct in structs%} else if (type == "VsolStuff.{{struct['name']}}")
91 {
92 let value = jsonValue["value"] as Message1;
93 return dispatcher.HandleMessage1(value);
94 }
95 {% enfor %}
96 else
97 {
98 return false;
99 }
100 } 89 }
101 90 {% endfor %}
102 /** Takes a serialized type and passes this to the dispatcher */ 91 else
103 export function StoneDispatchToHandler(
104 strValue: string, dispatcher: IDispatcher): boolean
105 { 92 {
106 let jsonValue: any = JSON.parse(strValue) 93 return false;
107 return StoneDispatchJsonToHandler(jsonValue, dispatcher);
108 } 94 }
109 } 95 }
96
97 /** Takes a serialized type and passes this to the dispatcher */
98 export function StoneDispatchToHandler(
99 strValue: string, dispatcher: IDispatcher): boolean
100 {
101 // console.//log("+------------------------------------------------+");
102 // console.//log("| StoneDispatchToHandler |");
103 // console.//log("+------------------------------------------------+");
104 // console.//log("strValue = ");
105 // console.//log(strValue);
106 let jsonValue: any = JSON.parse(strValue)
107 return StoneDispatchJsonToHandler(jsonValue, dispatcher);
108 }