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}}
      {{#isEnum}}
        {{{datatypeWithEnum}}} {{{name}}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}};
      {{/isEnum}}
      {{^isEnum}}
        {{^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}}
      {{/isEnum}}
    {{/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}}
    {{#isArray}}{{>_complex_from_json}}{{/isArray}}{{#isMap}}{{>_complex_from_json}}{{/isMap}}{{^items}}{{{name}}} = (json[r'{{{baseName}}}'] == null) ? null : {{>_simple_from_json}};{{/items}}
    {{/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}}
      {{#isArray}}
        json[r'{{baseName}}'] = {{{name}}}.map((v) => LocalApiClient.serialize(v)).toList();
      {{/isArray}}
      {{#isMap}}
        json[r'{{baseName}}'] = Map.fromIterables({{{name}}}.keys,
          {{{name}}}.values.map((v) => LocalApiClient.serialize(v)));
      {{/isMap}}
      {{^items}}
        {{#isDateTime}}
          json[r'{{baseName}}'] = {{{name}}}.toUtc().toIso8601String();
        {{/isDateTime}}
        {{#isDate}}
          json[r'{{baseName}}'] = {{{name}}}.toDateString();
        {{/isDate}}
        {{#isEnum}}
          json[r'{{baseName}}'] = {{{name}}}.toJson();
        {{/isEnum}}
        {{#isModel}}
          json[r'{{baseName}}'] = {{{name}}}.toJson();
        {{/isModel}}
        {{#isPrimitiveType}}
          json[r'{{baseName}}'] = {{{name}}};
        {{/isPrimitiveType}}
      {{/items}}
      {{^isNullable}}
    } {{#generateNullValuesToJson}}else {
    json[r'{{baseName}}'] = null;
    }{{/generateNullValuesToJson}}
        {{/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}}
      {{#isArray}}
        const ListEquality().equals({{{name}}}, other.{{{name}}}){{^-last}} &&
      {{/-last}}
      {{/isArray}}
      {{^isArray}}
        {{#isMap}}
          const MapEquality().equals({{{name}}}, other.{{{name}}}){{^-last}} &&
        {{/-last}}
        {{/isMap}}
        {{^isMap}}
          {{{name}}} == other.{{{name}}}{{^-last}} && // other
        {{/-last}}
        {{/isMap}}
      {{/isArray}}
    {{/complexType}}{{^complexType}}
    {{#isArray}}
      const ListEquality().equals({{{name}}}, other.{{{name}}}){{^-last}} &&
    {{/-last}}
    {{/isArray}}
    {{^isArray}}
     {{{name}}} == other.{{{name}}}{{^-last}} &&
    {{/-last}}
    {{/isArray}}
  {{/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}}
      if ({{{name}}} != null) {
      {{#isArray}}
        hashCode = hashCode * 31 + const ListEquality().hash({{{name}}});
      {{/isArray}}
      {{#isMap}}
        hashCode = hashCode * 31 + MapEquality().hash({{{name}}});
      {{/isMap}}
      {{^items}}
        hashCode = hashCode * 31 + {{{name}}}.hashCode;
      {{/items}}
    }
{{/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}}
         {{{dataType}}} {{{name}}},
     {{/vars}}
    }{{/hasVars}}) {
    {{classname}} copy = {{classname}}();
  {{#vars}}{{^vendorExtensions.x-dart-inherited}}{{{name}}} ??= this.{{{name}}};
  {{/vendorExtensions.x-dart-inherited}}{{/vars}}
  {{#vars}}
    {{^vendorExtensions.x-dart-inherited}}
      {{#isArray}}{{>_complex_copy}}{{/isArray}}{{#isMap}}{{>_complex_copy}}{{/isMap}}{{^items}}copy.{{{name}}} = {{>_simple_copy}};{{/items}}
    {{/vendorExtensions.x-dart-inherited}}{{/vars}}
    return copy;
  }
}

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

          extension {{{enumName}}}Extension on {{{enumName}}} {

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

            String toJson() => toMap[this];
            static {{{enumName}}} fromJson(String key) => key == null ? null : fromMap[key];

            static List<{{{enumName}}}> listFromJson(List json) =>
              json == null ? <{{{enumName}}}>[] : json.map((value) => fromJson(value)).toList();

            static {{{enumName}}} copyWith({{{enumName}}} instance) => instance;
          }
        {{/allowableValues}}
      {{/min}}
    {{/complexType}}
  {{/vendorExtensions.x-dart-inherited}}
{{/vars}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy