All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dart2-v3template.class.mustache Maven / Gradle / Ivy

There is a newer version: 8.1
Show newest version
// {{name}}
class {{classname}}{{#parent}} extends {{parent}}{{/parent}} {
{{#vars}}
  {{^vendorExtensions.x-dart-inherited}}
    {{#description}}/* {{{description}}} */{{/description}}
    {{^allowableValues}}
      {{{dataType}}} {{name}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}};
    {{/allowableValues}}
    {{#allowableValues}}
      {{^complexType}}
        {{#min}} // range from {{min}} to {{max}}
          {{{dataType}}} {{name}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}};
        {{/min}}
        {{classname}}{{nameInCamelCase}}Enum {{name}}{{#defaultValue}} = {{classname}}{{nameInCamelCase}}Enum.{{{defaultValue}}}{{/defaultValue}};
      {{/complexType}}
      {{#complexType}}
        {{{dataType}}} {{name}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}};
      {{/complexType}}
    {{/allowableValues}}
  {{/vendorExtensions.x-dart-inherited}}
{{/vars}}
{{^hasVars}}
  dynamic json;
{{/hasVars}}
{{classname}}();

  @override
  String toString() {
    return '{{classname}}[{{#vars}}{{name}}=${{name}}, {{/vars}}{{^hasVars}}json=$json{{/hasVars}}]';
  }

{{^hasVars}}
  {{classname}}.fromJson(dynamic rawValue) {
    this.json = rawValue;
  }

  dynamic toJson() {
    return json;
  }
{{/hasVars}}
{{#hasVars}}
  fromJson(Map json) {
    if (json == null) return;
  {{#parent}}super.fromJson(json);{{/parent}}
  {{#vars}}
    {{^vendorExtensions.x-dart-inherited}}
    {
      final _jsonData = json[r'{{{baseName}}}'];
      {{name}} = (_jsonData == null) ? null :
  {{#allowableValues}}
    {{#complexType}}
    {{#isListContainer}}
        {{complexType}}TypeTransformer.listFromJson(_jsonData);
    {{/isListContainer}}
    {{^isListContainer}}
      {{#isMapContainer}}
        {{complexType}}TypeTransformer.mapFromJson(_jsonData);
      {{/isMapContainer}}
      {{^isMapContainer}}
        {{complexType}}TypeTransformer.fromJson(_jsonData);
      {{/isMapContainer}}
    {{/isListContainer}}
    {{/complexType}}

  {{/allowableValues}}
  {{^allowableValues}}
  {{#isDateTime}}
        DateTime.parse(_jsonData);
  {{/isDateTime}}
  {{#isDate}}
        DateTime.parse(_jsonData + 'T00:00:00.000Z');
  {{/isDate}}
  {{^isDateTime}}
  {{^isDate}}
    {{#complexType}}
      {{#isListContainer}}
        {{#items}}
          {{#vendorExtensions.x-dart-dynamic}}
            (_jsonData as List)?.cast<{{{dataType}}}>();
          {{/vendorExtensions.x-dart-dynamic}}
          {{^vendorExtensions.x-dart-dynamic}}
          {{#complexType}}
              {{#allowableValues}}
          {{complexType}}TypeTransformer.listFromJson(_jsonData);
            {{/allowableValues}}
            {{^allowableValues}}
          {{#vendorExtensions.x-dart-datetime}}
            openApiDateTimeList(_jsonData);
          {{/vendorExtensions.x-dart-datetime}}
          {{^vendorExtensions.x-dart-datetime}}
            {{complexType}}.listFromJson(_jsonData);
          {{/vendorExtensions.x-dart-datetime}}
              {{/allowableValues}}
          {{/complexType}}
          {{/vendorExtensions.x-dart-dynamic}}
          {{^complexType}}
        (_jsonData as List)?.cast<{{{dataType}}}>();
          {{/complexType}}
        {{/items}}
      {{/isListContainer}}
      {{^isListContainer}}
      {{#isMapContainer}}
        {{#items}}
          {{#complexType}}
            {{#isListContainer}}
        (_jsonData as Map).cast().map((String key, dynamic value) => MapEntry(key, {{#items}}
          {{^vendorExtensions.x-dart-datetime}}
            {{#vendorExtensions.x-dart-dynamic}}value as List{{/vendorExtensions.x-dart-dynamic}}
            {{^vendorExtensions.x-dart-dynamic}}
              {{#vendorExtensions.x-dart-isenum}}
                {{{complexType}}}TypeTransformer.listFromJson(value)
              {{/vendorExtensions.x-dart-isenum}}
              {{^vendorExtensions.x-dart-isenum}}
                {{{complexType}}}.listFromJson(value)
              {{/vendorExtensions.x-dart-isenum}}
              {{/vendorExtensions.x-dart-dynamic}}
          {{/vendorExtensions.x-dart-datetime}}
          {{#vendorExtensions.x-dart-datetime}}openApiDateTimeList(value)
          {{/vendorExtensions.x-dart-datetime}}
        {{/items}}));
            {{/isListContainer}}
            {{^isListContainer}}
              {{#isMapContainer}}
        null; // too complex?
              {{/isMapContainer}}
              {{^isMapContainer}}
        (_jsonData as Map).cast().map((String key, dynamic value) => MapEntry(key,
        {{^vendorExtensions.x-dart-datetime}}
          {{#vendorExtensions.x-dart-dynamic}}value{{/vendorExtensions.x-dart-dynamic}}
          {{^vendorExtensions.x-dart-dynamic}}{{{complexType}}}.fromJson(value){{/vendorExtensions.x-dart-dynamic}}
        {{/vendorExtensions.x-dart-datetime}}
        {{#vendorExtensions.x-dart-datetime}}openApiDateTimeFromJson(value)
        {{/vendorExtensions.x-dart-datetime}}));
              {{/isMapContainer}}
            {{/isListContainer}}
          {{/complexType}}
          {{^complexType}}
            {{#isListContainer}}
        (_jsonData as Map).cast().map((String key, dynamic value) => MapEntry(key,
        {{#vendorExtensions.x-dart-dynamic}}value as List{{/vendorExtensions.x-dart-dynamic}}
        {{^vendorExtensions.x-dart-dynamic}}(value as List)?.cast<{{{items.dataType}}}>(){{/vendorExtensions.x-dart-dynamic}}));
            {{/isListContainer}}
            {{^isListContainer}}
              {{#isMapContainer}}
        null; // map of maps?
              {{/isMapContainer}}
              {{^isMapContainer}}
        (_jsonData as Map).cast(); // h2
              {{/isMapContainer}}
            {{/isListContainer}}
          {{/complexType}}
        {{/items}}
        {{^items}}
        {{complexType}}.mapFromJson(_jsonData);
        {{/items}}
      {{/isMapContainer}}
      {{^isMapContainer}}
        {{#vendorExtensions.x-dart-dynamic}}_jsonData;{{/vendorExtensions.x-dart-dynamic}}
        {{^vendorExtensions.x-dart-dynamic}}{{complexType}}.fromJson(_jsonData);{{/vendorExtensions.x-dart-dynamic}}
      {{/isMapContainer}}
      {{/isListContainer}}
    {{/complexType}}
    {{^complexType}}
      {{#isListContainer}}
        {{#items}}
        (_jsonData as List)?.cast<{{datatype}}>();
        {{/items}}
      {{/isListContainer}}
      {{^isListContainer}}
        {{#isMapContainer}}
        (_jsonData as Map)?.cast();
        {{/isMapContainer}}
        {{^isMapContainer}}
        _jsonData;
        {{/isMapContainer}}
      {{/isListContainer}}
    {{/complexType}}
  {{/isDate}}
  {{/isDateTime}}
  {{/allowableValues}}
  {{^vendorExtensions.x-dart-inherited}}
    {{^complexType}}
      {{#allowableValues}}
        {{^min}}
          {{classname}}{{nameInCamelCase}}EnumExtension.fromJson(_jsonData);
        {{/min}}
      {{/allowableValues}}
    {{/complexType}}
  {{/vendorExtensions.x-dart-inherited}}
    } // _jsonFieldName
{{/vendorExtensions.x-dart-inherited}}{{/vars}}
  }

  {{classname}}.fromJson(Map json) {
    fromJson(json); // allows child classes to call
  }

  Map toJson() {

    final json = {};
    {{#vars}}
      {{^vendorExtensions.x-dart-inherited}}
        {{^isNullable}}
    if ({{name}} != null) {
        {{/isNullable}}
        {{#isDateTime}}
      json[r'{{baseName}}'] = {{name}}.toUtc().toIso8601String();
      {{/isDateTime}}
      {{#isDate}}
      json[r'{{baseName}}'] = {{name}}.toUtc().toIso8601String();
      {{/isDate}}
      {{^isDateTime}}
      {{^isDate}}
        {{^complexType}}
          {{#allowableValues}}
            {{^min}}
              json[r'{{baseName}}'] = LocalApiClient.serialize({{name}}.toJson());
            {{/min}}
            {{#min}}
              json[r'{{baseName}}'] = LocalApiClient.serialize({{name}});
            {{/min}}
          {{/allowableValues}}
          {{^allowableValues}}
            json[r'{{baseName}}'] = LocalApiClient.serialize({{name}});
          {{/allowableValues}}
        {{/complexType}}
        {{#complexType}}
          json[r'{{baseName}}'] = LocalApiClient.serialize({{name}});
        {{/complexType}}
      {{/isDate}}
      {{/isDateTime}}
        {{^isNullable}}
    }
        {{/isNullable}}
      {{/vendorExtensions.x-dart-inherited}}
    {{/vars}}
    return json;
  }
{{/hasVars}}
  static List<{{classname}}> listFromJson(List json) {
    return json == null ? <{{classname}}>[] : json.map((value) => {{classname}}.fromJson(value)).toList();
  }

  static Map mapFromJson(Map json) {
    final map = {};
    if (json != null && json.isNotEmpty) {
      json.forEach((String key, dynamic value) => map[key] = {{classname}}.fromJson(value));
    }
    return map;
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) {
      return true;
    }

    if (other is {{classname}} && runtimeType == other.runtimeType) {
  {{#hasVars}}
    return {{^hasVars}}json?.equals(other.json);{{/hasVars}}{{#vars}}{{^vendorExtensions.x-dart-inherited}}
    {{#complexType}}
      {{#isListContainer}}
        const ListEquality().equals({{name}}, other.{{name}}){{#hasMore}} &&
      {{/hasMore}}
      {{/isListContainer}}
      {{^isListContainer}}
        {{#isMapContainer}}
          const MapEquality().equals({{name}}, other.{{name}}){{#hasMore}} &&
        {{/hasMore}}
        {{/isMapContainer}}
        {{^isMapContainer}}
          {{name}} == other.{{name}}{{#hasMore}} && // other
        {{/hasMore}}
        {{/isMapContainer}}
      {{/isListContainer}}
    {{/complexType}}{{^complexType}}
    {{#isListContainer}}
      const ListEquality().equals({{name}}, other.{{name}}){{#hasMore}} &&
    {{/hasMore}}
    {{/isListContainer}}
    {{^isListContainer}}
     {{name}} == other.{{name}}{{#hasMore}} &&
    {{/hasMore}}
    {{/isListContainer}}
  {{/complexType}}{{/vendorExtensions.x-dart-inherited}}{{/vars}}
    {{#parent}} &&
    super==other{{/parent}};{{/hasVars}}{{^hasVars}}
    return {{#parent}}super==other{{/parent}}{{^parent}}true{{/parent}};{{/hasVars}}
    }

    return false;
  }

  @override
  int get hashCode {
    var hashCode = runtimeType.hashCode;

  {{#hasVars}}
    {{#vars}}{{^vendorExtensions.x-dart-inherited}}
    {{#complexType}}
      {{#isListContainer}}
        hashCode = hashCode ^ const ListEquality().hash({{name}});
      {{/isListContainer}}
      {{^isListContainer}}
        {{#isMapContainer}}
            hashCode = hashCode ^ const MapEquality().hash({{name}});
        {{/isMapContainer}}
        {{^isMapContainer}}
            if ({{name}} != null) {
              hashCode = hashCode ^ {{name}}.hashCode;
            }
        {{/isMapContainer}}
      {{/isListContainer}}
    {{/complexType}}{{^complexType}}
    if ({{name}} != null) {
      {{#isListContainer}}
        hashCode = hashCode ^ const ListEquality().hash({{name}});
      {{/isListContainer}}
      {{^isListContainer}}
      hashCode = hashCode ^ {{name}}.hashCode;
      {{/isListContainer}}
    }
{{/complexType}}{{/vendorExtensions.x-dart-inherited}}{{/vars}}{{/hasVars}}{{^hasVars}}if (json != null) { hashCode = hashCode ^ json.hashCode; }{{/hasVars}}

    return hashCode{{#parent}} ^ super.hashCode{{/parent}};
  }

  {{classname}} copyWith({{#hasVars}}{
     {{#vars}}
       {{^vendorExtensions.x-dart-inherited}}
         {{^complexType}}
           {{#allowableValues}}
             {{^min}}
               {{classname}}{{nameInCamelCase}}Enum {{name}},
             {{/min}}
             {{#min}}
               {{{dataType}}} {{name}},
             {{/min}}
           {{/allowableValues}}
           {{^allowableValues}}
             {{{dataType}}} {{name}},
           {{/allowableValues}}
         {{/complexType}}
         {{#complexType}}
           {{{dataType}}} {{name}},
         {{/complexType}}
       {{/vendorExtensions.x-dart-inherited}}
       {{#vendorExtensions.x-dart-inherited}}
         {{{dataType}}} {{name}},
       {{/vendorExtensions.x-dart-inherited}}
     {{/vars}}
    }{{/hasVars}}) {
    {{classname}} copy = {{classname}}();
  {{#vars}}
    {{#isListContainer}}
      {{^complexType}}
        copy.{{name}} = {{name}} ?? []..addAll(this.{{name}});
      {{/complexType}}
      {{#complexType}}
        {
        var newVal;
        final v = {{name}} ?? this.{{name}};
        {{#items}}
          newVal = <{{^vendorExtensions.x-dart-dynamic}}{{{complexType}}}{{/vendorExtensions.x-dart-dynamic}}{{#vendorExtensions.x-dart-dynamic}}dynamic{{/vendorExtensions.x-dart-dynamic}}>{{>list-copy-with}};
        {{/items}}
        copy.{{name}} = newVal;
        }
      {{/complexType}}
    {{/isListContainer}}
    {{#isMapContainer}}
      {{#isPrimitiveType}}
        copy.{{name}} = {{name}} ?? (this.{{name}} == null ? {} : {}..addAll(this.{{name}}));
      {{/isPrimitiveType}}
      {{^isPrimitiveType}}
        {{#isContainer}}
          {
          var newVal;
          final fld = {{name}} ?? this.{{name}};
          {{#items}}
            {{#enumName}}
              newVal = fld?.map((k, v) => MapEntry(k, v));
            {{/enumName}}
            {{^enumName}}
              newVal = fld?.map((k, v) => MapEntry(k, {{>copy-with}} )); // e1
            {{/enumName}}
          {{/items}}
        {{/isContainer}}
        {{^isContainer}}
          newVal = fld?.map((k, v) => MapEntry(k, {{>copy-with}}));  // e2
        {{/isContainer}}
        copy.{{name}} = newVal;
        }
      {{/isPrimitiveType}}
    {{/isMapContainer}}
    {{^isContainer}}
      {{#isModel}}
        copy.{{name}} = {{name}} ?? this.{{name}}?.copyWith();
      {{/isModel}}
      {{^isModel}}
        copy.{{name}} = {{name}} ?? this.{{name}};
      {{/isModel}}
    {{/isContainer}}
  {{/vars}}
    return copy;
  }
}

  {{#vars}}
  {{^vendorExtensions.x-dart-inherited}}
    {{^complexType}}
      {{^min}}
        {{#allowableValues}}
           // Inline used enum: {{classname}} {{name}}
          enum {{classname}}{{nameInCamelCase}}Enum { {{#enumVars}}{{{name}}}{{^-last}}, {{/-last}}{{/enumVars}} }

          extension {{classname}}{{nameInCamelCase}}EnumExtension on {{classname}}{{nameInCamelCase}}Enum {

            static Map fromMap = {
            {{#allowableValues}}
              {{#enumVars}}{{{value}}}:{{classname}}{{{nameInCamelCase}}}Enum.{{{name}}}{{^-last}}, {{/-last}}{{/enumVars}}
            {{/allowableValues}} };
            static Map<{{classname}}{{nameInCamelCase}}Enum, String> toMap = {
            {{#allowableValues}}
              {{#enumVars}}{{classname}}{{{nameInCamelCase}}}Enum.{{{name}}}:{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}
            {{/allowableValues}} };

            String toJson() => toMap[this];
            static {{classname}}{{nameInCamelCase}}Enum fromJson(String key) => fromMap[key];
          }
        {{/allowableValues}}
      {{/min}}
    {{/complexType}}
  {{/vendorExtensions.x-dart-inherited}}
{{/vars}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy