Merge pull request #648 from geekerzp/develop_2.0_objc_naming

Updated naming convention in objc client.
This commit is contained in:
Tony Tam 2015-05-12 05:47:51 -04:00
commit e33fbc67ad
8 changed files with 66 additions and 108 deletions

View File

@ -43,6 +43,7 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
defaultIncludes = new HashSet<String>( defaultIncludes = new HashSet<String>(
Arrays.asList( Arrays.asList(
"bool", "bool",
"BOOL",
"int", "int",
"NSString", "NSString",
"NSObject", "NSObject",
@ -59,22 +60,32 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
"NSString", "NSString",
"NSObject", "NSObject",
"NSDate", "NSDate",
"bool") "bool",
"BOOL")
); );
// ref: http://www.tutorialspoint.com/objective_c/objective_c_basic_syntax.htm
reservedWords = new HashSet<String>( reservedWords = new HashSet<String>(
Arrays.asList( Arrays.asList(
"void", "char", "short", "int", "void", "char", "short", "int", "auto", "else", "long", "switch",
"long", "float", "double", "signed", "unsigned", "id", "const", "break", "enum", "register", "typedef",
"volatile", "in", "out", "inout", "bycopy", "byref", "oneway", "case", "extern", "return", "union",
"self", "super", "description" "char", "float", "short", "unsigned",
"const", "for", "signed", "void",
"continue", "goto", "sizeof", "volatile",
"default", "if", "id", "static", "while",
"do", "int", "struct", "_Packed",
"double", "protocol", "interface", "implementation",
"NSObject", "NSInteger", "NSNumber", "CGFloat",
"property", "nonatomic", "retain", "strong",
"weak", "unsafe_unretained", "readwrite", "readonly"
)); ));
typeMapping = new HashMap<String, String>(); typeMapping = new HashMap<String, String>();
typeMapping.put("enum", "NSString"); typeMapping.put("enum", "NSString");
typeMapping.put("Date", "NSDate"); typeMapping.put("Date", "NSDate");
typeMapping.put("DateTime", "NSDate"); typeMapping.put("DateTime", "NSDate");
typeMapping.put("boolean", "NSNumber"); typeMapping.put("boolean", "BOOL");
typeMapping.put("string", "NSString"); typeMapping.put("string", "NSString");
typeMapping.put("integer", "NSNumber"); typeMapping.put("integer", "NSNumber");
typeMapping.put("int", "NSNumber"); typeMapping.put("int", "NSNumber");
@ -188,7 +199,7 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
return swaggerType; return swaggerType;
} }
// In this codition, type of p is objective-c object type, e.g. `SWGPet', // In this codition, type of p is objective-c object type, e.g. `SWGPet',
// return type of p with pointer, e.g. `' // return type of p with pointer, e.g. `SWGPet*'
else { else {
return swaggerType + "*"; return swaggerType + "*";
} }
@ -197,18 +208,28 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
@Override @Override
public String toModelName(String type) { public String toModelName(String type) {
type = type.replaceAll("[^0-9a-zA-Z_]", "_");
// language build-in classes
if(typeMapping.keySet().contains(type) || if(typeMapping.keySet().contains(type) ||
foundationClasses.contains(type) || foundationClasses.contains(type) ||
importMapping.values().contains(type) || importMapping.values().contains(type) ||
defaultIncludes.contains(type) || defaultIncludes.contains(type) ||
languageSpecificPrimitives.contains(type)) { languageSpecificPrimitives.contains(type)) {
return Character.toUpperCase(type.charAt(0)) + type.substring(1); return camelize(type);
} }
// custom classes
else { else {
return PREFIX + Character.toUpperCase(type.charAt(0)) + type.substring(1); return PREFIX + camelize(type);
} }
} }
@Override
public String toModelFilename(String name) {
// should be the same as the model name
return toModelName(name);
}
@Override @Override
protected void setNonArrayMapProperty(CodegenProperty property, String type) { protected void setNonArrayMapProperty(CodegenProperty property, String type) {
super.setNonArrayMapProperty(property, type); super.setNonArrayMapProperty(property, type);
@ -243,28 +264,34 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
return outputFolder + File.separator + sourceFolder; return outputFolder + File.separator + sourceFolder;
} }
@Override
public String toModelFilename(String name) {
return PREFIX + initialCaps(name);
}
@Override @Override
public String toApiName(String name) { public String toApiName(String name) {
return PREFIX + initialCaps(name) + "Api"; return PREFIX + camelize(name) + "Api";
} }
public String toApiFilename(String name) { public String toApiFilename(String name) {
return PREFIX + initialCaps(name) + "Api"; return PREFIX + camelize(name) + "Api";
} }
@Override @Override
public String toVarName(String name) { public String toVarName(String name) {
String paramName = name.replaceAll("[^a-zA-Z0-9_]",""); // replace non-word characters to `_`
if(paramName.startsWith("new") || reservedWords.contains(paramName)) { // e.g. `created-at` to `created_at`
return escapeReservedWord(paramName); name = name.replaceAll("[^a-zA-Z0-9_]","_");
}
else // if it's all upper case, do noting
return paramName; if (name.matches("^[A-Z_]$"))
return name;
// camelize (lower first character) the variable name
// e.g. `pet_id` to `petId`
name = camelize(name, true);
// for reserved word or word starting with number, prepend `_`
if (reservedWords.contains(name) || name.matches("^\\d.*"))
name = escapeReservedWord(name);
return name;
} }
@Override @Override

