Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion Templates/ObjC/Base/SharedObjC.template.tt
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,29 @@ private string GetObjCTypeIdentifier(OdcmObject o, bool getUnderlyingType=false)
}
else
{
return GetObjCTypeIdentifier(prop.Type) + "Collection";
return prop.Type.GetCollectionTypeString();
}
}

return GetObjCTypeIdentifier(prop.Type,getUnderlyingType);
}
else if(o is OdcmMethod)
{
OdcmMethod method = (OdcmMethod)o;
if (method.IsCollection && !getUnderlyingType)
{
if (IsNSJSONSerializable(method.ReturnType))
{
return GetStaticCollectionObject();
}
else
{
return method.ReturnType.GetCollectionTypeString();
}
}

return GetObjCTypeIdentifier(method.ReturnType, getUnderlyingType);
}
else if(o is OdcmType)
{
return ((OdcmType)o).GetTypeString();
Expand Down Expand Up @@ -126,6 +143,35 @@ private string GetHydratedPropertyFromNSJSONSerializableExpression(string expr,O
prop.Type.GetNSNumberValueMethod());
}

private string GetHydratedPropertyFromNSJSONSerializableExpression(string expr,OdcmMethod method)
{
if(GetObjCTypeIdentifier(method,true)=="NSDate")
{
return String.Format(@"[NSDate ms_dateFromString: {0}]",expr);
}

if(method.IsEnum())
{
return String.Format(
@"[{0} to{1}]",
expr,
GetObjCTypeIdentifier(method,true));
}
//Complex catch-all
if(method.IsComplex())
{
return String.Format(
@"[[{0} alloc] initWithDictionary: {1} ]",
GetObjCTypeIdentifier(method,true),
expr);
}

return String.Format(
@"[{0} {1}]",
expr,
method.ReturnType.GetNSNumberValueMethod());
}

private string GetHydratedPropertyFromDictionary(OdcmProperty prop)
{
return GetHydratedPropertyFromNSJSONSerializableExpression(String.Format(@"self.dictionary[@""{0}""]",prop.Name),prop);
Expand Down
19 changes: 14 additions & 5 deletions Templates/ObjC/Models/CollectionType.h.tt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
<#@ include file="SharedObjC.template.tt"#>
<#
var prop = host.CurrentType.AsOdcmProperty();
var innerEntity = prop.Type;
var method = host.CurrentType.AsOdcmMethod();
OdcmType innerEntity = null;
if (prop != null)
{
innerEntity = prop.Type;
}
else if (method != null)
{
innerEntity = method.ReturnType;
}
#>

#import "<#=GetStaticCollectionObject()#>.h"
Expand All @@ -25,10 +34,10 @@ if(!innerEntity.IsSystem())
}
#>

@interface <#=GetObjCTypeIdentifier(innerEntity)#>Collection : <#=GetStaticCollectionObject()#>
@interface <#=innerEntity.GetCollectionTypeString()#> : <#=GetStaticCollectionObject()#>

