@@ -7,6 +7,7 @@ namespace Optimizely.Graph.Client.Tools
7
7
public class Program
8
8
{
9
9
private static string USER_AGENT => $ "Optimizely-Graph-Tools/{ typeof ( Program ) . Assembly . GetName ( ) . Version } ";
10
+ private static Dictionary < string , string > ExtensionMethodKeyPair = new Dictionary < string , string > ( ) ; //method name - class type
10
11
static void Main ( string [ ] args )
11
12
{
12
13
Console . WriteLine ( "**** Optimizely Content Graph Client Tools ****" ) ;
@@ -66,6 +67,10 @@ static void Main(string[] args)
66
67
output = Path . Combine ( di . FullName , defaultFileName ) ;
67
68
}
68
69
}
70
+ else
71
+ {
72
+ output = Path . Combine ( Directory . GetCurrentDirectory ( ) , defaultFileName ) ;
73
+ }
69
74
70
75
if ( args . Length > 2 )
71
76
{
@@ -111,6 +116,8 @@ public static void Run(IConfiguration configuration,
111
116
112
117
Console . WriteLine ( "Writing files..." ) ;
113
118
var sb = new StringBuilder ( ) ;
119
+ var extensionBuilder = new StringBuilder ( ) ;
120
+
114
121
sb . AppendLine ( "using System;" ) ;
115
122
sb . AppendLine ( "using System.Collections.Generic;" ) ;
116
123
sb . AppendLine ( "using EPiServer.Core;" ) ;
@@ -123,6 +130,8 @@ public static void Run(IConfiguration configuration,
123
130
sb . AppendLine ( "namespace " + modelNamespace ) ;
124
131
sb . AppendLine ( "{" ) ;
125
132
133
+ BeginExtension ( modelNamespace , extensionBuilder ) ;
134
+
126
135
foreach ( JProperty propertyType in propertyTypes )
127
136
{
128
137
var propertyTypeName = propertyType . Name ;
@@ -134,12 +143,14 @@ public static void Run(IConfiguration configuration,
134
143
135
144
foreach ( JProperty propertyProperty in properties )
136
145
{
146
+ var enumProps = new List < string > ( ) ;
137
147
var name = propertyProperty . Name ;
138
148
var dataType = ( propertyProperty . Children ( ) [ "type" ] . First ( ) as JValue ) . Value . ToString ( ) ;
139
- dataType = ConvertType ( dataType ) ;
149
+ dataType = ConvertType ( dataType , out var typeExtension ) ;
140
150
sb . Append ( $ " public { dataType } { name } ") ;
141
151
sb . AppendLine ( "{ get; set; }" ) ;
142
152
153
+ BuildExtensionMethods ( typeExtension , name , propertyTypeName , extensionBuilder ) ;
143
154
}
144
155
145
156
sb . AppendLine ( " }" ) ;
@@ -153,7 +164,8 @@ public static void Run(IConfiguration configuration,
153
164
154
165
if ( parentTypes != null && parentTypes . Count ( ) > 0 )
155
166
{
156
- inheritedFromType = string . Join ( ',' , parentTypes . Select ( type=> type . ToString ( ) ) ) ;
167
+ //inheritedFromType = string.Join(',', parentTypes.Select(type=> type.ToString()));
168
+ inheritedFromType = parentTypes . Select ( type => type . ToString ( ) ) . First ( ) ;
157
169
inheritedFromType = $ ":{ inheritedFromType } ";
158
170
}
159
171
sb . AppendLine ( $ " public partial class { propertyTypeName } { inheritedFromType } ") ;
@@ -166,7 +178,8 @@ public static void Run(IConfiguration configuration,
166
178
var name = propertyProperty . Name ;
167
179
var searchableAttr = ( bool ) ( ( propertyProperty . Children ( ) [ "searchable" ] . FirstOrDefault ( ) as JValue ) ? . Value ?? false ) ;
168
180
var dataType = ( propertyProperty . Children ( ) [ "type" ] . First ( ) as JValue ) . Value . ToString ( ) ;
169
- dataType = ConvertType ( dataType ) ;
181
+ dataType = ConvertType ( dataType , out var typeExtension ) ;
182
+ BuildExtensionMethods ( typeExtension , name , propertyTypeName , extensionBuilder ) ;
170
183
if ( searchableAttr )
171
184
{
172
185
sb . AppendLine ( $ " [Searchable]") ;
@@ -175,12 +188,19 @@ public static void Run(IConfiguration configuration,
175
188
sb . AppendLine ( "{ get; set; }" ) ;
176
189
177
190
}
178
-
191
+ //add system properties
192
+ //sb.AppendLine("[JsonProperty(\"_id\")]");
193
+ //sb.AppendLine("public string Id { get;set; }");
194
+ //end system properties
179
195
sb . AppendLine ( " }" ) ;
180
196
}
181
197
198
+ EndExtension ( extensionBuilder ) ;
182
199
183
200
sb . AppendLine ( "}" ) ;
201
+ //append extension to file
202
+ sb . AppendLine ( "//Extension methods" ) ;
203
+ sb . Append ( extensionBuilder ) ;
184
204
var classes = sb . ToString ( ) ;
185
205
FileInfo fi = new FileInfo ( output ) ;
186
206
// Check if directory exists, create if not
@@ -196,41 +216,47 @@ public static void Run(IConfiguration configuration,
196
216
Console . WriteLine ( $ "Optimizely Graph model C# classes have been written to { output } .") ;
197
217
}
198
218
199
- private static string ConvertType ( string propType )
219
+ private static string ConvertType ( string propType , out string typeExtension )
200
220
{
201
221
bool isIEnumerable = false ;
222
+ bool isComplexType = false ;
223
+ typeExtension = string . Empty ;
224
+
202
225
if ( propType . StartsWith ( "[" ) && propType . EndsWith ( "]" ) ) //Is IEnumerable
203
226
{
204
227
propType = propType . Substring ( 1 , propType . Length - 2 ) ;
205
228
isIEnumerable = true ;
206
229
}
207
- if ( propType . Equals ( "Date" ) )
208
- {
209
- propType = "DateTime" ;
210
- }
211
- if ( propType . Equals ( "Bool" ) || propType . Equals ( "Boolean" ) )
212
- {
213
- propType = "bool" ;
214
- }
215
- if ( propType . Equals ( "String" ) )
216
- {
217
- propType = "string" ;
218
- }
219
- if ( propType . Equals ( "LongString" ) )
220
- {
221
- propType = "string" ;
222
- }
223
- if ( propType . Equals ( "Int" ) )
224
- {
225
- propType = "int" ;
226
- }
227
- if ( propType . Equals ( "Float" ) )
230
+
231
+ switch ( propType )
228
232
{
229
- propType = "float" ;
233
+ case "Date" :
234
+ propType = "DateTime" ;
235
+ break ;
236
+ case "Bool" :
237
+ propType = "bool" ;
238
+ break ;
239
+ case "Boolean" :
240
+ propType = "bool" ;
241
+ break ;
242
+ case "String" :
243
+ propType = "string" ;
244
+ break ;
245
+ case "Int" :
246
+ propType = "int" ;
247
+ break ;
248
+ case "Float" :
249
+ propType = "float" ;
250
+ break ;
251
+ default :
252
+ isComplexType = true ;
253
+ break ;
230
254
}
255
+
231
256
if ( isIEnumerable )
232
257
{
233
- propType = $ "IEnumerable<{ propType } >";
258
+ typeExtension = isComplexType ? propType : string . Empty ;
259
+ return $ "IEnumerable<{ propType } >";
234
260
}
235
261
236
262
return propType ;
@@ -292,6 +318,33 @@ private static Config ReadConfig(IConfiguration configuration)
292
318
SecretKey = myconfigs . GetSection ( "ContentGraph:Secret" ) . Value
293
319
} ;
294
320
}
321
+ private static void BeginExtension ( string modelNamespace , StringBuilder builder )
322
+ {
323
+ builder . AppendLine ( $ "namespace { modelNamespace } .Extension") ;
324
+ builder . AppendLine ( "{" ) ; //begin namespace
325
+ builder . AppendLine ( $ " public static class GraphModelsExtension") ;
326
+ builder . AppendLine ( " {" ) ; //begin class
327
+ }
328
+ private static void EndExtension ( StringBuilder builder )
329
+ {
330
+ builder . AppendLine ( " }" ) ; //end class
331
+ builder . AppendLine ( "}" ) ; //end namespace
332
+ }
333
+ private static void BuildExtensionMethods ( string returnType , string methodName , string classType , StringBuilder builder )
334
+ {
335
+ if ( ! string . IsNullOrEmpty ( returnType ) )
336
+ {
337
+ if ( ExtensionMethodKeyPair . ContainsKey ( methodName ) && ExtensionMethodKeyPair [ methodName ] == classType )
338
+ {
339
+ return ;
340
+ }
341
+ ExtensionMethodKeyPair . TryAdd ( methodName , classType ) ;
342
+ builder . Append ( $ " public static { returnType } { methodName } (this { classType } obj) ") ;
343
+ builder . AppendLine ( " {" ) ;
344
+ builder . AppendLine ( " return null;" ) ;
345
+ builder . AppendLine ( " }" ) ;
346
+ }
347
+ }
295
348
}
296
349
internal class Config
297
350
{
0 commit comments