Skip to content

Commit d86c4d3

Browse files
author
Robert Anderson
committed
Merge branch 'master' of github.com:microsoftgraph/MSGraph-SDK-Code-Generator
2 parents 4c10e29 + c79fe44 commit d86c4d3

File tree

10 files changed

+114
-28
lines changed

10 files changed

+114
-28
lines changed

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ Currently the following target languages are supported by this writer:
1212
- Python
1313

1414
# Contents
15-
- [Prerequisites](#Prerequisites)
16-
- [Getting started](#Getting-started)
17-
- [Using Vipr with this writer](#Using-Vipr-with-this-Writer)
18-
- [Using generated code](#Using-generated-code)
19-
- [Contributing](#Contributing)
20-
- [License](#License)
15+
- [Prerequisites](#prerequisites)
16+
- [Getting started](#getting-started)
17+
- [Using Vipr with this writer](#using-vipr-with-this-writer)
18+
- [Contributing](#contributing)
19+
- [License](#license)
2120

2221
## Prerequisites
2322
- [Visual Studio SDK](https://www.microsoft.com/en-us/download/details.aspx?id=40758)
@@ -36,9 +35,9 @@ For more information on submodules read [this chapter](http://git-scm.com/book/e
3635
## Using Vipr with this Writer
3736

3837
1. Build the solution in Visual Studio.
39-
2. Go to the `src\T4TemplateWriter\bin\debug` folder to find all compiled components.
38+
2. Go to the `src\GraphODataTemplateWriter\bin\debug` folder to find all compiled components.
4039
3. In that folder, modify `.config\TemplateWriterSettings.json` to specify your template mapping see [Template Writer Settings](##Template-Writer-Settings) for more details.
41-
4. Open a command prompt as administrator in the same folder and run `Vipr.exe <path-or-url-to-metadata> --writer="GraphODataTemplateWriter"`
40+
4. Open a command prompt as administrator in the same folder and run `Vipr.exe <path-or-url-to-metadata> --writer="GraphODataTemplateWriter"`. An example metadata file can be found in the root of this project.
4241

4342
By default, output source code will be put in a folder named "output" next to the Vipr executable.
4443

Templates/Android/BaseModel.template.tt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,15 @@
473473
return null;
474474
}
475475

476+
public string OdcmMethodReturnType(OdcmMethod method) {
477+
return method.ReturnType is OdcmPrimitiveType
478+
? method.ReturnType.GetTypeString() : method.ReturnType.Name.ToCheckedCase();
479+
}
480+
476481
public string CollectionPageGeneric(OdcmObject c) {
477482
if (c is OdcmMethod) {
478-
return "<" + ClassTypeName(c) + ", " + ITypeCollectionRequestBuilder(c) + ">";
483+
string returnType = OdcmMethodReturnType(c as OdcmMethod);
484+
return "<" + returnType + ", " + ITypeCollectionRequestBuilder(c) + ">";
479485
}
480486
return "<" + TypeName(c) + ", " + ITypeCollectionRequestBuilder(c) + ">";
481487
}

Templates/Android/generated/BaseMethodCollectionResponse.java.tt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ import com.google.gson.annotations.*;
1313
<#=CreateClassDef(BaseTypeCollectionResponse(c), null, "IJsonBackedObject")#>
1414

1515
@SerializedName("value")
16-
public List<<#=ClassTypeName(c)#>> value;
16+
public List<<#=OdcmMethodReturnType(c as OdcmMethod)#>> value;
1717

1818
@SerializedName("@odata.nextLink")
1919
public String nextLink;
2020

2121
<#=CreateRawJsonObject()#>
22-
<#=UpdateListPropertiesWithinSetRawObject(new [] { "value" })#>
22+
<# if ( ! ((c as OdcmMethod).ReturnType is OdcmPrimitiveType) ) { #>
23+
<#= UpdateListPropertiesWithinSetRawObject(new [] { "value" })#>
24+
<# } else { #>
25+
}
26+
<# } #>
2327
}

Templates/ObjC/Base/SharedObjC.template.tt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@ private string GetObjCTypeIdentifier(OdcmObject o, bool getUnderlyingType=false)
2727
if(o is OdcmProperty)
2828
{
2929
OdcmProperty prop=(OdcmProperty)o;
30+
3031
if(prop.IsCollection && !getUnderlyingType)
3132
{
3233
return @"NSArray";
3334
}
3435

36+
if (prop.IsComplexCollectionOpenType(model) && !getUnderlyingType)
37+
{
38+
return @"NSDictionary";
39+
}
40+
3541
return GetObjCTypeIdentifier(prop.Projection.Type,getUnderlyingType);
3642
}
3743
else if(o is OdcmType)
@@ -105,6 +111,13 @@ private string GetJsonToObjCExpressionConversion(string expr,OdcmProperty prop)
105111
expr);
106112
}
107113

114+
if(GetObjCTypeIdentifier(prop,true)=="MSTimeOfDay")
115+
{
116+
return String.Format(@"[{0} ms_timeFromString: {1}]",
117+
GetObjCTypeIdentifier(prop,true),
118+
expr);
119+
}
120+
108121
if(prop.IsEnum())
109122
{
110123
return String.Format(
@@ -129,6 +142,17 @@ private string GetJsonToObjCExpressionConversion(string expr,OdcmProperty prop)
129142
type.GetNSNumberValueMethod());
130143
}
131144

145+
private string GetObjCTypeIdentifierCollection(OdcmProperty prop)
146+
{
147+
var propName = GetObjCTypeIdentifier(prop,true);
148+
if (prop.IsComplexCollectionOpenType(model))
149+
{
150+
propName = propName.RemoveFromEnd("Collection");
151+
}
152+
153+
return propName;
154+
}
155+
132156
private string GetHydratedPropertyFromDictionary(OdcmProperty prop)
133157
{
134158
return GetJsonToObjCExpressionConversion(String.Format(@"self.dictionary[@""{0}""]",prop.Name),prop);
@@ -191,8 +215,39 @@ private string GetHydratedIVarFromDictionary(OdcmProperty prop)
191215
GetJsonToObjCExpressionConversion(prop.Projection.Type.Name.ToLowerFirstChar(), prop));
192216
}
193217

218+
if(prop.IsComplexCollectionOpenType(model))
219+
{
220+
return String.Format(@"
221+
NSMutableDictionary *{0}Result = [[NSMutableDictionary alloc] init];
222+
NSDictionary *{0} = self.dictionary[@""{1}""];
223+
224+
if ([{0} isKindOfClass:[NSDictionary class]]){{
225+
[{0} enumerateKeysAndObjectsUsingBlock:^(NSString* key, id value, BOOL* stop){{
226+
[{0}Result setValue:{3} forKey:key];
227+
}}];
228+
}}
229+
230+
_{0} = {0}Result;
231+
",
232+
GetObjCProperty(prop),
233+
prop.Name,
234+
prop.Projection.Type.Name.ToLowerFirstChar().RemoveFromEnd("Collection"),
235+
GetJsonToObjCExpressionConversionOpenType(prop));
236+
}
237+
194238
return String.Format(@"_{0} = {1};",GetObjCProperty(prop),GetHydratedPropertyFromDictionary(prop));
239+
}
240+
241+
private string GetJsonToObjCExpressionConversionOpenType(OdcmProperty prop)
242+
{
243+
string objCIdentifierType = GetObjCTypeIdentifierCollection(prop);
244+
245+
if(model.GetComplexTypes().Any(complexType => objCIdentifierType.Equals(writer.GetPrefix() + complexType.Name.ToUpperFirstChar())))
246+
{
247+
return String.Format("[[{0} alloc] initWithDictionary:value]", objCIdentifierType);
248+
}
195249

250+
return "value";
196251
}
197252

198253
public string GetFunctionParameterDictionary(List<OdcmParameter> parameters)

Templates/ObjC/Models/EntityType.h.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ if (baseEntity == null)
1818
{
1919
#>
2020
@property (nullable, nonatomic, setter=setODataType:, getter=oDataType) NSString *oDataType;
21+
@property (nullable, nonatomic, setter=setODataEtag:, getter=oDataEtag) NSString *oDataEtag;
2122
<#
2223
}
2324

Templates/ObjC/Models/EntityType.m.tt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ if (entity.Base == null)
5757
{
5858
self.dictionary[@"@odata.type"] = val;
5959
}
60+
- (NSString*) oDataEtag
61+
{
62+
return self.dictionary[@"@odata.etag"];
63+
}
64+
- (void) setODataEtag: (NSString*) val
65+
{
66+
self.dictionary[@"@odata.etag"] = val;
67+
}
6068
<#
6169
}
6270

Templates/ObjC/Models/Models.h.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<#@ include file="SharedObjC.template.tt"#>
44

55
#import "MSDate.h"
6+
#import "MSTimeOfDay.h"
67
<#
78
var enums = model.GetEnumTypes();
89
foreach(var e in enums)

Templates/ObjC/Requests/ODataEntities.h.tt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,6 @@ foreach(var et in entityTypes)
9393

9494
foreach(var complexType in model.GetComplexTypes())
9595
{
96-
foreach(var prop in complexType.GetProperties().Where(prop => prop.IsCollection && !(prop.Projection.Type is OdcmPrimitiveType) && !prop.IsEnum() && !prop.LongDescriptionContains("ignorableCollection")))
97-
{
98-
var baseName = writer.GetPrefix() + prop.Class.Name.ToUpperFirstChar() + prop.Name.ToUpperFirstChar();
99-
if(!prop.IsReference())
100-
{
101-
classes.Add("#import \"" + baseName + "CollectionRequestBuilder.h\"");
102-
classes.Add("#import \"" + baseName + "CollectionRequest.h\"");
103-
}
104-
else
105-
{
106-
classes.Add("#import \"" + baseName + "CollectionWithReferencesRequestBuilder.h\"");
107-
classes.Add("#import \"" + baseName + "CollectionWithReferencesRequest.h\"");
108-
classes.Add("#import \"" + baseName + "CollectionReferenceRequestBuilder.h\"");
109-
classes.Add("#import \"" + baseName + "CollectionReferenceRequest.h\"");
110-
}
111-
}
11296
foreach(var streamProperty in complexType.GetProperties(typeName:"Stream"))
11397
{
11498
classes.Add("#import \"" + writer.GetPrefix() + streamProperty.Class.Name.ToUpperFirstChar() + streamProperty.Name.ToUpperFirstChar() + "Request.h\"");

src/GraphODataTemplateWriter/CodeHelpers/ObjC/TypeHelperObjC.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,18 @@ public static string GetTypeString(this OdcmType type)
108108
return "NSDate";
109109
case "Date":
110110
return "MSDate";
111+
case "TimeOfDay":
112+
return "MSTimeOfDay";
111113
case "Binary":
112114
return "NSString";
113115
case "Boolean":
114116
return "BOOL";
115117
case "Stream":
116118
return "NSStream";
119+
case "Duration":
120+
return "Duration";
121+
case "NSDictionary":
122+
return "NSDictionary";
117123
default:
118124
return Prefix + type.Name.ToUpperFirstChar();
119125
}
@@ -128,7 +134,7 @@ public static bool IsComplex(this OdcmType type)
128134
{
129135
string t = GetTypeString(type);
130136
return
131-
!(t.Contains("int") || t == "BOOL" || t == "Byte" || t == "CGFloat");
137+
!(t == "int32_t" || t == "int64_t" || t == "int16_t" || t == "BOOL" || t == "Byte" || t == "CGFloat");
132138
}
133139

134140
public static bool IsComplex(this OdcmProperty property)
@@ -183,6 +189,12 @@ public static bool IsSystem(this OdcmType type)
183189
return (t.Contains("int") || t == "BOOL" || t == "Byte" || t == "NSString" || t == "NSDate" || t == "NSStream" || t == "CGFloat");
184190
}
185191

192+
public static bool IsComplexCollectionOpenType(this OdcmProperty property, OdcmModel model)
193+
{
194+
return property.IsComplex() && property.Projection.Type.Name.ToLower().EndsWith("collection") &&
195+
model.GetComplexTypes().Any(complexType => complexType.Name.Equals(property.Projection.Type.Name) && complexType.IsOpen);
196+
}
197+
186198
public static bool IsDate(this OdcmProperty prop)
187199
{
188200
return prop.Projection.Type.IsDate();

src/GraphODataTemplateWriter/Extensions/StringExtensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,21 @@ public static string ToUnderscore(this string input)
7272
return Inflector.Inflector.Underscore(input);
7373
}
7474

75+
public static string RemoveFromEnd(this string input, string suffix)
76+
{
77+
if (input.EndsWith(suffix))
78+
{
79+
return input.Substring(0, input.Length - suffix.Length);
80+
}
81+
else
82+
{
83+
return input;
84+
}
85+
}
86+
87+
public static bool Equals(this string input, string compareWith)
88+
{
89+
return input.Equals(compareWith);
90+
}
7591
}
7692
}

0 commit comments

Comments
 (0)