Skip to content

Commit 7d56bab

Browse files
committed
Merge pull request #6 from kevklam/master
New collection & enum patterns
2 parents 4b9d927 + a8b648a commit 7d56bab

File tree

16 files changed

+247
-361
lines changed

16 files changed

+247
-361
lines changed

Templates/ObjC/Base/SharedObjC.template.tt

Lines changed: 49 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,11 @@ private string GetObjCTypeIdentifier(OdcmObject o, bool getUnderlyingType=false)
2929
OdcmProperty prop=(OdcmProperty)o;
3030
if(prop.IsCollection && !getUnderlyingType)
3131
{
32-
if(IsNSJSONSerializable(prop.Type))
33-
{
34-
return GetStaticCollectionObject();
35-
}
36-
else
37-
{
38-
return prop.Type.GetCollectionTypeString();
39-
}
32+
return @"NSArray";
4033
}
4134

4235
return GetObjCTypeIdentifier(prop.Type,getUnderlyingType);
4336
}
44-
else if(o is OdcmMethod)
45-
{
46-
OdcmMethod method = (OdcmMethod)o;
47-
if (method.IsCollection && !getUnderlyingType)
48-
{
49-
if (IsNSJSONSerializable(method.ReturnType))
50-
{
51-
return GetStaticCollectionObject();
52-
}
53-
else
54-
{
55-
return method.ReturnType.GetCollectionTypeString();
56-
}
57-
}
58-
59-
return GetObjCTypeIdentifier(method.ReturnType, getUnderlyingType);
60-
}
6137
else if(o is OdcmType)
6238
{
6339
return ((OdcmType)o).GetTypeString();
@@ -108,14 +84,19 @@ private string GetObjCPropertySetter(OdcmProperty prop)
10884
return "set" + GetObjCProperty(prop).ToUpperFirstChar();
10985
}
11086

111-
private bool IsNSJSONSerializable(OdcmType type)
87+
private bool IsObjCTypeSameAsJsonType(OdcmType type)
11288
{
89+
// TODO: To be more correct, we should add a GetJsonType() method
90+
// and return (GetObjCType(type) == GetJsonType(type)). Practically speaking though,
91+
// only Edm.String has the same friendly ObjC type and JSON serializer type.
11392
string ts=GetObjCTypeIdentifier(type);
114-
return ts=="NSString" || ts=="NSNumber" || ts=="NSArray" || ts=="NSDictionary" || ts=="NSNull";
93+
return ts=="NSString";
11594
}
11695

117-
private string GetHydratedPropertyFromNSJSONSerializableExpression(string expr,OdcmProperty prop)
96+
private string GetJsonToObjCExpressionConversion(string expr,OdcmProperty prop)
11897
{
98+
// This generates a wrapper around a provided JSON-typed expression (i.e. types obtained from NSJSONSerialization)
99+
// that converts it to a user-friendly ObjC type i.e. generated Entity/enum class etc.
119100
if(GetObjCTypeIdentifier(prop,true)=="NSDate")
120101
{
121102
return String.Format(@"[NSDate ms_dateFromString: {0}]",expr);
@@ -128,11 +109,11 @@ private string GetHydratedPropertyFromNSJSONSerializableExpression(string expr,O
128109
expr,
129110
GetObjCTypeIdentifier(prop,true));
130111
}
131-
//Complex catch-all
132-
if(prop.IsComplex())
112+
113+
if(prop.Type is OdcmEntityClass || prop.Type is OdcmMediaClass || prop.Type is OdcmComplexClass)
133114
{
134115
return String.Format(
135-
@"[[{0} alloc] initWithDictionary: {1} ]",
116+
@"[[{0} alloc] initWithDictionary: {1}]",
136117
GetObjCTypeIdentifier(prop,true),
137118
expr);
138119
}
@@ -143,94 +124,66 @@ private string GetHydratedPropertyFromNSJSONSerializableExpression(string expr,O
143124
prop.Type.GetNSNumberValueMethod());
144125
}
145126

146-
private string GetHydratedPropertyFromNSJSONSerializableExpression(string expr,OdcmMethod method)
147-
{
148-
if(GetObjCTypeIdentifier(method,true)=="NSDate")
149-
{
150-
return String.Format(@"[NSDate ms_dateFromString: {0}]",expr);
151-
}
152-
153-
if(method.IsEnum())
154-
{
155-
return String.Format(
156-
@"[{0} to{1}]",
157-
expr,
158-
GetObjCTypeIdentifier(method,true));
159-
}
160-
//Complex catch-all
161-
if(method.IsComplex())
162-
{
163-
return String.Format(
164-
@"[[{0} alloc] initWithDictionary: {1} ]",
165-
GetObjCTypeIdentifier(method,true),
166-
expr);
167-
}
168-
169-
return String.Format(
170-
@"[{0} {1}]",
171-
expr,
172-
method.ReturnType.GetNSNumberValueMethod());
173-
}
174-
175127
private string GetHydratedPropertyFromDictionary(OdcmProperty prop)
176128
{
177-
return GetHydratedPropertyFromNSJSONSerializableExpression(String.Format(@"self.dictionary[@""{0}""]",prop.Name),prop);
129+
return GetJsonToObjCExpressionConversion(String.Format(@"self.dictionary[@""{0}""]",prop.Name),prop);
178130
}
179131

