Skip to content

Commit 0681707

Browse files
committed
Refactor C# type generation for improved mapping
Updated integer type mapping from int to long and refactored the From and ToMap methods for C# type generation. The changes improve handling of arrays, enums, relationships, and nullable types, resulting in more concise and robust code generation.
1 parent 99b4ba4 commit 0681707

File tree

1 file changed

+45
-72
lines changed

1 file changed

+45
-72
lines changed

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

Lines changed: 45 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CSharp extends LanguageMeta {
1515
}
1616
break;
1717
case AttributeType.INTEGER:
18-
type = "int";
18+
type = "long";
1919
break;
2020
case AttributeType.FLOAT:
2121
type = "double";
@@ -87,103 +87,76 @@ public class <%= toPascalCase(collection.name) %>
8787
<% } -%>
8888
}
8989

90-
public static <%= toPascalCase(collection.name) %> From(Dictionary<string, object> map)
91-
{
92-
return new <%= toPascalCase(collection.name) %>(
90+
public static <%= toPascalCase(collection.name) %> From(Dictionary<string, object> map) => new <%= toPascalCase(collection.name) %>(
9391
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
94-
<%- toCamelCase(attribute.key) %>:<%
92+
<%- toCamelCase(attribute.key) %>: <%
9593
// ENUM
9694
if (attribute.format === 'enum') {
9795
if (attribute.array) {
98-
-%> (map["<%- attribute.key %>"] as IEnumerable<object>)<%- !attribute.required ? '?' : '' %>.Select(e => (<%- toPascalCase(attribute.key) %>)Enum.Parse(typeof(<%- toPascalCase(attribute.key) %>), e.ToString())).ToList()<%
96+
-%>((IEnumerable<object>)map["<%- attribute.key %>"]).Select(e => Enum.Parse<Models.<%- toPascalCase(attribute.key) %>>(e.ToString()!, true)).ToList()<%
9997
} else {
100-
if (attribute.required) {
101-
-%> (<%- toPascalCase(attribute.key) %>)Enum.Parse(typeof(<%- toPascalCase(attribute.key) %>), map["<%- attribute.key %>"].ToString())<%
102-
} else {
103-
-%> map["<%- attribute.key %>"] != null ? (<%- toPascalCase(attribute.key) %>)Enum.Parse(typeof(<%- toPascalCase(attribute.key) %>), map["<%- attribute.key %>"].ToString()) : null<%
104-
}
98+
-%>Enum.Parse<Models.<%- toPascalCase(attribute.key) %>>(map["<%- attribute.key %>"].ToString()!, true)<%
10599
}
106100
// RELATIONSHIP
107101
} else if (attribute.type === 'relationship') {
108102
const relatedClass = toPascalCase(collections.find(c => c.$id === attribute.relatedCollection).name);
109103
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany' || attribute.array) {
110-
-%> (map["<%- attribute.key %>"] as IEnumerable<object>)<%- !attribute.required ? '?' : '' %>.Select(e => Models.<%- relatedClass %>.From((Dictionary<string, object>)e)).ToList()<%
104+
-%>((IEnumerable<object>)map["<%- attribute.key %>"]).Select(it => Models.<%- relatedClass %>.From((Dictionary<string, object>)it)).ToList()<%
111105
} else {
112-
if (attribute.required) {
113-
-%> <%- relatedClass %>.From((Dictionary<string, object>)map["<%- attribute.key %>"])<%
114-
} else {
115-
-%> map["<%- attribute.key %>"] != null ? <%- relatedClass %>.From((Dictionary<string, object>)map["<%- attribute.key %>"]) : null<%
116-
}
106+
-%>Models.<%- relatedClass %>.From((Dictionary<string, object>)map["<%- attribute.key %>"])<%
117107
}
118-
// INTEGER
119-
} else if (attribute.type === 'integer') {
120-
if (attribute.array) {
121-
-%> (map["<%- attribute.key %>"] as IEnumerable<object>)<%- !attribute.required ? '?' : '' %>.Select(e => Convert.ToInt32(e)).ToList()<%
122-
} else {
123-
-%> map["<%- attribute.key %>"] != null ? Convert.ToInt32(map["<%- attribute.key %>"]) : <% if (attribute.required) { %>throw new ArgumentNullException("<%- attribute.key %>", "Required attribute '<%- attribute.key %>' was null.")<% } else { %>null<% } %><%
108+
// ARRAY TYPES
109+
} else if (attribute.array) {
110+
if (attribute.type === 'string' || attribute.type === 'datetime' || attribute.type === 'email') {
111+
-%>((IEnumerable<object>)map["<%- attribute.key %>"]).Select(x => x?.ToString())<%- attribute.required ? '.Where(x => x != null)' : '' %>.ToList()!<%
112+
} else if (attribute.type === 'integer') {
113+
-%>((IEnumerable<object>)map["<%- attribute.key %>"]).Select(x => <%- !attribute.required ? 'x == null ? (long?)null : ' : '' %>Convert.ToInt64(x)).ToList()<%
114+
} else if (attribute.type === 'double') {
115+
-%>((IEnumerable<object>)map["<%- attribute.key %>"]).Select(x => <%- !attribute.required ? 'x == null ? (double?)null : ' : '' %>Convert.ToDouble(x)).ToList()<%
116+
} else if (attribute.type === 'boolean') {
117+
-%>((IEnumerable<object>)map["<%- attribute.key %>"]).Select(x => <%- !attribute.required ? 'x == null ? (bool?)null : ' : '' %>(bool)x).ToList()<%
124118
}
125-
// FLOAT
119+
// SINGLE VALUE TYPES
120+
} else if (attribute.type === 'integer') {
121+
-%><%- !attribute.required ? 'map["' + attribute.key + '"] == null ? null : ' : '' %>Convert.ToInt64(map["<%- attribute.key %>"])<%
126122
} else if (attribute.type === 'double') {
127-
if (attribute.array) {
128-
-%> (map["<%- attribute.key %>"] as IEnumerable<object>)<%- !attribute.required ? '?' : '' %>.Select(e => Convert.ToDouble(e)).ToList()<%
129-
} else {
130-
-%> map["<%- attribute.key %>"] != null ? Convert.ToDouble(map["<%- attribute.key %>"]) : <% if (attribute.required) { %>throw new ArgumentNullException("<%- attribute.key %>", "Required attribute '<%- attribute.key %>' was null.")<% } else { %>null<% } %><%
131-
}
132-
// BOOLEAN
123+
-%><%- !attribute.required ? 'map["' + attribute.key + '"] == null ? null : ' : '' %>Convert.ToDouble(map["<%- attribute.key %>"])<%
133124
} else if (attribute.type === 'boolean') {
134-
if (attribute.array) {
135-
-%> (map["<%- attribute.key %>"] as IEnumerable<object>)<%- !attribute.required ? '?' : '' %>.Select(e => Convert.ToBoolean(e)).ToList()<%
136-
} else {
137-
-%> map["<%- attribute.key %>"] != null ? Convert.ToBoolean(map["<%- attribute.key %>"]) : <% if (attribute.required) { %>throw new ArgumentNullException("<%- attribute.key %>", "Required attribute '<%- attribute.key %>' was null.")<% } else { %>null<% } %><%
138-
}
139-
// STRING, DATETIME, EMAIL
125+
-%>(<%- getType(attribute, collections) %>)map["<%- attribute.key %>"]<%
140126
} else if (attribute.type === 'string' || attribute.type === 'datetime' || attribute.type === 'email') {
141-
if (attribute.array) {
142-
-%> (map["<%- attribute.key %>"] as IEnumerable<object>)<%- !attribute.required ? '?' : '' %>.Select(e => e.ToString()).ToList()<%
143-
} else {
144-
if (attribute.required) {
145-
-%> map["<%- attribute.key %>"].ToString()<%
146-
} else {
147-
-%> map["<%- attribute.key %>"]?.ToString()<%
148-
}
149-
}
150-
// UNKNOWN
127+
-%>map["<%- attribute.key %>"]<%- !attribute.required ? '?' : '' %>.ToString()<%- attribute.required ? '!' : ''%><%
151128
} else {
152-
-%> null <%
129+
-%>default<%
153130
}
154131
-%><% if (index < collection.attributes.length - 1) { %>,<% } %>
155132
<% } -%>
156-
);
157-
}
133+
);
158134

