rythm-swift.unionSerializers.txt Maven / Gradle / Ivy
@args com.linkedin.pegasus.generator.spec.UnionTemplateSpec union, org.coursera.courier.swift.SwiftSyntax syntax, org.coursera.courier.swift.SwiftyJSON swifty
@import com.linkedin.pegasus.generator.spec.UnionTemplateSpec.Member
@{ String className = syntax.escapeKeyword(union.getClassName()) }
public static func readJSON(json: JSON) throws -> @className {
@if (union.getMembers().size() > 0) {let dict = json.dictionaryValue}
@for(Member member: union.getMembers()) {
@{ String unionMemberName = syntax.toUnionMemberName(member.getClassTemplateSpec()) }
if let member = dict["@(member.getSchema().getUnionMemberKey())"] {
return .@(unionMemberName)(@swifty.toGetAccessor("member", member))
}
}
if let unknownDict = json.dictionaryObject {
return .UNKNOWN$(unknownDict)
} else {
throw ReadError(cause: "Union must be a JSON object.")
}
}
public func writeData() -> [String: AnyObject] {
switch self {
@for(Member member: union.getMembers()) {
@{ String unionMemberName = syntax.toUnionMemberName(member.getClassTemplateSpec()) }
case .@(unionMemberName)(let member):
return ["@(member.getSchema().getUnionMemberKey())": @swifty.toSetAccessor("member", member)];
}
case .UNKNOWN$(let dict):
return dict
}
}