Hello,
When testing I encountered an issue where the requests with IReturn< Array > are not fully generated; from the documentation I found out that this is a Swift limitation. I have a few dozen requests that returns an array/list (I cannot change the requests) so I was wondering if their was any updates about this limitation. I was able to workaround most limitations of the Swift generator but I don’t think I can find a workaround for this one.
I was thinking that ServiceStack could automatically create a wrapper class for the arrays. Something like this:
ServiceStack would needs to generate the DTO with the correct Return typealias
public class Class1 : IReturn
{
public typealias Return = ArrayOfSomeClass // using the wrapper for the array of SomeClass
required public init(){}
}
protocol ArrayWrapper
{
func fromArray(any:NSArray)
}
// This is the 'wrapper' class that ServiceStack could generate by replacing 'SomeClass' with the targeted class name
// SomeClass would be the returned type in IReturn<SomeClass[]>
public class ArrayOfSomeClass : JsonSerializable, ArrayWrapper
{
public required init() {}
public static var typeName:String { return "ArrayOfSomeClass" }
public static var metadata = Metadata.create([])
public var values = [SomeClass]()
public func fromArray(array:NSArray)
{
for(item) in array
{
values.append(SomeClass.fromObject(item)!)
}
}
// I don't think that we need to implement these methods since this class will never be serialized or deserialized without using the fromArray method
public static func fromObject(any:AnyObject) -> ArrayOfSomeClass? { return nil }
public func toJson() -> String { return "" }
public func toString() -> String { return toJson() }
public static func fromString(string:String) -> ArrayOfSomeClass? { return nil }
}
// Changes required to the fromJson method of the JsonServiceClient.swift
static func fromJson<T : JsonSerializable>(instance:T, json:String) -> T? {
if instance is NSString || instance is String {
if let value = json as? T {
return value
}
}
let parsedJson = parseJson(json);
if let map = parsedJson as? NSDictionary {
return populate(instance, map: map, propertiesMap: T.propertyMap)
}
// read the returned array
if let array = parsedJson as? NSArray {
if let arrayInstance = instance as? ArrayWrapper {
arrayInstance.fromArray(array)
return instance
}
}
return nil
}