Skip to content

Commit aa42613

Browse files
committed
Fix json-to-objc conversion, reclassify enum as Object type, code cleanup for clarity
1 parent e8574f4 commit aa42613

File tree

4 files changed

+48
-89
lines changed

4 files changed

+48
-89
lines changed

Templates/ObjC/Base/SharedObjC.template.tt

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,19 @@ private string GetObjCPropertySetter(OdcmProperty prop)
8484
return "set" + GetObjCProperty(prop).ToUpperFirstChar();
8585
}
8686

87-
private bool IsNSJSONSerializable(OdcmType type)
87+
private bool IsObjCTypeSameAsJsonType(OdcmType type)
8888
{
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.
8992
string ts=GetObjCTypeIdentifier(type);
90-
return ts=="NSString" || ts=="NSNumber" || ts=="NSArray" || ts=="NSDictionary" || ts=="NSNull";
93+
return ts=="NSString";
9194
}
9295

93-
private string GetHydratedPropertyFromNSJSONSerializableExpression(string expr,OdcmProperty prop)
96+
private string GetJsonToObjCExpressionConversion(string expr,OdcmProperty prop)
9497
{
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.
95100
if(GetObjCTypeIdentifier(prop,true)=="NSDate")
96101
{
97102
return String.Format(@"[NSDate ms_dateFromString: {0}]",expr);
@@ -104,8 +109,8 @@ private string GetHydratedPropertyFromNSJSONSerializableExpression(string expr,O
104109
expr,
105110
GetObjCTypeIdentifier(prop,true));
106111
}
107-
//Complex catch-all
108-
if(prop.IsComplex())
112+
113+
if(prop.Type is OdcmEntityClass || prop.Type is OdcmMediaClass || prop.Type is OdcmComplexClass)
109114
{
110115
return String.Format(
111116
@"[[{0} alloc] initWithDictionary: {1} ]",
@@ -121,39 +126,42 @@ private string GetHydratedPropertyFromNSJSONSerializableExpression(string expr,O
121126

122127
private string GetHydratedPropertyFromDictionary(OdcmProperty prop)
123128
{
124-
return GetHydratedPropertyFromNSJSONSerializableExpression(String.Format(@"self.dictionary[@""{0}""]",prop.Name),prop);
129+
return GetJsonToObjCExpressionConversion(String.Format(@"self.dictionary[@""{0}""]",prop.Name),prop);
125130
}
126131

127-
private string SetNSJSONPermitedExpressionSerialized(OdcmType type,string val)
132+
private string GetObjCToJsonConvertibleExpressionConversion(OdcmType type, bool isCollection, string val)
128133
{
129-
//Fallback to complex entities that must support ms_toString
130-
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)
131147
{
132-
return val;
148+
return "@(" + val + ")";
133149
}
134150

135-
if(type is OdcmEnum)
136-
{
137-
return String.Format("[NSString stringWith{0}:{1}]",GetObjCTypeIdentifier(type),val);
138-
}
139-
140-
return "@(" + val + ")";
151+
return val;
141152
}
142153

143-
private string SetNSJSONPermitedExpressionSerialized(OdcmProperty prop,string val)
154+
private string GetObjCToJsonConvertibleExpressionConversion(OdcmProperty prop,string val)
144155
{
145-
//Fallback to complex entities that must support ms_toString
146-
if(prop.IsCollection)
147-
{
148-
return val;
149-
}
150-
151-
return SetNSJSONPermitedExpressionSerialized(prop.Type,val);
156+
return GetObjCToJsonConvertibleExpressionConversion(prop.Type, prop.IsCollection, val);
152157
}
153158

154-
private string SetDictionaryPropertySerialized(OdcmProperty prop, string val)
159+
private string SetDictionaryPropertyInJsonConvertibleForm(OdcmProperty prop, string val)
155160
{
156-
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));
157165
}
158166

159167
private string GetHydratedIVarFromDictionary(OdcmProperty prop)
@@ -176,7 +184,7 @@ private string GetHydratedIVarFromDictionary(OdcmProperty prop)
176184
",
177185
GetObjCProperty(prop),
178186
prop.Name,
179-
GetObjCProperty(prop),
187+
GetJsonToObjCExpressionConversion(GetObjCProperty(prop), prop),
180188
GetStaticCollectionObject());
181189
}
182190