180-
private string SetNSJSONPermitedExpressionSerialized(OdcmType type,string val)
132+
private string GetObjCToJsonConvertibleExpressionConversion(OdcmType type, bool isCollection, string val)
181133
{
182-
//Fallback to complex entities that must support ms_toString
183-
if(type.IsComplex())
134+
// This generates a wrapper around a provided friendly-ObjC-typed expression that converts it
135+
// to an expression that can be passed into [MSObject getNSJsonSerializationCompatibleValue].
136+
//
137+
// The categories of Obj-C property types are as follows:
138+
// Entity/Complex generated classes
139+
// Collections (NSArrays)
140+
// Enum generated classes
141+
// System object-types (NSString, NSDate)
142+
// System primitives (int types, BOOL)
143+
//
144+
// All of these implement the methods (some via extensions) dictionary/arrayWithItem or ms_toString
145+
// except for system primitives, which we need to box here using @().
146+
if (type.IsPrimitive() && !isCollection)
184147
{
185-
return val;
148+
return "@(" + val + ")";
186149
}
187150

188-
if(type is OdcmEnum)
189-
{
190-
return String.Format("[NSString stringWith{0}:{1}]",GetObjCTypeIdentifier(type),val);
191-
}
192-
193-
return "@(" + val + ")";
151+
return val;
194152
}
195153

196-
private string SetNSJSONPermitedExpressionSerialized(OdcmProperty prop,string val)
154+
private string GetObjCToJsonConvertibleExpressionConversion(OdcmProperty prop,string val)
197155
{
198-
//Fallback to complex entities that must support ms_toString
199-
if(prop.IsCollection)
200-
{
201-
return val;
202-
}
203-
204-
return SetNSJSONPermitedExpressionSerialized(prop.Type,val);
156+
return GetObjCToJsonConvertibleExpressionConversion(prop.Type, prop.IsCollection, val);
205157
}
206158

207-
private string SetDictionaryPropertySerialized(OdcmProperty prop, string val)
159+
private string SetDictionaryPropertyInJsonConvertibleForm(OdcmProperty prop, string val)
208160
{
209-
return String.Format(@"self.dictionary[@""{0}""] = {1};",prop.Name,SetNSJSONPermitedExpressionSerialized(prop,val));
161+
// Dictionary entries can either be directly NSJSON-typed, or convertible to
162+
// a NSJSON type via [MSObject getNSJsonSerializationCompatibleValue].
163+
//
164+
return String.Format(@"self.dictionary[@""{0}""] = {1};",prop.Name,GetObjCToJsonConvertibleExpressionConversion(prop,val));
210165
}
211166

