add jsonmodel in objective-c client

This commit is contained in:
geekerzp 2015-03-26 10:07:33 +08:00
parent 20add5b58e
commit fbe069cf5c
7 changed files with 61 additions and 112 deletions

View File

@ -48,6 +48,7 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
"NSObject", "NSObject",
"NSArray", "NSArray",
"NSNumber", "NSNumber",
"NSDate",
"NSDictionary", "NSDictionary",
"NSMutableArray", "NSMutableArray",
"NSMutableDictionary") "NSMutableDictionary")
@ -57,6 +58,7 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
"NSNumber", "NSNumber",
"NSString", "NSString",
"NSObject", "NSObject",
"NSDate",
"bool") "bool")
); );
@ -70,9 +72,8 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
typeMapping = new HashMap<String, String>(); typeMapping = new HashMap<String, String>();
typeMapping.put("enum", "NSString"); typeMapping.put("enum", "NSString");
typeMapping.put("Date", "SWGDate"); typeMapping.put("Date", "NSDate");
typeMapping.put("DateTime", "SWGDate"); typeMapping.put("DateTime", "NSDate");
// typeMapping.put("Date", "SWGDate");
typeMapping.put("boolean", "NSNumber"); typeMapping.put("boolean", "NSNumber");
typeMapping.put("string", "NSString"); typeMapping.put("string", "NSString");
typeMapping.put("integer", "NSNumber"); typeMapping.put("integer", "NSNumber");
@ -87,13 +88,13 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
typeMapping.put("object", "NSObject"); typeMapping.put("object", "NSObject");
importMapping = new HashMap<String, String> (); importMapping = new HashMap<String, String> ();
importMapping.put("Date", "SWGDate");
foundationClasses = new HashSet<String> ( foundationClasses = new HashSet<String> (
Arrays.asList( Arrays.asList(
"NSNumber", "NSNumber",
"NSObject", "NSObject",
"NSString", "NSString",
"NSDate",
"NSDictionary") "NSDictionary")
); );
@ -151,12 +152,46 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
@Override @Override
public String getTypeDeclaration(Property p) { public String getTypeDeclaration(Property p) {
if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
Property inner = ap.getItems();
String innerType = getSwaggerType(inner);
// In this codition, type of property p is array of primitive,
// return container type with pointer, e.g. `NSArray*'
if (languageSpecificPrimitives.contains(innerType))
return getSwaggerType(p) + "*";
// In this codition, type of property p is array of model,
// return container type combine inner type with pointer, e.g. `NSArray<SWGTag>*'
String innerTypeDeclaration = getTypeDeclaration(inner);
if (innerTypeDeclaration.endsWith("*"))
innerTypeDeclaration = innerTypeDeclaration.substring(0, innerTypeDeclaration.length() - 1);
return getSwaggerType(p) + "<" + innerTypeDeclaration + ">*";
}
else {
String swaggerType = getSwaggerType(p); String swaggerType = getSwaggerType(p);
if(languageSpecificPrimitives.contains(swaggerType) && !foundationClasses.contains(swaggerType))
return toModelName(swaggerType); // In this codition, type of p is objective-c primitive type, e.g. `NSSNumber',
else // return type of p with pointer, e.g. `NSNumber*'
if (languageSpecificPrimitives.contains(swaggerType) &&
foundationClasses.contains(swaggerType)) {
return swaggerType + "*"; return swaggerType + "*";
} }
// In this codition, type of p is c primitive type, e.g. `bool',
// return type of p, e.g. `bool'
else if (languageSpecificPrimitives.contains(swaggerType)) {
return swaggerType;
}
// In this codition, type of p is objective-c object type, e.g. `SWGPet',
// return type of p with pointer, e.g. `'
else {
return swaggerType + "*";
}
}
}
@Override @Override
public String toModelName(String type) { public String toModelName(String type) {

View File

@ -1,3 +1,4 @@
platform :ios, '6.0' platform :ios, '6.0'
xcodeproj '{{projectName}}/{{projectName}}.xcodeproj' xcodeproj '{{projectName}}/{{projectName}}.xcodeproj'
pod 'AFNetworking', '~> 2.1' pod 'AFNetworking', '~> 2.1'
pod 'JSONModel', '~> 1.0'

View File

@ -1,6 +1,5 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "JSONModel.h"
@interface SWGObject : NSObject @interface SWGObject : JSONModel
- (id) initWithValues:(NSDictionary*)dict;
- (NSDictionary*) asDictionary;
@end @end

View File

@ -1,17 +1,4 @@
#import "SWGObject.h" #import "SWGObject.h"
@implementation SWGObject @implementation SWGObject
- (id) initWithValues:(NSDictionary*)dict {
return self;
}
- (NSDictionary*) asDictionary{
return [NSDictionary init];
}
- (NSString*)description {
return [NSString stringWithFormat:@"%@ %@", [super description], [self asDictionary]];
}
@end @end

View File

@ -1,81 +1,12 @@
{{#models}} {{#models}}
{{#model}} {{#model}}
#import "SWGDate.h"
#import "{{classname}}.h" #import "{{classname}}.h"
@implementation {{classname}} @implementation {{classname}}
-(id){{#vars}}{{name}}: ({{datatype}}) {{name}} + (JSONKeyMapper *)keyMapper
{{/vars}}
{{newline}}{
{{#vars}}_{{name}} = {{name}};
{{/vars}}
return self;
}
-(id) initWithValues:(NSDictionary*)dict
{ {
self = [super init]; return [[JSONKeyMapper alloc] initWithDictionary:@{ {{#vars}}@"{{baseName}}": @"{{name}}"{{#hasMore}}, {{/hasMore}}{{/vars}} }];
if(self) {
{{#vars}}{{#isPrimitiveType}}_{{name}} = dict[@"{{baseName}}"];{{/isPrimitiveType}}
{{#complexType}}
id {{name}}_dict = dict[@"{{baseName}}"];
{{#isContainer}}
if([{{name}}_dict isKindOfClass:[NSArray class]]) {
NSMutableArray * objs = [[NSMutableArray alloc] initWithCapacity:[(NSArray*){{name}}_dict count]];
if([(NSArray*){{name}}_dict count] > 0) {
for (NSDictionary* dict in (NSArray*){{name}}_dict) {
{{{complexType}}}* d = [[{{#instantiationType}}NSClassFromString(@"{{{instantiationType}}}") {{/instantiationType}}{{^instantiationType}}{{{complexType}}}{{/instantiationType}} alloc] initWithValues:dict];
[objs addObject:d];
}
_{{{name}}} = [[NSArray alloc] initWithArray:objs];
}
else
_{{name}} = [[NSArray alloc] init];
}
else {
_{{name}} = [[NSArray alloc] init];
}
{{/isContainer}}{{#isNotContainer}}
if({{name}}_dict != nil)
_{{name}} = [[{{#instantiationType}}NSClassFromString(@"{{{instantiationType}}}") {{/instantiationType}}{{^instantiationType}}{{{complexType}}} {{/instantiationType}} alloc]{{setter}}:{{name}}_dict];
{{/isNotContainer}}
{{/complexType}}
{{/vars}}{{newline}}
}
return self;
}
-(NSDictionary*) asDictionary {
NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
{{#vars}}
{{#complexType}}
if(_{{name}} != nil){
if([_{{name}} isKindOfClass:[NSArray class]]){
NSMutableArray * array = [[NSMutableArray alloc] init];
for( {{complexType}} *{{name}} in (NSArray*)_{{name}}) {
[array addObject:[(SWGObject*){{name}} asDictionary]];
}
dict[@"{{name}}"] = array;
}
else if(_{{name}} && [_{{name}} isKindOfClass:[SWGDate class]]) {
NSString * dateString = [(SWGDate*)_{{name}} toString];
if(dateString){
dict[@"{{name}}"] = dateString;
}
}
else {
{{/complexType}}
if(_{{name}} != nil) dict[@"{{baseName}}"] = {{#complexType}}[(SWGObject*){{/complexType}}_{{name}} {{#complexType}}asDictionary]{{/complexType}};
{{#complexType}}
}
}
{{/complexType}}
{{/vars}}
NSDictionary* output = [dict copy];
return output;
} }
{{/model}} {{/model}}

View File

@ -6,17 +6,16 @@
{{#models}} {{#models}}
{{#model}} {{#model}}
@protocol {{classname}}
@end
@interface {{classname}} : SWGObject @interface {{classname}} : SWGObject
{{#vars}} {{#vars}}
@property(nonatomic) {{datatype}} {{name}}; {{#description}}/* {{{description}}} {{#isNotRequired}}[optional]{{/isNotRequired}} */{{/description}}{{newline}} {{#description}}/* {{{description}}} {{#isNotRequired}}[optional]{{/isNotRequired}} */{{/description}}
@property(nonatomic) {{{ datatype }}}{{#isNotRequired}}<Optional>{{/isNotRequired}} {{name}};
{{/vars}} {{/vars}}
- (id) {{#vars}}{{name}}: ({{datatype}}) {{name}}{{#hasMore}}{{newline}} {{/hasMore}}{{^hasMore}};{{/hasMore}}
{{/vars}}
{{newline}}
- (id) initWithValues: (NSDictionary*)dict;
- (NSDictionary*) asDictionary;
{{newline}}
@end @end
{{/model}} {{/model}}
{{/models}} {{/models}}

View File

@ -56,17 +56,14 @@ class ObjcModelTest extends FlatSpec with Matchers {
vars.get(1).isNotContainer should equal (true) vars.get(1).isNotContainer should equal (true)
vars.get(2).baseName should be ("createdAt") vars.get(2).baseName should be ("createdAt")
vars.get(2).complexType should be ("SWGDate") vars.get(2).datatype should be ("NSDate*")
vars.get(2).datatype should be ("SWGDate*")
vars.get(2).name should be ("createdAt") vars.get(2).name should be ("createdAt")
vars.get(2).defaultValue should be (null) vars.get(2).defaultValue should be (null)
vars.get(2).baseType should be ("SWGDate") vars.get(2).baseType should be ("NSDate")
vars.get(2).hasMore should equal (null) vars.get(2).hasMore should equal (null)
vars.get(2).required should equal (false) vars.get(2).required should equal (false)
vars.get(2).isNotContainer should equal (true) vars.get(2).isNotContainer should equal (true)
(cm.imports.asScala.toSet &
Set("SWGDate")).size should be (1)
} }
it should "convert a model with list property" in { it should "convert a model with list property" in {
@ -173,7 +170,7 @@ class ObjcModelTest extends FlatSpec with Matchers {
val vars = cm.vars val vars = cm.vars
vars.get(0).baseName should be ("children") vars.get(0).baseName should be ("children")
vars.get(0).complexType should be ("SWGChildren") vars.get(0).complexType should be ("SWGChildren")
vars.get(0).datatype should be ("NSArray*") vars.get(0).datatype should be ("NSArray<SWGChildren>*")
vars.get(0).name should be ("children") vars.get(0).name should be ("children")
vars.get(0).baseType should be ("NSArray") vars.get(0).baseType should be ("NSArray")
vars.get(0).containerType should be ("array") vars.get(0).containerType should be ("array")