Skip to content

Commit c79fe44

Browse files
authored
Merge pull request #46 from dianambb/HandleOpenTypes
Support for OpenType JSON objects. Checked this should be a [String: AnyObject], so dictionary is sound.
2 parents 03a8bfe + 588f0de commit c79fe44

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

Templates/ObjC/Base/SharedObjC.template.tt

Lines changed: 48 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)
@@ -136,6 +142,17 @@ private string GetJsonToObjCExpressionConversion(string expr,OdcmProperty prop)
136142
type.GetNSNumberValueMethod());
137143
}
138144

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+
139156
private string GetHydratedPropertyFromDictionary(OdcmProperty prop)
140157
{
141158
return GetJsonToObjCExpressionConversion(String.Format(@"self.dictionary[@""{0}""]",prop.Name),prop);
@@ -198,8 +215,39 @@ private string GetHydratedIVarFromDictionary(OdcmProperty prop)
198215
GetJsonToObjCExpressionConversion(prop.Projection.Type.Name.ToLowerFirstChar(), prop));
199216
}
200217

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+
201238
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+
}
202249

250+
return "value";
203251
}
204252

205253
public string GetFunctionParameterDictionary(List<OdcmParameter> parameters)

src/GraphODataTemplateWriter/CodeHelpers/ObjC/TypeHelperObjC.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ public static bool IsSystem(this OdcmType type)
189189
return (t.Contains("int") || t == "BOOL" || t == "Byte" || t == "NSString" || t == "NSDate" || t == "NSStream" || t == "CGFloat");
190190
}
191191

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+
192198
public static bool IsDate(this OdcmProperty prop)
193199
{
194200
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)