View File

@ -1 +1,10 @@
<?xml version='1.0' encoding='UTF-8'?><Workspace version='1.0'><FileRef location='group:PetstoreClient/PetstoreClient.xcodeproj'/><FileRef location='group:Pods/Pods.xcodeproj'/></Workspace> <?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:PetstoreClient/PetstoreClient.xcodeproj">
</FileRef>
<FileRef
location = "group:Pods/Pods.xcodeproj">
</FileRef>
</Workspace>

View File

@ -39,7 +39,6 @@
EAEA85EE1811D3AE00F06E69 /* SWGUser.m in Sources */ = {isa = PBXBuildFile; fileRef = EAEA85E11811D3AE00F06E69 /* SWGUser.m */; }; EAEA85EE1811D3AE00F06E69 /* SWGUser.m in Sources */ = {isa = PBXBuildFile; fileRef = EAEA85E11811D3AE00F06E69 /* SWGUser.m */; };
EAEA85EF1811D3AE00F06E69 /* SWGUserApi.m in Sources */ = {isa = PBXBuildFile; fileRef = EAEA85E31811D3AE00F06E69 /* SWGUserApi.m */; }; EAEA85EF1811D3AE00F06E69 /* SWGUserApi.m in Sources */ = {isa = PBXBuildFile; fileRef = EAEA85E31811D3AE00F06E69 /* SWGUserApi.m */; };
EAFBEABB1A925B8500A27431 /* test-1.png in Resources */ = {isa = PBXBuildFile; fileRef = EAFBEABA1A925B8500A27431 /* test-1.png */; }; EAFBEABB1A925B8500A27431 /* test-1.png in Resources */ = {isa = PBXBuildFile; fileRef = EAFBEABA1A925B8500A27431 /* test-1.png */; };
EAFBEABE1A92C42700A27431 /* SWGApiResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = EAFBEABD1A92C42700A27431 /* SWGApiResponse.m */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */ /* Begin PBXContainerItemProxy section */
@ -105,8 +104,6 @@
EAEA85E21811D3AE00F06E69 /* SWGUserApi.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SWGUserApi.h; sourceTree = "<group>"; }; EAEA85E21811D3AE00F06E69 /* SWGUserApi.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SWGUserApi.h; sourceTree = "<group>"; };
EAEA85E31811D3AE00F06E69 /* SWGUserApi.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SWGUserApi.m; sourceTree = "<group>"; }; EAEA85E31811D3AE00F06E69 /* SWGUserApi.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SWGUserApi.m; sourceTree = "<group>"; };
EAFBEABA1A925B8500A27431 /* test-1.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "test-1.png"; sourceTree = "<group>"; }; EAFBEABA1A925B8500A27431 /* test-1.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "test-1.png"; sourceTree = "<group>"; };
EAFBEABC1A92C42700A27431 /* SWGApiResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SWGApiResponse.h; sourceTree = "<group>"; };
EAFBEABD1A92C42700A27431 /* SWGApiResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SWGApiResponse.m; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@ -229,8 +226,6 @@
children = ( children = (
EA8B8AA21AC6683700638FBB /* SWGQueryParamCollection.h */, EA8B8AA21AC6683700638FBB /* SWGQueryParamCollection.h */,
EA8B8AA31AC6683700638FBB /* SWGQueryParamCollection.m */, EA8B8AA31AC6683700638FBB /* SWGQueryParamCollection.m */,
EAFBEABC1A92C42700A27431 /* SWGApiResponse.h */,
EAFBEABD1A92C42700A27431 /* SWGApiResponse.m */,
EAEA85CC1811D3AE00F06E69 /* SWGApiClient.h */, EAEA85CC1811D3AE00F06E69 /* SWGApiClient.h */,
EAEA85CD1811D3AE00F06E69 /* SWGApiClient.m */, EAEA85CD1811D3AE00F06E69 /* SWGApiClient.m */,
EAEA85CE1811D3AE00F06E69 /* SWGCategory.h */, EAEA85CE1811D3AE00F06E69 /* SWGCategory.h */,
@ -400,7 +395,6 @@
EA6699B31811D2FA00A70D03 /* ViewController.m in Sources */, EA6699B31811D2FA00A70D03 /* ViewController.m in Sources */,
EA6699AA1811D2FA00A70D03 /* AppDelegate.m in Sources */, EA6699AA1811D2FA00A70D03 /* AppDelegate.m in Sources */,
EAEA85EE1811D3AE00F06E69 /* SWGUser.m in Sources */, EAEA85EE1811D3AE00F06E69 /* SWGUser.m in Sources */,
EAFBEABE1A92C42700A27431 /* SWGApiResponse.m in Sources */,
EAEA85EF1811D3AE00F06E69 /* SWGUserApi.m in Sources */, EAEA85EF1811D3AE00F06E69 /* SWGUserApi.m in Sources */,
EAEA85EB1811D3AE00F06E69 /* SWGPetApi.m in Sources */, EAEA85EB1811D3AE00F06E69 /* SWGPetApi.m in Sources */,
EAEA85E61811D3AE00F06E69 /* SWGDate.m in Sources */, EAEA85E61811D3AE00F06E69 /* SWGDate.m in Sources */,

