Mercurial > hg > orthanc-stone
comparison Resources/CodeGeneration/template.in.ts @ 493:6fbf2eae7c88 bgo-commands-codegen
All unit tests pass for generation, including handler and dispatcher
author | bgo-osimis |
---|---|
date | Fri, 22 Feb 2019 10:48:43 +0100 |
parents | 8e7e151ef472 |
children | fc17251477d6 |
comparison
equal
deleted
inserted
replaced
491:8e7e151ef472 | 493:6fbf2eae7c88 |
---|---|
37 {{key}}, | 37 {{key}}, |
38 {%endfor%} | 38 {%endfor%} |
39 }; | 39 }; |
40 {%endfor%} | 40 {%endfor%} |
41 | 41 |
42 export class Message1 { | 42 |
43 a: number; | 43 """ // end of generic methods |
44 b: string; | 44 {% for struct in structs%} export class {{struct['name']}} { |
45 c: EnumMonth0; | 45 {% for key in struct['fields']%} {{key}}:{{CanonToTs(struct['fields'][key])}}; |
46 d: boolean; | 46 {% endfor %} |
47 constructor() { | |
48 {% for key in struct['fields']%} {{key}} = new {{CanonToTs(struct['fields'][key])}}(); | |
49 {% endfor %} } | |
50 | |
47 public StoneSerialize(): string { | 51 public StoneSerialize(): string { |
48 let container: object = {}; | 52 let container: object = {}; |
49 container['type'] = 'VsolStuff.Message1'; | 53 container['type'] = '{{rWholootName}}.{{struct['name']}}'; |
50 container['value'] = this; | 54 container['value'] = this; |
51 return JSON.stringify(container); | 55 return JSON.stringify(container); |
52 } | 56 } |
53 }; | |
54 | 57 |
55 export class Message2 { | 58 public static StoneDeserialize(valueStr: string) : {{struct['name']}} |
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 { | 59 { |
77 let value: any = JSON.parse(valueStr); | 60 let value: any = JSON.parse(valueStr); |
78 StoneCheckSerializedValueType(value, "VsolStuff.Message2"); | 61 StoneCheckSerializedValueType(value, '{{rootName}}.{{struct['name']}}'); |
79 let result: Message2 = value['value'] as Message2; | 62 let result: {{struct['name']}} = value['value'] as {{struct['name']}}; |
80 return result; | 63 return result; |
81 } | 64 } |
65 | |
66 } | |
67 | |
68 {% endfor %} | |
69 | |
82 }; | 70 }; |
83 | 71 |
84 export interface IDispatcher | 72 export interface IDispatcher |
85 { | 73 { |
86 HandleMessage1(value: Message1): boolean; | 74 {% for struct in structs%} HandleMessage1(value: {{struct['name']}}): boolean; |
87 HandleMessage2(value: Message2): boolean; | 75 {% endfor %} |
88 }; | 76 }; |
89 | 77 |
90 /** Service function for StoneDispatchToHandler */ | 78 /** Service function for StoneDispatchToHandler */ |
91 export function StoneDispatchJsonToHandler( | 79 export function StoneDispatchJsonToHandler( |
92 jsonValueStr: string, dispatcher: IDispatcher): boolean | 80 jsonValueStr: string, dispatcher: IDispatcher): boolean |
97 if (type == "") | 85 if (type == "") |
98 { | 86 { |
99 // this should never ever happen | 87 // this should never ever happen |
100 throw new Error("Caught empty type while dispatching"); | 88 throw new Error("Caught empty type while dispatching"); |
101 } | 89 } |
102 else if (type == "VsolStuff.Message1") | 90 {% for struct in structs%} else if (type == "VsolStuff.{{struct['name']}}") |
103 { | 91 { |
104 let value = jsonValue["value"] as Message1; | 92 let value = jsonValue["value"] as Message1; |
105 return dispatcher.HandleMessage1(value); | 93 return dispatcher.HandleMessage1(value); |
106 } | 94 } |
107 else if (type == "VsolStuff.Message2") | 95 {% enfor %} |
108 { | |
109 let value = jsonValue["value"] as Message2; | |
110 return dispatcher.HandleMessage2(value); | |
111 } | |
112 else | 96 else |
113 { | 97 { |
114 return false; | 98 return false; |
115 } | 99 } |
116 } | 100 } |