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

dart2.class.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
class {{{classname}}} {
  /// Returns a new [{{{classname}}}] instance.
  {{{classname}}}({
  {{#vars}}
    {{#required}}{{^defaultValue}}@required {{/defaultValue}}{{/required}}this.{{{name}}}{{^isNullable}}{{#defaultValue}} = {{#isEnum}}{{^isContainer}}const {{{classname}}}{{{enumName}}}._({{/isContainer}}{{/isEnum}}{{{defaultValue}}}{{#isEnum}}{{^isContainer}}){{/isContainer}}{{/isEnum}}{{/defaultValue}}{{/isNullable}},
  {{/vars}}
  });

  {{#vars}}
  {{#description}}
  /// {{{description}}}
  {{/description}}
  {{^isEnum}}
    {{#minimum}}
  // minimum: {{{minimum}}}
    {{/minimum}}
    {{#maximum}}
  // maximum: {{{maximum}}}
    {{/maximum}}
  {{/isEnum}}
  {{{datatypeWithEnum}}} {{{name}}};

  {{/vars}}
  @override
  bool operator ==(Object other) => identical(this, other) || other is {{{classname}}} &&
  {{#vars}}
     other.{{{name}}} == {{{name}}}{{^-last}} &&{{/-last}}{{#-last}};{{/-last}}
  {{/vars}}

  @override
  int get hashCode =>
  {{#vars}}
    ({{{name}}} == null ? 0 : {{{name}}}.hashCode){{^-last}} +{{/-last}}{{#-last}};{{/-last}}
  {{/vars}}

  @override
  String toString() => '{{{classname}}}[{{#vars}}{{{name}}}=${{{name}}}{{^-last}}, {{/-last}}{{/vars}}]';

  Map toJson() {
    final json = {};
  {{#vars}}
    if ({{{name}}} != null) {
    {{#isDateTime}}
      {{#pattern}}
      json[r'{{{baseName}}}'] = _dateEpochMarker == '{{{pattern}}}'
        ? {{{name}}}.millisecondsSinceEpoch
        : {{{name}}}.toUtc().toIso8601String();
      {{/pattern}}
      {{^pattern}}
      json[r'{{{baseName}}}'] = {{{name}}}.toUtc().toIso8601String();
      {{/pattern}}
    {{/isDateTime}}
    {{#isDate}}
      {{#pattern}}
      json[r'{{{baseName}}}'] = _dateEpochMarker == '{{{pattern}}}'
        ? {{{name}}}.millisecondsSinceEpoch
        : _dateFormatter.format({{{name}}}.toUtc());
      {{/pattern}}
      {{^pattern}}
      json[r'{{{baseName}}}'] = _dateFormatter.format({{{name}}}.toUtc());
      {{/pattern}}
    {{/isDate}}
    {{^isDateTime}}
      {{^isDate}}
      json[r'{{{baseName}}}'] = {{{name}}};
      {{/isDate}}
    {{/isDateTime}}
    }
  {{/vars}}
    return json;
  }

  /// Returns a new [{{{classname}}}] instance and imports its values from
  /// [json] if it's non-null, null if [json] is null.
  static {{{classname}}} fromJson(Map json) => json == null
    ? null
    : {{{classname}}}(
  {{#vars}}
  {{#isDateTime}}
        {{{name}}}: json[r'{{{baseName}}}'] == null
          ? null
    {{#pattern}}
          : _dateEpochMarker == '{{{pattern}}}'
            ? DateTime.fromMillisecondsSinceEpoch(json[r'{{{baseName}}}'] as int, isUtc: true)
            : DateTime.parse(json[r'{{{baseName}}}']),
	{{/pattern}}
    {{^pattern}}
          : DateTime.parse(json[r'{{{baseName}}}']),
	{{/pattern}}
  {{/isDateTime}}
  {{#isDate}}
        {{{name}}}: json[r'{{{baseName}}}'] == null
          ? null
    {{#pattern}}
          : _dateEpochMarker == '{{{pattern}}}'
            ? DateTime.fromMillisecondsSinceEpoch(json[r'{{{baseName}}}'] as int, isUtc: true)
            : DateTime.parse(json[r'{{{baseName}}}']),
	{{/pattern}}
    {{^pattern}}
          : DateTime.parse(json[r'{{{baseName}}}']),
	{{/pattern}}
  {{/isDate}}
  {{^isDateTime}}
  {{^isDate}}
    {{#complexType}}
      {{#isArray}}
        {{#items.isArray}}
        {{{name}}}: json[r'{{{baseName}}}'] == null
          ? null
          : (json[r'{{{baseName}}}'] as List).map(
      {{#items.complexType}}
              {{items.complexType}}.listFromJson(json[r'{{{baseName}}}'])
      {{/items.complexType}}
      {{^items.complexType}}
              (e) => e == null ? null : (e as List).cast<{{items.items.dataType}}>()
      {{/items.complexType}}
            ).toList(growable: false),
        {{/items.isArray}}
        {{^items.isArray}}
        {{{name}}}: {{{complexType}}}.listFromJson(json[r'{{{baseName}}}']),
        {{/items.isArray}}
      {{/isArray}}
      {{^isArray}}
        {{#isMap}}
          {{#items.isArray}}
        {{{name}}}: json[r'{{{baseName}}}'] == null
          ? null
              {{#items.complexType}}
          : {{items.complexType}}.mapListFromJson(json[r'{{{baseName}}}']),
              {{/items.complexType}}
              {{^items.complexType}}
          : (json[r'{{{baseName}}}'] as Map).cast(),
  	          {{/items.complexType}}
          {{/items.isArray}}
          {{^items.isArray}}
        {{{name}}}: json[r'{{{baseName}}}'] == null
          ? null
          : {{{complexType}}}.mapFromJson(json[r'{{{baseName}}}']),
          {{/items.isArray}}
        {{/isMap}}
        {{^isMap}}
            {{#isBinary}}
        {{{name}}}: null, // No support for decoding binary content from JSON
            {{/isBinary}}
            {{^isBinary}}
        {{{name}}}: {{{complexType}}}.fromJson(json[r'{{{baseName}}}']),
            {{/isBinary}}
        {{/isMap}}
      {{/isArray}}
    {{/complexType}}
    {{^complexType}}
      {{#isArray}}
        {{#isEnum}}
        {{{name}}}: {{{classname}}}{{{items.datatypeWithEnum}}}.listFromJson(json[r'{{{baseName}}}']),
        {{/isEnum}}
        {{^isEnum}}
        {{{name}}}: json[r'{{{baseName}}}'] == null
          ? null
          : (json[r'{{{baseName}}}'] as {{#uniqueItems}}Set{{/uniqueItems}}{{^uniqueItems}}List{{/uniqueItems}}).cast<{{{items.datatype}}}>(),
        {{/isEnum}}
      {{/isArray}}
      {{^isArray}}
        {{#isMap}}
        {{{name}}}: json[r'{{{baseName}}}'] == null ?
          null :
          (json[r'{{{baseName}}}'] as Map).cast(),
        {{/isMap}}
        {{^isMap}}
          {{#isNumber}}
        {{{name}}}: json[r'{{{baseName}}}'] == null ?
          null :
          json[r'{{{baseName}}}'].toDouble(),
          {{/isNumber}}
          {{^isNumber}}
    {{^isEnum}}
        {{{name}}}: json[r'{{{baseName}}}'],
    {{/isEnum}}
    {{#isEnum}}
        {{{name}}}: {{{classname}}}{{{enumName}}}.fromJson(json[r'{{{baseName}}}']),
    {{/isEnum}}
          {{/isNumber}}
        {{/isMap}}
      {{/isArray}}
    {{/complexType}}
  {{/isDate}}
  {{/isDateTime}}
  {{/vars}}
    );

  static List<{{{classname}}}> listFromJson(List json, {bool emptyIsNull, bool growable,}) =>
    json == null || json.isEmpty
      ? true == emptyIsNull ? null : <{{{classname}}}>[]
      : json.map((v) => {{{classname}}}.fromJson(v)).toList(growable: true == growable);

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

  // maps a json object with a list of {{{classname}}}-objects as value to a dart map
  static Map> mapListFromJson(Map json, {bool emptyIsNull, bool growable,}) {
    final map = >{};
    if (json != null && json.isNotEmpty) {
      json.forEach((String key, dynamic v) {
        map[key] = {{{classname}}}.listFromJson(v, emptyIsNull: emptyIsNull, growable: growable);
      });
    }
    return map;
  }
}
{{#vars}}
    {{#isEnum}}
        {{^isContainer}}

{{>enum_inline}}
        {{/isContainer}}
        {{#isContainer}}
            {{#mostInnerItems}}

{{>enum_inline}}
            {{/mostInnerItems}}
        {{/isContainer}}
    {{/isEnum}}
{{/vars}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy