Merge pull request #1688 from wing328/objc_model_default_value

[ObjC] add default value to ObjC model properties
This commit is contained in:
wing328 2015-12-10 14:17:16 +08:00
commit 2b6b500c35
7 changed files with 113 additions and 8 deletions

View File

@ -7,9 +7,7 @@ import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.SupportingFile;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.*;
import java.io.File;
import java.util.Arrays;
@ -343,11 +341,6 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
return name;
}
@Override
public String toDefaultValue(Property p) {
return null;
}
@Override
public String apiFileFolder() {
return outputFolder + File.separatorChar + apiPackage();
@ -452,4 +445,55 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
public void setLicense(String license) {
this.license = license;
}
/**
* Return the default value of the property
*
* @param p Swagger property object
* @return string presentation of the default value of the property
*/
@Override
public String toDefaultValue(Property p) {
if (p instanceof StringProperty) {
StringProperty dp = (StringProperty) p;
if (dp.getDefault() != null) {
return "@\"" + dp.getDefault().toString() + "\"";
}
} else if (p instanceof BooleanProperty) {
BooleanProperty dp = (BooleanProperty) p;
if (dp.getDefault() != null) {
if (dp.getDefault().toString().equalsIgnoreCase("false"))
return "@0";
else
return "@1";
}
} else if (p instanceof DateProperty) {
// TODO
} else if (p instanceof DateTimeProperty) {
// TODO
} else if (p instanceof DoubleProperty) {
DoubleProperty dp = (DoubleProperty) p;
if (dp.getDefault() != null) {
return "@" + dp.getDefault().toString();
}
} else if (p instanceof FloatProperty) {
FloatProperty dp = (FloatProperty) p;
if (dp.getDefault() != null) {
return "@" + dp.getDefault().toString();
}
} else if (p instanceof IntegerProperty) {
IntegerProperty dp = (IntegerProperty) p;
if (dp.getDefault() != null) {
return "@" + dp.getDefault().toString();
}
} else if (p instanceof LongProperty) {
LongProperty dp = (LongProperty) p;
if (dp.getDefault() != null) {
return "@" + dp.getDefault().toString();
}
}
return null;
}
}

View File

@ -4,6 +4,17 @@
@implementation {{classname}}
- (instancetype)init {
self = [super init];
if (self)
{
// initalise property's default value, if any
{{#vars}}{{#defaultValue}}self.{{name}} = {{{defaultValue}}};
{{/defaultValue}}{{/vars}}
}
return self;
}
/**
* Maps json key to property name.
* This method is used by `JSONModel`.

View File

@ -2,6 +2,16 @@
@implementation SWGCategory
- (instancetype)init {
self = [super init];
if (self)
{
// initalise property's default value, if any
}
return self;
}
/**
* Maps json key to property name.
* This method is used by `JSONModel`.

View File

@ -2,6 +2,16 @@
@implementation SWGOrder
- (instancetype)init {
self = [super init];
if (self)
{
// initalise property's default value, if any
}
return self;
}
/**
* Maps json key to property name.
* This method is used by `JSONModel`.

View File

@ -2,6 +2,16 @@
@implementation SWGPet
- (instancetype)init {
self = [super init];
if (self)
{
// initalise property's default value, if any
}
return self;
}
/**
* Maps json key to property name.
* This method is used by `JSONModel`.

View File

@ -2,6 +2,16 @@
@implementation SWGTag
- (instancetype)init {
self = [super init];
if (self)
{
// initalise property's default value, if any
}
return self;
}
/**
* Maps json key to property name.
* This method is used by `JSONModel`.

View File

@ -2,6 +2,16 @@
@implementation SWGUser
- (instancetype)init {
self = [super init];
if (self)
{
// initalise property's default value, if any
}
return self;
}
/**
* Maps json key to property name.
* This method is used by `JSONModel`.