-
Notifications
You must be signed in to change notification settings - Fork 3
/
TypeScriptGenerator.tt
160 lines (142 loc) · 3.54 KB
/
TypeScriptGenerator.tt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Globalization" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="nTsMapper.TypeScript" #>
// ReSharper disable RedundantQualifier
<#
// Enums
foreach (var module in mEnumsToGenerate) {
#>
module <#= module.Key #> {
<#
foreach (var tsType in module) {
#>
export enum <#= tsType.TsTypeName #> {
<#
foreach (var enumMember in tsType.Members) {
#>
<#=enumMember.Key #> = <#= enumMember.Value #>,
<#
}
#>
}
<#
}
#>
}
<#
}
#>
<#
foreach (var tsType in mTypesToGenerate) {
if (mDebugMode) {
#>
<#
}
#>
module <#= tsType.ModuleName #> {
export interface I<#= tsType.TsTypeName #> <#= tsType.BaseTsType != null ? "extends " + string.Format("{0}.I{1}", ((TsTypeWithProperties)tsType.BaseTsType).ModuleName, tsType.BaseTsType.TsTypeName) : "" #> {
<#
foreach (var prop in tsType.Properties) {
#>
<#=CamelCase(prop.Name) #>: <#= prop.TsType.TsTypeReferenceName #>;
<#
}
#>
}
<#
if (mDebugMode) {
#>
// InheritanceHierarchyLevel: <#= (mDebugMode) ? ((TsTypeWithProperties)tsType).InheritanceHierarchyLevel.ToString() : "" #>
<#
}
#>
export class <#= tsType.TsTypeName #> <#= tsType.BaseTsType != null ? "extends " + tsType.BaseTsType.TsTypeReferenceName : "" #> implements I<#= tsType.TsTypeName #> {
<#
foreach (var prop in tsType.Properties) {
#>
<#=CamelCase(prop.Name) #>: <#= prop.TsType.TsTypeReferenceName #>;
<#
}
#>
public static fromJSON(json: any) : <#= tsType.TsTypeName #> {
if (json === undefined)
return undefined;
if (json === null)
return null;
<#
if (tsType.BaseTsType == null) {
#>
return {
<#
for (var i = 0; i < tsType.Properties.Count; i++) {
var prop = tsType.Properties[i];
#>
<#= CamelCase(prop.Name) #>: <#= GetJsonAssignment(prop)#><#
if (i < tsType.Properties.Count - 1) { #>,
<# }
}
#>
};
<#
} else {
#>
var baseObj: <#=tsType.TsTypeName#> = <<#=tsType.TsTypeName#>> <#=tsType.BaseTsType.TsTypeReferenceName#>.fromJSON(json);
<#
foreach (var prop in tsType.Properties) {
#>
baseObj.<#= CamelCase(prop.Name) #> = <#= GetJsonAssignment(prop)#>;
<#
}
#>
return baseObj;
<#
}
#>
}
}
}
<#
}
#>
<#
#>
// ReSharper restore RedundantQualifier
<#+
// camel case converter code borrowed from JSON.NET
string CamelCase(string identifier) {
if (string.IsNullOrEmpty(identifier))
return identifier;
if (!char.IsUpper(identifier[0]))
return identifier;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < identifier.Length; i++)
{
bool hasNext = (i + 1 < identifier.Length);
if ((i == 0 || !hasNext) || char.IsUpper(identifier[i + 1]))
{
char lowerCase;
lowerCase = char.ToLower(identifier[i], CultureInfo.InvariantCulture);
sb.Append(lowerCase);
}
else
{
sb.Append(identifier.Substring(i));
break;
}
}
return sb.ToString();
}
string GetJsonAssignment(TsProperty prop) {
return GetAssignmentExpression(prop.TsType, "json." + CamelCase(prop.Name));
}
string GetAssignmentExpression(TsType type, string sourceExpression) {
if(type is TsMappedType) return string.Format(((TsMappedType)type).AssignmentTemplate, sourceExpression);
if(type is TsTypeWithProperties) return type.TsTypeReferenceName + ".fromJSON(" + sourceExpression + ")";
if(type is TsCollection) return sourceExpression + " === null ? null : " + sourceExpression + ".map(o => " + GetAssignmentExpression(((TsCollection)type).TsItemType, "o") + ")";
return sourceExpression;
}
#>