159-
public Dictionary<string, object?> ToMap()
135+
public Dictionary<string, object?> ToMap() => new Dictionary<string, object?>()
160136
{
161-
return new Dictionary<string, object?>
162-
{
163137
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
164-
{ "<%= attribute.key %>",<%
165-
// ENUM
166-
if (attribute.format === 'enum') {
167-
if (attribute.array) {
168-
-%> <%= toPascalCase(attribute.key) %>?.Select(e => e.ToString()).ToList()<%
169-
} else {
170-
-%> <%= toPascalCase(attribute.key) %>?.ToString()<%
171-
}
172-
// RELATIONSHIP
173-
} else if (attribute.type === 'relationship') {
174-
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany' || attribute.array) {
175-
-%> <%= toPascalCase(attribute.key) %>?.Select(e => e.ToMap()).ToList()<%
176-
} else {
177-
-%> <%= toPascalCase(attribute.key) %>?.ToMap()<%
178-
}
179-
// OTHER
138+
{ "<%- attribute.key %>", <%
139+
// ENUM
140+
if (attribute.format === 'enum') {
141+
if (attribute.array) {
142+
-%><%= toPascalCase(attribute.key) %>?.Select(e => e.ToString()).ToList()<%
180143
} else {
181-
-%> <%= toPascalCase(attribute.key) %><%
144+
-%><%= toPascalCase(attribute.key) %>?.ToString()<%
182145
}
183-
-%> }<% if (index < collection.attributes.length - 1) { %>,<% } %>
146+
// RELATIONSHIP
147+
} else if (attribute.type === 'relationship') {
148+
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany' || attribute.array) {
149+
-%><%= toPascalCase(attribute.key) %>?.Select(e => e.ToMap()).ToList()<%
150+
} else {
151+
-%><%= toPascalCase(attribute.key) %>?.ToMap()<%
152+
}
153+
// OTHER
154+
} else {
155+
-%><%= toPascalCase(attribute.key) %><%
156+
}
157+
-%> }<% if (index < collection.attributes.length - 1) { %>,<% } %>
184158
<% } -%>
185-
};
186-
}
159+
};
187160
}
188161
}
189162
`;

0 commit comments

Comments
 (0)