View File

@ -1,18 +0,0 @@
#import <Foundation/Foundation.h>
#import "SWGObject.h"
@interface SWGApiResponse : SWGObject
@property(nonatomic) NSNumber* code;
@property(nonatomic) NSString* type;
@property(nonatomic) NSString* message;
- (id) code: (NSNumber*) code
type: (NSString*) type
message: (NSString*) message;
- (id) initWithValues: (NSDictionary*)dict;
- (NSDictionary*) asDictionary;
@end

View File

@ -1,54 +0,0 @@
#import "SWGDate.h"
#import "SWGApiResponse.h"
@implementation SWGApiResponse
-(id)code: (NSNumber*) code
type: (NSString*) type
message: (NSString*) message
{
_code = code;
_type = type;
_message = message;
return self;
}
-(id) initWithValues:(NSDictionary*)dict
{
self = [super init];
if(self) {
_code = dict[@"code"];
_type = dict[@"type"];
_message = dict[@"message"];
}
return self;
}
-(NSDictionary*) asDictionary {
NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
if(_code != nil) dict[@"code"] = _code ;
if(_type != nil) dict[@"type"] = _type ;
if(_message != nil) dict[@"message"] = _message ;
NSDictionary* output = [dict copy];
return output;
}
@end

View File

@ -19,6 +19,6 @@
*/ */
@property(nonatomic) NSString* status; @property(nonatomic) NSString* status;
@property(nonatomic) NSNumber* complete; @property(nonatomic) BOOL complete;
@end @end

View File

@ -116,13 +116,13 @@
Deletes a pet Deletes a pet
@param api_key @param apiKey
@param petId Pet id to delete @param petId Pet id to delete
return type: return type:
*/ */
-(NSNumber*) deletePetWithCompletionBlock :(NSString*) api_key -(NSNumber*) deletePetWithCompletionBlock :(NSString*) apiKey
petId:(NSNumber*) petId petId:(NSNumber*) petId

View File

@ -587,11 +587,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
/*! /*!
* Deletes a pet * Deletes a pet
* *
* \param api_key * \param apiKey
* \param petId Pet id to delete * \param petId Pet id to delete
* \returns void * \returns void
*/ */
-(NSNumber*) deletePetWithCompletionBlock: (NSString*) api_key -(NSNumber*) deletePetWithCompletionBlock: (NSString*) apiKey
petId: (NSNumber*) petId petId: (NSNumber*) petId
@ -616,8 +616,8 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders]; NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders];
if(api_key != nil) if(apiKey != nil)
headerParams[@"api_key"] = api_key; headerParams[@"api_key"] = apiKey;
id bodyDictionary = nil; id bodyDictionary = nil;