view 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
line wrap: on
line source

/*
         1         2         3         4         5         6         7
12345678901234567890123456789012345678901234567890123456789012345678901234567890
*/

namespace {{rootName}}
{
  function StoneCheckSerializedValueType(value: any, typeStr: string)
  {
    StoneCheckSerializedValueTypeGeneric(value);

    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)
  {
    if ( (!('type' in value)) || (!isString(value)) )
    {
      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%}
  };
{%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 %}    }

    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;
    }

  }

{% 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
  {
    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;
    }
  }

  /** Takes a serialized type and passes this to the dispatcher */
  export function StoneDispatchToHandler(
    strValue: string, dispatcher: IDispatcher): boolean
  {
    let jsonValue: any = JSON.parse(strValue)
    return StoneDispatchJsonToHandler(jsonValue, dispatcher);
  }
}