@@ -201,7 +209,7 @@ public string GetFunctionParameterDictionary(List<OdcmParameter> parameters)
201209
{
202210
result.AppendFormat("[{0}Object getNSJsonSerializationCompatibleValue:{1}],",
203211
writer.GetStaticCodePrefix(),
204-
SetNSJSONPermitedExpressionSerialized(param.Type,"_"+param.Name.ToLowerFirstChar()));
212+
GetObjCToJsonConvertibleExpressionConversion(param.Type, param.IsCollection, "_"+param.Name.ToLowerFirstChar()));
205213

206214
result.AppendFormat("@\"{0}\",", param.Name);
207215
}
@@ -239,7 +247,7 @@ private void PropertyGetterImplementation(OdcmProperty prop)
239247
- (<#=GetObjCTypeForVarDeclaration(prop)#>) <#=GetObjCPropertyGetter(prop)#>
240248
{
241249
<#+
242-
if(IsNSJSONSerializable(prop.Type))
250+
if(IsObjCTypeSameAsJsonType(prop.Type))
243251
{
244252
#>
245253
return self.dictionary[@"<#=prop.Name#>"];
@@ -278,7 +286,7 @@ private void PropertySetterImplementation(OdcmProperty prop)
278286
- (void) <#=GetObjCPropertySetter(prop)#>: (<#=GetObjCTypeForVarDeclaration(prop)#>) val
279287
{
280288
<#+
281-
if(IsNSJSONSerializable(prop.Type))
289+
if(IsObjCTypeSameAsJsonType(prop.Type))
282290
{
283291
#>
284292
self.dictionary[@"<#=prop.Name#>"] = val;
@@ -288,7 +296,7 @@ private void PropertySetterImplementation(OdcmProperty prop)
288296
{
289297
#>
290298
_<#=GetObjCProperty(prop)#> = val;
291-
<#=SetDictionaryPropertySerialized(prop,"val")#>
299+
<#=SetDictionaryPropertyInJsonConvertibleForm(prop,"val")#>
292300
<#+
293301
}
294302
#>

Templates/ObjC/Requests/MethodRequest.m.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ if (func.Parameters.Count > 0)
145145
{
146146
#>
147147
[self.options addObject:[[<#=writer.GetStaticCodePrefix()#>FunctionParameters alloc] initWithKey:@"<#=param.Name#>"
148-
value:[<#=writer.GetStaticCodePrefix()#>Object getNSJsonSerializationCompatibleValue:<#=SetNSJSONPermitedExpressionSerialized(param.Type,"_"+param.Name.ToLowerFirstChar())#>]]];
148+
value:[<#=writer.GetStaticCodePrefix()#>Object getNSJsonSerializationCompatibleValue:<#=GetObjCToJsonConvertibleExpressionConversion(param.Type, param.IsCollection, "_"+param.Name.ToLowerFirstChar())#>]]];
149149
<#
150150
}
151151
#>

src/GraphODataTemplateWriter/CodeHelpers/ObjC/CodeWriterObjC.cs

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,7 @@ public string GetMethodDoc(string name, List<OdcmProperty> parameters)
9494
{
9595
return "";
9696
}
97-
98-
public string GetParams(IEnumerable<OdcmProperty> parameters)
99-
{
100-
string param = "With";
101-
102-
foreach (var p in parameters)
103-
{
104-
if (param == "With")
105-
{
106-
param += string.Format("{0}:({2} {3}) {1}", char.ToUpper(p.Name[0]) + p.Name.Substring(1)
107-
, p.Name.ToLowerFirstChar(), p.Type.GetFullType(), (p.Type.IsComplex() ? "*" : ""));
108-
}
109-
else
110-
{
111-
param += string.Format("{0}:({1} {2}) {1}", p.Name.ToLowerFirstChar(), p.Type.GetFullType(), (p.Type.IsComplex() ? "*" : ""));
112-
}
113-
}
114-
return param;
115-
}
116-
97+
11798
public string GetParamsForRaw(IEnumerable<string> parameters)
11899
{
119100
string param = "With";
@@ -129,30 +110,11 @@ public string GetParamsForRaw(IEnumerable<string> parameters)
129110
return param;
130111
}
131112

132-
public string GetParam(OdcmProperty type)
133-
{
134-
if (type.IsComplex())
135-
{
136-
return type.IsSystem() ? string.Empty : type.GetTypeString() + " *" + type.Name.ToLowerFirstChar();
137-
}
138-
139-
return type.GetTypeString() + " " + type.Name;
140-
}
141-
142113
public string GetParamRaw(string type)
143114
{
144115
return "NSString *" + type.ToLowerFirstChar();
145116
}
146117

147-
public string GetType(OdcmType type)
148-
{
149-
if (type.IsComplex()) {
150-
return type.IsSystem() ? type.GetTypeString() : type.GetTypeString() + " *";
151-
}
152-
153-
return type.GetTypeString();
154-
}
155-
156118
public string GetImportsClass(IEnumerable<OdcmProperty> references, IEnumerable<string> extraImports = null, IEnumerable<string> extraClasses = null)
157119
{
158120
if (references != null && references.Any())
@@ -211,11 +173,6 @@ public string GetImportsClass(IEnumerable<OdcmProperty> references, IEnumerable<
211173
return "";
212174
}
213175

214-
public string GetClass(OdcmProperty type)
215-
{
216-
return type.IsComplex() ? string.Format("[{0}{1} class]", this.GetPrefix(), type.GetTypeString()) : "nil";
217-
}
218-
219176
public string GetParametersToJsonRaw(IEnumerable<string> parameters)
220177
{
221178
if (!parameters.Any()) { return new StringBuilder().AppendLine().ToString(); }
@@ -311,16 +268,6 @@ public string GetParamsString(IEnumerable<OdcmParameter> parameters)
311268
return param;
312269
}
313270

314-
public string GetParamString(OdcmType p)
315-
{
316-
p.GetFullType();
317-
if (p.IsComplex())
318-
{
319-
return p.IsSystem() ? string.Empty : p.GetTypeString() + " *" + p.Name.ToLowerFirstChar();
320-
}
321-
return p.GetTypeString() + " " + p.Name;
322-
}
323-
324271
public string GetNetworkCompletionBlock(string parameterType, string parameterName)
325272
{
326273
if (!string.IsNullOrEmpty(parameterType) && !string.IsNullOrEmpty(parameterName))

src/GraphODataTemplateWriter/CodeHelpers/ObjC/TypeHelperObjC.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,19 @@ public static bool IsComplex(this OdcmType type)
127127
{
128128
string t = GetTypeString(type);
129129
return
130-
!(t.Contains("int") || t == "BOOL" || t == "Byte" || t == "CGFloat" ||
131-
type is OdcmEnum);
130+
!(t.Contains("int") || t == "BOOL" || t == "Byte" || t == "CGFloat");
132131
}
133132

134-
public static bool IsComplex(this OdcmProperty property)
133+
public static bool IsComplex(this OdcmProperty property)
135134
{
136135
return property.Type.IsComplex();
137136
}
138137

138+
public static bool IsPrimitive(this OdcmType type)
139+
{
140+
return !type.IsComplex();
141+
}
142+
139143
public static string ToSetterTypeString(this OdcmProperty property)
140144
{
141145
return string.Format("{0} {1}", property.GetFullType(), (property.IsComplex() ? "*" : string.Empty));

0 commit comments

Comments
 (0)