Mercurial > hg > orthanc-stone
comparison Resources/CodeGeneration/template.in.ts.j2 @ 520:7a16fb9a4ba5 am-touch-events
merge bgo-commands-codegen
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Tue, 12 Mar 2019 14:50:14 +0100 |
parents | 17106b29ed6d |
children | 84af39146e76 |
comparison
equal
deleted
inserted
replaced
503:77e0eb83ff63 | 520:7a16fb9a4ba5 |
---|---|
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])}}; | |
76 {% endfor %}{% endif %}{% endif %} | |
77 constructor() { | |
78 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%}{% if NeedsTsConstruction(enums,CanonToTs(struct['fields'][key])) %} this.{{key}} = new {{CanonToTs(struct['fields'][key])}}(); | |
79 {% endif %}{% endfor %}{% endif %}{% endif %} } | |
80 | |
81 public StoneSerialize(): string { | |
82 let container: object = {}; | |
83 container['type'] = '{{rootName}}.{{struct['name']}}'; | |
84 container['value'] = this; | |
85 return JSON.stringify(container); | |
86 } | |
87 | |
88 public static StoneDeserialize(valueStr: string) : {{struct['name']}} | |
89 { | |
90 let value: any = JSON.parse(valueStr); | |
91 StoneCheckSerializedValueType(value, '{{rootName}}.{{struct['name']}}'); | |
92 let result: {{struct['name']}} = value['value'] as {{struct['name']}}; | |
93 return result; | |
94 } | |
95 } | |
96 {% endfor %} | |
97 export interface IHandler { | |
98 {% for struct in structs%}{% if struct['__meta__'].handleInTypescript %} Handle{{struct['name']}}(value: {{struct['name']}}): boolean; | |
99 {% endif %}{% endfor %}}; | |
100 | |
101 /** Service function for StoneDispatchToHandler */ | |
102 export function StoneDispatchJsonToHandler( | |
103 jsonValue: any, handler: IHandler): boolean | |
104 { | |
105 StoneCheckSerializedValueTypeGeneric(jsonValue); | |
106 let type: string = jsonValue["type"]; | |
107 if (type == "") | |
108 { | |
109 // this should never ever happen | |
110 throw new Error("Caught empty type while dispatching"); | |
111 } | |
112 {% for struct in structs%}{% if struct['__meta__'].handleInTypescript %} else if (type == "{{rootName}}.{{struct['name']}}") | |
113 { | |
114 let value = jsonValue["value"] as {{struct['name']}}; | |
115 return handler.Handle{{struct['name']}}(value); | |
116 } | |
117 {% endif %}{% endfor %} else | |
118 { | |
119 return false; | |
120 } | |
121 } | |
122 | |
123 /** Takes a serialized type and passes this to the handler */ | |
124 export function StoneDispatchToHandler( | |
125 strValue: string, handler: IHandler): boolean | |
126 { | |
127 // console.//log("+------------------------------------------------+"); | |
128 // console.//log("| StoneDispatchToHandler |"); | |
129 // console.//log("+------------------------------------------------+"); | |
130 // console.//log("strValue = "); | |
131 // console.//log(strValue); | |
132 let jsonValue: any = JSON.parse(strValue) | |
133 return StoneDispatchJsonToHandler(jsonValue, handler); | |
134 } |