212167
private string GetHydratedIVarFromDictionary(OdcmProperty prop)
213168
{
214169
if(prop.IsCollection)
215170
{
216171
return String.Format(@"
217-
NSMutableArray *{0}Collection = [NSMutableArray array];
218-
NSArray *{0}s = self.dictionary[@""{1}""];
172+
NSMutableArray *{0}Result = [NSMutableArray array];
173+
NSArray *{0} = self.dictionary[@""{1}""];
219174

220-
if ([{0}s isKindOfClass:[NSArray class]]){{
221-
for (id {0} in {0}s){{
222-
[{0}Collection addObject:{2}];
223-
}}
175+
if ([{0} isKindOfClass:[NSArray class]]){{
176+
for (id {2} in {0}){{
177+
[{0}Result addObject:{3}];
178+
}}
224179
}}
225180

226-
if ([{0}Collection count] > 0){{
227-
_{0} = [[{3} alloc] initWithArray:{0}Collection nextLink:self.dictionary[@""@nextLink""] additionalData:self.dictionary];
228-
}}
181+
_{0} = {0}Result;
229182
",
230183
GetObjCProperty(prop),
231184
prop.Name,
232-
GetObjCProperty(prop),
233-
GetObjCTypeIdentifier(prop));
185+
prop.Type.Name.ToLowerFirstChar(),
186+
GetJsonToObjCExpressionConversion(prop.Type.Name.ToLowerFirstChar(), prop));
234187
}
235188

236189
return String.Format(@"_{0} = {1};",GetObjCProperty(prop),GetHydratedPropertyFromDictionary(prop));
@@ -254,7 +207,7 @@ public string GetFunctionParameterDictionary(List<OdcmParameter> parameters)
254207
{
255208
result.AppendFormat("[{0}Object getNSJsonSerializationCompatibleValue:{1}],",
256209
writer.GetStaticCodePrefix(),
257-
SetNSJSONPermitedExpressionSerialized(param.Type,"_"+param.Name.ToLowerFirstChar()));
210+
GetObjCToJsonConvertibleExpressionConversion(param.Type, param.IsCollection, "_"+param.Name.ToLowerFirstChar()));
258211

259212
result.AppendFormat("@\"{0}\",", param.Name);
260213
}
@@ -292,7 +245,7 @@ private void PropertyGetterImplementation(OdcmProperty prop)
292245
- (<#=GetObjCTypeForVarDeclaration(prop)#>) <#=GetObjCPropertyGetter(prop)#>
293246
{
294247
<#+
295-
if(IsNSJSONSerializable(prop.Type))
248+
if(IsObjCTypeSameAsJsonType(prop.Type))
296249
{
297250
#>
298251
return self.dictionary[@"<#=prop.Name#>"];
@@ -331,7 +284,7 @@ private void PropertySetterImplementation(OdcmProperty prop)
331284
- (void) <#=GetObjCPropertySetter(prop)#>: (<#=GetObjCTypeForVarDeclaration(prop)#>) val
332285
{
333286
<#+
334-
if(IsNSJSONSerializable(prop.Type))
287+
if(IsObjCTypeSameAsJsonType(prop.Type))
335288
{
336289
#>
337290
self.dictionary[@"<#=prop.Name#>"] = val;
@@ -341,7 +294,7 @@ private void PropertySetterImplementation(OdcmProperty prop)
341294
{
342295
#>
343296
_<#=GetObjCProperty(prop)#> = val;
344-
<#=SetDictionaryPropertySerialized(prop,"val")#>
297+
<#=SetDictionaryPropertyInJsonConvertibleForm(prop,"val")#>
345298
<#+
346299
}
347300
#>

Templates/ObjC/Models/CollectionType.h.tt

Lines changed: 0 additions & 43 deletions
This file was deleted.

Templates/ObjC/Models/CollectionType.m.tt

Lines changed: 0 additions & 69 deletions
This file was deleted.

Templates/ObjC/Models/ComplexType.h.tt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
<#
55
var entity = host.CurrentType.AsOdcmClass();
66
var baseEntity = entity.Base;
7-
87
#>
9-
<#=writer.GetImportsClass(entity.Properties,entity.Properties.Where(prop => prop.IsCollection).Select(prop => GetObjCTypeIdentifier(prop)).Distinct())#>
8+
9+
<#=writer.GetImportsClass(entity.Properties)#>
1010

1111
#import "<#=GetObjCTypeIdentifier(baseEntity)#>.h"
12-
#import "<#=GetStaticCollectionObject()#>.h"
1312

1413
@interface <#=GetObjCTypeIdentifier(entity)#> : <#=GetObjCTypeIdentifier(baseEntity)#>
1514

Templates/ObjC/Models/EntityType.h.tt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
var entity = host.CurrentType.AsOdcmClass();
66
var baseEntity = entity.Base;
77

8+
IEnumerable<string> extraClasses = null;
89
#>
9-
<#=writer.GetImportsClass(entity.Properties,entity.Properties.Where(prop => prop.IsCollection).Select(prop => GetObjCTypeIdentifier(prop)).Distinct())#>
10+
<#=writer.GetImportsClass(entity.Properties, extraClasses:extraClasses)#>
1011

1112
#import "<#=GetObjCTypeIdentifier(baseEntity)#>.h"
12-
#import "<#=GetStaticCollectionObject()#>.h"
1313

1414
@interface <#=GetObjCTypeIdentifier(entity)#> : <#=GetObjCTypeIdentifier(baseEntity)#>
1515

@@ -28,7 +28,7 @@ foreach(var prop in entity.Properties.Where(prop => GetObjCTypeIdentifier(prop)!
2828
<#
2929
}
3030

31-
foreach (var prop in entity.Properties.Where(prop => prop.IsCollection && !prop.LongDescriptionContains("not-enumerable")))
31+
foreach (var prop in entity.Properties.Where(prop => prop.IsCollection && prop.LongDescriptionContains("enumerable")))
3232
{
3333
#>
3434
- (<#=GetObjCTypeForVarDeclaration(prop,true)#>) <#=GetObjCProperty(prop)#>:(NSInteger)index;

0 commit comments

Comments
 (0)