dart2-v3template.class.mustache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of connect-openapi-dart Show documentation
Show all versions of connect-openapi-dart Show documentation
dart2 generator from openapi 3.x spec files. Changing to openapitools dependencies.
class {{classname}} {
{{#vars}}
{{#description}}/* {{{description}}} */{{/description}}
{{{dataType}}} {{name}} = {{{defaultValue}}};
{{#allowableValues}}
{{#min}} // range from {{min}} to {{max}}{{/min}}//{{^min}}enum {{name}}Enum { {{#values}} {{.}}, {{/values}} };{{/min}}{
{{/allowableValues}}
{{/vars}}
{{classname}}();
@override
String toString() {
return '{{classname}}[{{#vars}}{{name}}=${{name}}, {{/vars}}]';
}
{{classname}}.fromJson(Map json) {
if (json == null) return;
{{#vars}}
if (json['{{baseName}}'] == null) {
{{name}} = null;
} else {
{{#isDateTime}}
{{name}} = DateTime.parse(json['{{baseName}}']);
{{/isDateTime}}
{{#isDate}}
var formatter = DateFormat('yyyy-MM-dd');
{{name}} = formatter.parseStrict(json['{{baseName}}']);
{{/isDate}}
{{^isDateTime}}
{{^isDate}}
{{#complexType}}
{{#isListContainer}}
{{name}} = {{complexType}}.listFromJson(json['{{baseName}}']);
{{/isListContainer}}
{{^isListContainer}}
{{#isMapContainer}}
{{name}} = {{complexType}}.mapFromJson(json['{{baseName}}']);
{{/isMapContainer}}
{{^isMapContainer}}
{{name}} = {{complexType}}.fromJson(json['{{baseName}}']);
{{/isMapContainer}}
{{/isListContainer}}
{{/complexType}}
{{^complexType}}
{{#isListContainer}}
{{name}} = (json['{{baseName}}'] as List).cast<{{items.datatype}}>();
{{/isListContainer}}
{{^isListContainer}}
{{#isMapContainer}}
{{name}} = (json['{{baseName}}'] as Map).cast();
{{/isMapContainer}}
{{^isMapContainer}}
// here
{{name}} = json['{{baseName}}'];
{{/isMapContainer}}
{{/isListContainer}}
{{/complexType}}
{{/isDate}}
{{/isDateTime}}
}
{{/vars}}
}
Map toJson() {
Map json = {};
{{#vars}}
{{^isNullable}}
if ({{name}} != null)
{{/isNullable}}
{{#isDateTime}}
json['{{baseName}}'] = {{name}} == null ? null : {{name}}.toUtc().toIso8601String();
{{/isDateTime}}
{{#isDate}}
json['{{baseName}}'] = {{name}} == null ? null : {{name}}.toUtc().toIso8601String();
{{/isDate}}
{{^isDateTime}}
{{^isDate}}
{{^isEnum}}
json['{{baseName}}'] = {{name}};
{{/isEnum}}
{{#isEnum}}
json['{{baseName}}'] = {{complexType}}TypeTransformer.encode({{name}});
{{/isEnum}}
{{/isDate}}
{{/isDateTime}}
{{/vars}}
return json;
}
static List<{{classname}}> listFromJson(List json) {
return json == null ? List<{{classname}}>() : json.map((value) => {{classname}}.fromJson(value)).toList();
}
static Map mapFromJson(Map json) {
var map = Map();
if (json != null && json.isNotEmpty) {
json.forEach((String key, dynamic value) => map[key] = {{classname}}.fromJson(value));
}
return map;
}
}