Skip to content

Commit 8a759ca

Browse files
committed
feat: add factory and toMap methods for Dart model generation
1 parent 41efeed commit 8a759ca

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

templates/cli/lib/type-generation/languages/dart.js.twig

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,78 @@ class <%= toPascalCase(collection.name) %> {
6868
<% if (attribute.required) { %>required <% } %>this.<%= toCamelCase(attribute.key) %>,
6969
<% } -%>
7070
});
71+
72+
factory <%= toPascalCase(collection.name) %>.fromMap(Map<String, dynamic> map) {
73+
return <%= toPascalCase(collection.name) %>(
74+
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
75+
<%= toCamelCase(attribute.key) %>: <% if (attribute.type === 'string' || attribute.type === 'email' || attribute.type === 'datetime') { -%>
76+
<% if (attribute.format === 'enum') { -%>
77+
<% if (attribute.array) { -%>
78+
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(attribute.key) %>.values.firstWhere((element) => element.name == e)).toList()<% if (!attribute.required) { %> ?? []<% } -%>
79+
<% } else { -%>
80+
<% if (!attribute.required) { -%>
81+
map['<%= attribute.key %>'] != null ? <%- toPascalCase(attribute.key) %>.values.where((e) => e.name == map['<%= attribute.key %>']).firstOrNull : null<% } else { -%>
82+
<%- toPascalCase(attribute.key) %>.values.firstWhere((e) => e.name == map['<%= attribute.key %>'])<% } -%>
83+
<% } -%>
84+
<% } else { -%>
85+
<% if (attribute.array) { -%>
86+
List<String>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
87+
<% } else { -%>
88+
map['<%= attribute.key %>']<% if (!attribute.required) { %>?<% } %>.toString()<% if (!attribute.required) { %> ?? null<% } -%>
89+
<% } -%>
90+
<% } -%>
91+
<% } else if (attribute.type === 'integer') { -%>
92+
<% if (attribute.array) { -%>
93+
List<int>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
94+
<% } else { -%>
95+
map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%>
96+
<% } -%>
97+
<% } else if (attribute.type === 'float') { -%>
98+
<% if (attribute.array) { -%>
99+
List<double>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
100+
<% } else { -%>
101+
map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%>
102+
<% } -%>
103+
<% } else if (attribute.type === 'boolean') { -%>
104+
<% if (attribute.array) { -%>
105+
List<bool>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
106+
<% } else { -%>
107+
map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%>
108+
<% } -%>
109+
<% } else if (attribute.type === 'relationship') { -%>
110+
<% if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { -%>
111+
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(attribute.relatedCollection) %>.fromMap(e)).toList()<% if (!attribute.required) { %> ?? []<% } -%>
112+
<% } else { -%>
113+
<% if (!attribute.required) { -%>
114+
map['<%= attribute.key %>'] != null ? <%- toPascalCase(attribute.relatedCollection) %>.fromMap(map['<%= attribute.key %>']) : null<% } else { -%>
115+
<%- toPascalCase(attribute.relatedCollection) %>.fromMap(map['<%= attribute.key %>'])<% } -%>
116+
<% } -%>
117+
<% } -%>,
118+
<% } -%>
119+
);
120+
}
121+
122+
Map<String, dynamic> toMap() {
123+
return {
124+
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
125+
"<%= attribute.key %>": <% if (attribute.type === 'relationship') { -%>
126+
<% if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { -%>
127+
<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.map((e) => e.toMap()).toList()<% if (!attribute.required) { %> ?? []<% } -%>
128+
<% } else { -%>
129+
<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.toMap()<% if (!attribute.required) { %> ?? {}<% } -%>
130+
<% } -%>
131+
<% } else if (attribute.format === 'enum') { -%>
132+
<% if (attribute.array) { -%>
133+
<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.map((e) => e.name).toList()<% if (!attribute.required) { %> ?? []<% } -%>
134+
<% } else { -%>
135+
<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.name<% if (!attribute.required) { %> ?? null<% } -%>
136+
<% } -%>
137+
<% } else { -%>
138+
<%= toCamelCase(attribute.key) -%>
139+
<% } -%>,
140+
<% } -%>
141+
};
142+
}
71143
}
72144
`;
73145
}

0 commit comments

Comments
 (0)