comparison Resources/CodeGeneration/template.in.ts @ 491:8e7e151ef472 bgo-commands-codegen

Unit tests pass for enum generation
author bgo-osimis
date Wed, 20 Feb 2019 20:51:30 +0100
parents 6470248790db
children 6fbf2eae7c88
comparison
equal deleted inserted replaced
490:6470248790db 491:8e7e151ef472
1 class Greeter { 1 /*
2 greeting: string; 2 1 2 3 4 5 6 7
3 constructor(message: string) { 3 12345678901234567890123456789012345678901234567890123456789012345678901234567890
4 this.greeting = message; 4 */
5
6 namespace {{rootName}}
7 {
8 function StoneCheckSerializedValueType(value: any, typeStr: string)
9 {
10 StoneCheckSerializedValueTypeGeneric(value);
11
12 if (value['type'] != typeStr)
13 {
14 throw new Error(
15 `Cannot deserialize type ${value['type']} into ${typeStr}`);
5 } 16 }
6 greet() { 17 }
7 return "Hello, " + this.greeting; 18
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)");
8 } 30 }
31 }
32
33 // end of generic methods
34 {% for enum in enums%}
35 export enum {{enum['name']}} {
36 {% for key in enumDict.keys()%}
37 {{key}},
38 {%endfor%}
39 };
40 {%endfor%}
41
42 export class Message1 {
43 a: number;
44 b: string;
45 c: EnumMonth0;
46 d: boolean;
47 public StoneSerialize(): string {
48 let container: object = {};
49 container['type'] = 'VsolStuff.Message1';
50 container['value'] = this;
51 return JSON.stringify(container);
52 }
53 };
54
55 export class Message2 {
56 constructor()
57 {
58 this.tata = new Array<Message1>();
59 this.tutu = new Array<string>();
60 this.titi = new Map<string, string>();
61 this.lulu = new Map<string, Message1>();
62 }
63 toto: string;
64 tata: Message1[];
65 tutu: string[];
66 titi: Map<string, string>;
67 lulu: Map<string, Message1>;
68
69 public StoneSerialize(): string {
70 let container: object = {};
71 container['type'] = 'VsolStuff.Message2';
72 container['value'] = this;
73 return JSON.stringify(container);
74 }
75 public static StoneDeserialize(valueStr: string) : Message2
76 {
77 let value: any = JSON.parse(valueStr);
78 StoneCheckSerializedValueType(value, "VsolStuff.Message2");
79 let result: Message2 = value['value'] as Message2;
80 return result;
81 }
82 };
83
84 export interface IDispatcher
85 {
86 HandleMessage1(value: Message1): boolean;
87 HandleMessage2(value: Message2): boolean;
88 };
89
90 /** Service function for StoneDispatchToHandler */
91 export function StoneDispatchJsonToHandler(
92 jsonValueStr: string, dispatcher: IDispatcher): boolean
93 {
94 let jsonValue: any = JSON.parse(jsonValueStr);
95 StoneCheckSerializedValueTypeGeneric(jsonValue);
96 let type: string = jsonValue["type"];
97 if (type == "")
98 {
99 // this should never ever happen
100 throw new Error("Caught empty type while dispatching");
101 }
102 else if (type == "VsolStuff.Message1")
103 {
104 let value = jsonValue["value"] as Message1;
105 return dispatcher.HandleMessage1(value);
106 }
107 else if (type == "VsolStuff.Message2")
108 {
109 let value = jsonValue["value"] as Message2;
110 return dispatcher.HandleMessage2(value);
111 }
112 else
113 {
114 return false;
115 }
116 }
117
118 /** Takes a serialized type and passes this to the dispatcher */
119 export function StoneDispatchToHandler(
120 strValue: string, dispatcher: IDispatcher): boolean
121 {
122 let jsonValue: any = JSON.parse(strValue)
123 return StoneDispatchJsonToHandler(jsonValue, dispatcher);
124 }
9 } 125 }
10 enum Color { Red, Green, Blue };
11
12 class TestMessage {
13 s1: string;
14 s2: Array<string>;
15 s3: Array<Array<string>>;
16 s4: Map<string, number>;
17 s5: Map<number, Array<string>>;
18 s6: Color;
19 s7: boolean;
20 }
21
22 let tm = new TestMessage();
23 tm.s2 = new Array<string>()
24 tm.s2.push("toto");
25 tm.s2.push("toto2");
26 tm.s2.push("toto3");
27 tm.s4 = new Map<string, number>();
28 tm.s4["toto"] = 42;
29 tm.s4["toto"] = 1999;
30 tm.s4["tatata"] = 1999;
31 tm.s6 = Color.Red;
32 tm.s7 = true
33
34 let txt = JSON.stringify(tm)
35 console.log(txt);
36
37 let greeter = new Greeter("world");
38