Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Commit2bbb8ed

Browse files
Generate method extensions for Enumerable prop type
1 parentfb6da50 commit2bbb8ed

File tree

3 files changed

+84
-29
lines changed

3 files changed

+84
-29
lines changed

‎APIs/src/CGTypeSync/Optimizely.ContentGraph.Client.Tools.csproj‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
<PackageId>Optimizely.Graph.Client.Tools</PackageId>
1313
<Authors>Optimizely</Authors>
1414
<Title>Optimizely Graph Client tool</Title>
15-
<Version>$(version)</Version>
15+
<!--<Version>$(version)</Version>-->
1616
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
1717
<PackageProjectUrl>https://github.com/episerver/graph-net-sdk</PackageProjectUrl>
1818
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
1919
<Description>Optimizely Graph Client tool for generating models from schema</Description>
2020
<Copyright>Copyright(c) Optimizely $(Year)</Copyright>
2121
<PackageTags>Optimizely Graph Client Tools</PackageTags>
22+
<Version>$(ToolsVersion)$(ToolsVersion)</Version>
2223
<!--End nuget specs-->
2324
</PropertyGroup>
2425

‎APIs/src/CGTypeSync/Program.cs‎

Lines changed: 81 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Optimizely.Graph.Client.Tools
77
publicclassProgram
88
{
99
privatestaticstringUSER_AGENT=>$"Optimizely-Graph-Tools/{typeof(Program).Assembly.GetName().Version}";
10+
privatestaticDictionary<string,string>ExtensionMethodKeyPair=newDictionary<string,string>();//method name - class type
1011
staticvoidMain(string[]args)
1112
{
1213
Console.WriteLine("**** Optimizely Content Graph Client Tools ****");
@@ -66,6 +67,10 @@ static void Main(string[] args)
6667
output=Path.Combine(di.FullName,defaultFileName);
6768
}
6869
}
70+
else
71+
{
72+
output=Path.Combine(Directory.GetCurrentDirectory(),defaultFileName);
73+
}
6974

7075
if(args.Length>2)
7176
{
@@ -111,6 +116,8 @@ public static void Run(IConfiguration configuration,
111116

112117
Console.WriteLine("Writing files...");
113118
varsb=newStringBuilder();
119+
varextensionBuilder=newStringBuilder();
120+
114121
sb.AppendLine("using System;");
115122
sb.AppendLine("using System.Collections.Generic;");
116123
sb.AppendLine("using EPiServer.Core;");
@@ -123,6 +130,8 @@ public static void Run(IConfiguration configuration,
123130
sb.AppendLine("namespace "+modelNamespace);
124131
sb.AppendLine("{");
125132

133+
BeginExtension(modelNamespace,extensionBuilder);
134+
126135
foreach(JPropertypropertyTypeinpropertyTypes)
127136
{
128137
varpropertyTypeName=propertyType.Name;
@@ -134,12 +143,14 @@ public static void Run(IConfiguration configuration,
134143

135144
foreach(JPropertypropertyPropertyinproperties)
136145
{
146+
varenumProps=newList<string>();
137147
varname=propertyProperty.Name;
138148
vardataType=(propertyProperty.Children()["type"].First()asJValue).Value.ToString();
139-
dataType=ConvertType(dataType);
149+
dataType=ConvertType(dataType,outvartypeExtension);
140150
sb.Append($" public{dataType}{name} ");
141151
sb.AppendLine("{ get; set; }");
142152

153+
BuildExtensionMethods(typeExtension,name,propertyTypeName,extensionBuilder);
143154
}
144155

145156
sb.AppendLine(" }");
@@ -153,7 +164,8 @@ public static void Run(IConfiguration configuration,
153164

154165
if(parentTypes!=null&&parentTypes.Count()>0)
155166
{
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();
157169
inheritedFromType=$":{inheritedFromType}";
158170
}
159171
sb.AppendLine($" public partial class{propertyTypeName}{inheritedFromType}");
@@ -166,7 +178,8 @@ public static void Run(IConfiguration configuration,
166178
varname=propertyProperty.Name;
167179
varsearchableAttr=(bool)((propertyProperty.Children()["searchable"].FirstOrDefault()asJValue)?.Value??false);
168180
vardataType=(propertyProperty.Children()["type"].First()asJValue).Value.ToString();
169-
dataType=ConvertType(dataType);
181+
dataType=ConvertType(dataType,outvartypeExtension);
182+
BuildExtensionMethods(typeExtension,name,propertyTypeName,extensionBuilder);
170183
if(searchableAttr)
171184
{
172185
sb.AppendLine($" [Searchable]");
@@ -175,12 +188,19 @@ public static void Run(IConfiguration configuration,
175188
sb.AppendLine("{ get; set; }");
176189

177190
}
178-
191+
//add system properties
192+
//sb.AppendLine("[JsonProperty(\"_id\")]");
193+
//sb.AppendLine("public string Id { get;set; }");
194+
//end system properties
179195
sb.AppendLine(" }");
180196
}
181197

198+
EndExtension(extensionBuilder);
182199

183200
sb.AppendLine("}");
201+
//append extension to file
202+
sb.AppendLine("//Extension methods");
203+
sb.Append(extensionBuilder);
184204
varclasses=sb.ToString();
185205
FileInfofi=newFileInfo(output);
186206
// Check if directory exists, create if not
@@ -196,41 +216,47 @@ public static void Run(IConfiguration configuration,
196216
Console.WriteLine($"Optimizely Graph model C# classes have been written to{output}.");
197217
}
198218

199-
privatestaticstringConvertType(stringpropType)
219+
privatestaticstringConvertType(stringpropType,outstringtypeExtension)
200220
{
201221
boolisIEnumerable=false;
222+
boolisComplexType=false;
223+
typeExtension=string.Empty;
224+
202225
if(propType.StartsWith("[")&&propType.EndsWith("]"))//Is IEnumerable
203226
{
204227
propType=propType.Substring(1,propType.Length-2);
205228
isIEnumerable=true;
206229
}
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)
228232
{
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;
230254
}
255+
231256
if(isIEnumerable)
232257
{
233-
propType=$"IEnumerable<{propType}>";
258+
typeExtension=isComplexType?propType:string.Empty;
259+
return$"IEnumerable<{propType}>";
234260
}
235261

236262
returnpropType;
@@ -292,6 +318,33 @@ private static Config ReadConfig(IConfiguration configuration)
292318
SecretKey=myconfigs.GetSection("ContentGraph:Secret").Value
293319
};
294320
}
321+
privatestaticvoidBeginExtension(stringmodelNamespace,StringBuilderbuilder)
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+
privatestaticvoidEndExtension(StringBuilderbuilder)
329+
{
330+
builder.AppendLine(" }");//end class
331+
builder.AppendLine("}");//end namespace
332+
}
333+
privatestaticvoidBuildExtensionMethods(stringreturnType,stringmethodName,stringclassType,StringBuilderbuilder)
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+
}
295348
}
296349
internalclassConfig
297350
{

‎msbuild/version.props‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<PropertyGroup>
44
<VersionPrefix>1.4.1</VersionPrefix>
55
<VersionSuffix></VersionSuffix>
6+
<ToolsVersion>1.3.0</ToolsVersion>
67
</PropertyGroup>
78
</Project>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp