view Resources/CodeGeneration/template.in.ts.j2 @ 1327:4f8db2d202c8 broker

OrthancSeriesProgressiveLoader now has two modes that can be selected at object creation : - progressive (will first load jpeg50, then jpeg90 then PAM) - non-progressive (will directly load PAM (uncompressed)) Please note that the slice loading order remains dynamic and depending upon the slice that the client code wishes to extract from the volume.
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 25 Mar 2020 14:34:27 +0100
parents 84af39146e76
children
line wrap: on
line source

/*
         1         2         3         4         5         6         7
12345678901234567890123456789012345678901234567890123456789012345678901234567890

Generated on {{currentDatetime}} by stonegentool

*/

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)
{
  // console.//log("+-------------------------------------------------+");
  // console.//log("|            StoneCheckSerializedValueTypeGeneric |");
  // console.//log("+-------------------------------------------------+");
  // console.//log("value = ");
  // console.//log(value);
  if ( (!('type' in value)) || (!isString(value.type)) )
  {
    throw new Error(
      "Cannot deserialize value ('type' key invalid)");
  }
}

// end of generic methods
{% for enum in enums%}
export enum {{enum['name']}} {
{% for key in enum['fields']%}  {{key}} = "{{key}}"{% if not loop.last %},{%endif%}
{%endfor%}};

export function {{enum['name']}}_FromString(strValue:string) : {{enum['name']}}
{
{% for key in enum['fields'] %}  if( strValue == "{{key}}" )
  {
    return {{enum['name']}}.{{key}};
  }
{%endfor%}
  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 %}`;
  throw new Error(msg);
}

export function {{enum['name']}}_ToString(value:{{enum['name']}}) : string
{
{% for key in enum['fields'] %}  if( value == {{enum['name']}}.{{key}} )
  {
    return "{{key}}";
  }
{%endfor%}
  let msg : string = `Value ${value} cannot be converted to {{enum['name']}}. Possible values are: `;
{% for key in enum['fields']%}  {
    let _{{key}}_enumValue : string = {{enum['name']}}.{{key}}; // enums are strings in stonecodegen, so this will work.
    let msg_{{key}} : string = `{{key}} (${_{{key}}_enumValue}){% if not loop.last %}, {%endif%}`;
    msg = msg + msg_{{key}};
  }
{%endfor%}  throw new Error(msg);
}
{%endfor%}


{% for struct in structs%}export class {{struct['name']}} {
{% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%}  {{key}}:{{CanonToTs(struct['fields'][key]['type'])}};
{% endfor %}{% endif %}{% endif %}
  constructor() {
{% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%}{% if NeedsTsConstruction(enums,CanonToTs(struct['fields'][key]['type'])) %}    this.{{key}} = new {{CanonToTs(struct['fields'][key]['type'])}}();
{% endif %}
{% if struct['fields'][key]['defaultValue'] %}    this.{{key}} = {{DefaultValueToTs(enums,struct['fields'][key])}};
{% endif %}{% endfor %}{% endif %}{% endif %}  }

  public StoneSerialize(): string {
    let container: object = {};
    container['type'] = '{{rootName}}.{{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 IHandler {
{% for struct in structs%}{% if struct['__meta__'].handleInTypescript %}  Handle{{struct['name']}}(value:  {{struct['name']}}): boolean;
{% endif %}{% endfor %}};

/** Service function for StoneDispatchToHandler */
export function StoneDispatchJsonToHandler(
  jsonValue: any, handler: IHandler): boolean
{
  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%}{% if struct['__meta__'].handleInTypescript %}  else if (type == "{{rootName}}.{{struct['name']}}")
  {
    let value = jsonValue["value"] as {{struct['name']}};
    return handler.Handle{{struct['name']}}(value);
  }
{% endif %}{% endfor %}  else
  {
    return false;
  }
}

/** Takes a serialized type and passes this to the handler */
export function StoneDispatchToHandler(
  strValue: string, handler: IHandler): boolean
{
  // console.//log("+------------------------------------------------+");
  // console.//log("|            StoneDispatchToHandler              |");
  // console.//log("+------------------------------------------------+");
  // console.//log("strValue = ");
  // console.//log(strValue);
  let jsonValue: any = JSON.parse(strValue)
  return StoneDispatchJsonToHandler(jsonValue, handler);
}