diff 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
line wrap: on
line diff
--- a/Resources/CodeGeneration/template.in.ts	Fri Feb 22 10:48:43 2019 +0100
+++ b/Resources/CodeGeneration/template.in.ts	Sat Feb 23 10:18:13 2019 +0100
@@ -3,107 +3,106 @@
 12345678901234567890123456789012345678901234567890123456789012345678901234567890
 */
 
-namespace {{rootName}}
+function StoneCheckSerializedValueType(value: any, typeStr: string)
 {
-  function StoneCheckSerializedValueType(value: any, typeStr: string)
-  {
-    StoneCheckSerializedValueTypeGeneric(value);
+  StoneCheckSerializedValueTypeGeneric(value);
 
-    if (value['type'] != typeStr)
-    {
-      throw new Error(
-        `Cannot deserialize type ${value['type']} into ${typeStr}`);
-    }
+  if (value['type'] != typeStr)
+  {
+    throw new Error(
+      `Cannot deserialize type ${value['type']} into ${typeStr}`);
   }
+}
 
-  function isString(val: any) :boolean
-  {
-    return ((typeof val === 'string') || (val instanceof String));
-  }
-  
-  function StoneCheckSerializedValueTypeGeneric(value: any)
+function isString(val: any) :boolean
+{
+  return ((typeof val === 'string') || (val instanceof String));
+}
+
+function StoneCheckSerializedValueTypeGeneric(value: any)
+{
+  // console.//log("+-------------------------------------------------+");
+  // console.//log("|            StoneCheckSerializedValueTypeGeneric |");
+  // console.//log("+-------------------------------------------------+");
+  // console.//log("value = ");
+  // console.//log(value);
+  if ( (!('type' in value)) || (!isString(value.type)) )
   {
-    if ( (!('type' in value)) || (!isString(value)) )
-    {
-      throw new Error(
-        "Cannot deserialize value ('type' key invalid)");
-    }
+    throw new Error(
+      "Cannot deserialize value ('type' key invalid)");
   }
+}
 
 // end of generic methods
 {% for enum in enums%}
-  export enum {{enum['name']}} {
-    {% for key in enumDict.keys()%}
-    {{key}},
-    {%endfor%}
-  };
+export enum {{enum['name']}} {
+  {% for key in enum['fields']%}{{key}},
+  {%endfor%}
+};
 {%endfor%}
 
-
-"""  // end of generic methods
 {% for struct in structs%}  export class {{struct['name']}} {
 {% for key in struct['fields']%}    {{key}}:{{CanonToTs(struct['fields'][key])}};
 {% endfor %}
-    constructor() {
-{% for key in struct['fields']%}      {{key}} = new {{CanonToTs(struct['fields'][key])}}();
-{% endfor %}    }
+  constructor() {
+{% for key in struct['fields']%}{% if NeedsTsConstruction(enums,CanonToTs(struct['fields'][key])) %}      this.{{key}} = new {{CanonToTs(struct['fields'][key])}}();
+{% endif %}{% endfor %}    }
 
-    public StoneSerialize(): string {
-      let container: object = {};
-      container['type'] = '{{rWholootName}}.{{struct['name']}}';
-      container['value'] = this;
-      return JSON.stringify(container);
-    }
+  public StoneSerialize(): string {
+    let container: object = {};
+    container['type'] = '{{rWholootName}}.{{struct['name']}}';
+    container['value'] = this;
+    return JSON.stringify(container);
+  }
 
-    public static StoneDeserialize(valueStr: string) : {{struct['name']}}
-    {
-      let value: any = JSON.parse(valueStr);
-      StoneCheckSerializedValueType(value, '{{rootName}}.{{struct['name']}}');
-      let result: {{struct['name']}} = value['value'] as {{struct['name']}};
-      return result;
-    }
-
+  public static StoneDeserialize(valueStr: string) : {{struct['name']}}
+  {
+    let value: any = JSON.parse(valueStr);
+    StoneCheckSerializedValueType(value, '{{rootName}}.{{struct['name']}}');
+    let result: {{struct['name']}} = value['value'] as {{struct['name']}};
+    return result;
   }
+}
 
 {% endfor %}
 
-    };
+export interface IDispatcher {
+  {% for struct in structs%}    Handle{{struct['name']}}(value:  {{struct['name']}}): boolean;
+  {% endfor %}
+};
 
-  export interface IDispatcher
-  {
-    {% for struct in structs%}    HandleMessage1(value:  {{struct['name']}}): boolean;
-    {% endfor %}
-  };
-
-  /** Service function for StoneDispatchToHandler */
-  export function StoneDispatchJsonToHandler(
-    jsonValueStr: string, dispatcher: IDispatcher): boolean
+/** Service function for StoneDispatchToHandler */
+export function StoneDispatchJsonToHandler(
+  jsonValue: any, dispatcher: IDispatcher): boolean
+{
+  StoneCheckSerializedValueTypeGeneric(jsonValue);
+  let type: string = jsonValue["type"];
+  if (type == "")
   {
-    let jsonValue: any = JSON.parse(jsonValueStr);
-    StoneCheckSerializedValueTypeGeneric(jsonValue);
-    let type: string = jsonValue["type"];
-    if (type == "")
-    {
-      // this should never ever happen
-      throw new Error("Caught empty type while dispatching");
-    }
-{% for struct in structs%}    else if (type == "VsolStuff.{{struct['name']}}")
-    {
-      let value = jsonValue["value"] as Message1;
-      return dispatcher.HandleMessage1(value);
-    }
-{% enfor %}
-    else
-    {
-      return false;
-    }
+    // this should never ever happen
+    throw new Error("Caught empty type while dispatching");
   }
-
-  /** Takes a serialized type and passes this to the dispatcher */
-  export function StoneDispatchToHandler(
-    strValue: string, dispatcher: IDispatcher): boolean
+{% for struct in structs%}    else if (type == "{{rootName}}.{{struct['name']}}")
   {
-    let jsonValue: any = JSON.parse(strValue)
-    return StoneDispatchJsonToHandler(jsonValue, dispatcher);
+    let value = jsonValue["value"] as {{struct['name']}};
+    return dispatcher.Handle{{struct['name']}}(value);
+  }
+{% endfor %}
+  else
+  {
+    return false;
   }
 }
+
+/** Takes a serialized type and passes this to the dispatcher */
+export function StoneDispatchToHandler(
+  strValue: string, dispatcher: IDispatcher): boolean
+{
+  // console.//log("+------------------------------------------------+");
+  // console.//log("|            StoneDispatchToHandler              |");
+  // console.//log("+------------------------------------------------+");
+  // console.//log("strValue = ");
+  // console.//log(strValue);
+  let jsonValue: any = JSON.parse(strValue)
+  return StoneDispatchJsonToHandler(jsonValue, dispatcher);
+}