+ (<#=GetObjCTypeIdentifier(innerEntity)#>Collection *) fromMSCollection:(<#=GetStaticCollectionObject()#> *)collection;
- (<#=GetObjCTypeForVarDeclaration(innerEntity)#>) objectAtIndexInCurrentPage:(NSUInteger)index;
@property(readonly) NSUInteger countInCurrentPage;
+ (<#=innerEntity.GetCollectionTypeString()#> *) fromMSCollection:(<#=GetStaticCollectionObject()#> *)collection;
- (<#=GetObjCTypeForVarDeclaration(innerEntity)#>) objectAtIndex:(NSUInteger)index;
@property(readonly) NSUInteger count;

@end
39 changes: 32 additions & 7 deletions Templates/ObjC/Models/CollectionType.m.tt
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,39 @@
<#@ include file="SharedObjC.template.tt"#>
<#
var prop = host.CurrentType.AsOdcmProperty();
var innerEntity = prop.Type;
var method = host.CurrentType.AsOdcmMethod();
OdcmType innerEntity = null;
if (prop != null)
{
innerEntity = prop.Type;
}
else if (method != null)
{
innerEntity = method.ReturnType;
}
#>

#import "<#=GetObjCTypeIdentifier(innerEntity)#>Collection.h"
#import "<#=innerEntity.GetCollectionTypeString()#>.h"
<#
if (!innerEntity.IsSystem() && innerEntity.IsComplex())
{
#>
#import "<#=GetObjCTypeIdentifier(innerEntity)#>.h"
<#
}
#>

@implementation <#=GetObjCTypeIdentifier(innerEntity)#>Collection
@implementation <#=innerEntity.GetCollectionTypeString()#>

+ (<#=GetObjCTypeIdentifier(innerEntity)#>Collection *) fromMSCollection:(<#=GetStaticCollectionObject()#> *)collection {
<#=GetObjCTypeIdentifier(innerEntity)#>Collection *col = [[<#=GetObjCTypeIdentifier(innerEntity)#>Collection alloc] init];
+ (<#=innerEntity.GetCollectionTypeString()#> *) fromMSCollection:(<#=GetStaticCollectionObject()#> *)collection {
<#=innerEntity.GetCollectionTypeString()#> *col = [[<#=innerEntity.GetCollectionTypeString()#> alloc] init];
col.value = collection.value;
col.nextLink = collection.nextLink;
col.additionalData = collection.additionalData;
return col;
}

- (<#=GetObjCTypeForVarDeclaration(innerEntity)#>) objectAtIndexInCurrentPage:(NSUInteger)index {
- (<#=GetObjCTypeForVarDeclaration(innerEntity)#>) objectAtIndex:(NSUInteger)index {
<#
if(IsNSJSONSerializable(innerEntity))
{
Expand All @@ -29,15 +45,24 @@ var innerEntity = prop.Type;
}
else
{
if (prop != null)
{
#>
return <#=GetHydratedPropertyFromNSJSONSerializableExpression("[super.value objectAtIndex:index]",prop)#>;
<#
}
else
{
#>
return <#=GetHydratedPropertyFromNSJSONSerializableExpression("[super.value objectAtIndex:index]",method)#>;
<#
}
}
#>

}

- (NSUInteger) countInCurrentPage {
- (NSUInteger) count {
return [super.value count];
}

Expand Down
5 changes: 0 additions & 5 deletions Templates/ObjC/Requests/EntityCollectionRequest.h.tt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ if (innerEntityType.Equals(propName))

#import "<#=writer.GetPrefix()#>Models.h"
#import "<#=writer.GetStaticCodePrefix()#>CollectionRequest.h"
<#
if(prop.IsReference()) {
#>
#import "<#=writer.GetStaticCodePrefix()#>CollectionReferences.h"
<# } #>

typedef void (^<#=innerEntityType#>CompletionHandler)(<#=innerEntityType#> *response, NSError *error);

Expand Down
8 changes: 4 additions & 4 deletions Templates/ObjC/Requests/EntityCollectionRequest.m.tt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ if (innerEntityType.Equals(propName))

@implementation <#=propNameRequest#>

<#
if(!collectionReference)
{
#>
- (MSURLSessionDataTask *)<#=GetObjCTypeIdentifier(prop)#>TaskWithRequest:(NSMutableURLRequest *)request
odObjectWithDictionary:(MSObject* (^)(NSDictionary *response))castBlock
completion:(void (^)(<#=GetObjCTypeForVarDeclaration(prop)#> response, NSError *error))completionHandler
Expand All @@ -43,10 +47,6 @@ if (innerEntityType.Equals(propName))
}];
}

<#
if(!collectionReference)
{
#>
- (NSMutableURLRequest *)get
{
return [self requestWithMethod:@"GET"
Expand Down
26 changes: 8 additions & 18 deletions Templates/ObjC/Requests/MethodRequest.h.tt
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,23 @@ else
returnType = "NSDictionary";
}

var superClass = writer.GetStaticCodePrefix() + ((func.IsCollection || func.LongDescriptionContains("specialCollection")) ? "CollectionRequest" : "Request");
var superClass = writer.GetStaticCodePrefix() + (func.IsCollection ? "CollectionRequest" : "Request");
var method = ((func.Verbs & OdcmAllowedVerbs.Get) != OdcmAllowedVerbs.Get) ? "POST" : "GET";
var isAsync = func.LongDescriptionContains("async");
var specialCollectionType = writer.GetPrefix() + func.Class.Name.ToUpperFirstChar() + func.Name.Substring(func.Name.IndexOf('.') + 1).ToUpperFirstChar() + "Collection";

if (func.LongDescriptionContains("specialCollection"))
{
#>
@class <#=writer.GetStaticCodePrefix()#>URLSessionDataTask, <#=specialCollectionType#>;
@class <#=writer.GetStaticCodePrefix()#>URLSessionDataTask;

#import "<#=superClass#>.h"
<#
}
else
if (func.ReturnType != null && func.IsCollection)
{
#>
@class <#=writer.GetStaticCodePrefix()#>URLSessionDataTask;
#import "<#=func.ReturnType.GetCollectionTypeString()#>.h"
<#
}
#>

#import "<#=superClass#>.h"

@interface <#=requestType#> : <#=superClass#>

@property (readonly) NSMutableURLRequest *mutableRequest;
Expand All @@ -51,15 +47,10 @@ if (func.Parameters.Count > 0)
}
string completionHandler;
string taskType = writer.GetStaticCodePrefix() + "URLSessionDataTask *";
if (func.IsCollection || func.LongDescriptionContains("specialCollection"))
if (func.IsCollection)
{
string collectionType = writer.GetStaticCodePrefix() + "Collection";
if (func.LongDescriptionContains("specialCollection"))
{
collectionType = specialCollectionType;
}
string collectionType = func.ReturnType.GetCollectionTypeString();
completionHandler = "(void (^)(" + collectionType + " *response, " + requestType + " *nextRequest, NSError *error))";

}
else if(isAsync)
{
Expand All @@ -68,7 +59,6 @@ else if(isAsync)
}
else
{

completionHandler = writer.GetNetworkCompletionBlock(returnType, "response");
}
if (!returnType.Contains("Stream"))
Expand Down
54 changes: 23 additions & 31 deletions Templates/ObjC/Requests/MethodRequest.m.tt
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,12 @@ else
var superClass = writer.GetStaticCodePrefix() + ((func.IsCollection) ? "CollectionRequest" : "Request");
var method = ((func.Verbs & OdcmAllowedVerbs.Get) != OdcmAllowedVerbs.Get) ? "POST" : "GET";
var isAsync = func.LongDescriptionContains("async");
var specialCollecitonName = writer.GetPrefix() + func.Class.Name.ToUpperFirstChar() + func.Name.Substring(func.Name.IndexOf('.') + 1).ToUpperFirstChar() + "Collection";

#>

#import "<#=writer.GetPrefix()#>ODataEntities.h"
#import "<#=writer.GetPrefix()#>Models.h"
#import "<#=writer.GetStaticCodePrefix()#>URLSessionDataTask.h"
<#
if (func.LongDescriptionContains("specialCollection"))
{
#>
#import "<#=specialCollecitonName#>.h"
<#
}
#>


<#
Expand Down Expand Up @@ -131,6 +122,22 @@ if (func.Parameters.Count > 0)
}
<#
}

if (func.IsCollection)
{
#>

- (MSURLSessionDataTask *)<#=func.ReturnType.GetCollectionTypeString()#>TaskWithRequest:(NSMutableURLRequest *)request
odObjectWithDictionary:(MSObject* (^)(NSDictionary *response))castBlock
completion:(void (^)(<#=func.ReturnType.GetCollectionTypeString()#> *response, NSError *error))completionHandler
{
return [self collectionTaskWithRequest: request odObjectWithDictionary:castBlock
completion:^(MSCollection* collectionResponse, NSError *error){
completionHandler([<#=func.ReturnType.GetCollectionTypeString()#> fromMSCollection:collectionResponse],error);
}];
}
<#
}
#>

- (NSMutableURLRequest *)mutableRequest
Expand Down Expand Up @@ -176,30 +183,16 @@ string completionBlock;
string completionHandler;
string taskMethod;
string taskType = writer.GetStaticCodePrefix() + "URLSessionDataTask *";
if (func.IsCollection || func.LongDescriptionContains("specialCollection"))
if (func.IsCollection)
{

if (func.LongDescriptionContains("specialCollection"))
{
completionHandler = "(void (^)(" + specialCollecitonName + " *response, " + requestType + " *nextRequest, NSError *error))";
}
else
{
completionHandler = "(void (^)(" + writer.GetStaticCodePrefix() +"Collection *response, " + requestType + " *nextRequest, NSError *error))";
}
completionHandler = "(void (^)(" + func.ReturnType.GetCollectionTypeString() + " *response, " + requestType + " *nextRequest, NSError *error))";

var stringBuilder = new StringBuilder();
var baseIndents = (" " + writer.GetStaticCodePrefix() + "URLSessionDataTask *task = [").ToSpaces();

stringBuilder.AppendLine("^(" + writer.GetStaticCodePrefix() + "Collection *collectionResponse, NSError *error){");
stringBuilder.AppendLine("^(" + func.ReturnType.GetCollectionTypeString() + " *collectionResponse, NSError *error){");
string returnCollectionName = "collectionResponse";
if (func.LongDescriptionContains("specialCollection"))
{
returnCollectionName = "collection";
stringBuilder.AppendLine(baseIndents + " " + specialCollecitonName + " *" + returnCollectionName +"= nil;");
stringBuilder.AppendLine(baseIndents + " if(collectionResponse){");
stringBuilder.AppendLine(baseIndents + " " + returnCollectionName + " = [[" + specialCollecitonName + " alloc] initWithCollection:collectionResponse];");
stringBuilder.AppendLine(baseIndents + " }");
}

stringBuilder.AppendLine(baseIndents + " if(!error && "+ returnCollectionName + ".nextLink && completionHandler){");
stringBuilder.AppendLine(baseIndents + " " + requestType + " *nextRequest = [[" + requestType + " alloc] initWithURL:" + returnCollectionName + ".nextLink");
stringBuilder.AppendLine(baseIndents + " options:nil");
Expand All @@ -212,8 +205,7 @@ if (func.IsCollection || func.LongDescriptionContains("specialCollection"))
stringBuilder.Append(baseIndents + "}");

completionBlock = stringBuilder.ToString();
taskMethod = "[self collectionTaskWithRequest:";

taskMethod = "[self " + func.ReturnType.GetCollectionTypeString() + "TaskWithRequest:";
}
else if(isAsync)
{
Expand All @@ -239,7 +231,7 @@ if (!returnType.Contains("Stream"))
<#=taskType#>task = <#=taskMethod#>self.mutableRequest
<#=indentSpace.Substring("odObjectWithDictionary".Length + 1)#>odObjectWithDictionary:^(id responseObject){
<#
if (returnType == writer.GetPrefix() + "String" || returnType == writer.GetStaticCodePrefix() + "Guid")
if (func.IsCollection || IsNSJSONSerializable(func.ReturnType))
{
#>
<#=indentSpace#> return [responseObject copy];
Expand Down
5 changes: 2 additions & 3 deletions src/GraphODataTemplateWriter/.config/ObjCSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@

{ "Template": "StreamRequest", "SubProcessor": "MediaEntityType", "Type": "Request", "Name": "<Class>StreamRequest" },

{ "Template": "CollectionType", "SubProcessor": "CollectionProperty", "Type": "Model", "Name": "<PropertyType>Collection" },


{ "Template": "CollectionType", "SubProcessor": "CollectionProperty", "Type": "Model", "Name": "<PropertyType>Collection" },
{ "Template": "CollectionType", "SubProcessor": "CollectionMethod", "Type": "Model", "Name": "<PropertyType>Collection" },

{ "Template": "EntityWithReferenceRequest", "SubProcessor": "EntityReferenceType", "Type": "Request", "Name": "<Class>WithReferenceRequest" },
{ "Template": "EntityWithReferenceRequestBuilder", "SubProcessor": "EntityReferenceType", "Type": "Request", "Name": "<Class>WithReferenceRequestBuilder" },
Expand Down
Loading