From 75a1f97a1e116dd47758266e8465be4bc8184d33 Mon Sep 17 00:00:00 2001 From: wing328 Date: Thu, 10 Dec 2015 15:21:52 +0800 Subject: [PATCH] minor style change for objc client --- .../resources/objc/ApiClient-body.mustache | 43 +++++++------- .../src/main/resources/objc/api-body.mustache | 6 +- .../main/resources/objc/model-body.mustache | 5 +- .../objc/SwaggerClient/SWGApiClient.m | 59 ++++++++++--------- .../petstore/objc/SwaggerClient/SWGCategory.m | 5 +- .../petstore/objc/SwaggerClient/SWGOrder.m | 5 +- .../petstore/objc/SwaggerClient/SWGPet.m | 5 +- .../petstore/objc/SwaggerClient/SWGPetApi.m | 6 +- .../petstore/objc/SwaggerClient/SWGTag.m | 5 +- .../petstore/objc/SwaggerClient/SWGUser.m | 5 +- .../petstore/objc/SwaggerClient/SWGUserApi.m | 4 +- 11 files changed, 79 insertions(+), 69 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache b/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache index 0c23c800469..25b5d9adfbf 100644 --- a/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache @@ -132,17 +132,20 @@ static void (^reachabilityChangeBlock)(int); if (accepts == nil || [accepts count] == 0) { return @""; } + NSMutableArray *lowerAccepts = [[NSMutableArray alloc] initWithCapacity:[accepts count]]; for (NSString *string in accepts) { NSString * lowerAccept = [string lowercaseString]; - if([lowerAccept containsString:@"application/json"]) { + if ([lowerAccept containsString:@"application/json"]) { return @"application/json"; } [lowerAccepts addObject:lowerAccept]; } - if(lowerAccepts.count == 1){ + + if (lowerAccepts.count == 1) { return [lowerAccepts firstObject]; } + return [lowerAccepts componentsJoinedByString:@", "]; } @@ -169,7 +172,7 @@ static void (^reachabilityChangeBlock)(int); } + (NSString*)escape:(id)unescaped { - if([unescaped isKindOfClass:[NSString class]]){ + if ([unescaped isKindOfClass:[NSString class]]){ return (NSString *)CFBridgingRelease (CFURLCreateStringByAddingPercentEscapes( NULL, @@ -210,7 +213,7 @@ static void (^reachabilityChangeBlock)(int); -(Boolean) executeRequestWithId:(NSNumber*) requestId { NSSet* matchingItems = [queuedRequests objectsPassingTest:^BOOL(id obj, BOOL *stop) { - if([obj intValue] == [requestId intValue]) { + if ([obj intValue] == [requestId intValue]) { return YES; } else { @@ -218,7 +221,7 @@ static void (^reachabilityChangeBlock)(int); } }]; - if(matchingItems.count == 1) { + if (matchingItems.count == 1) { {{classPrefix}}DebugLog(@"removed request id %@", requestId); [queuedRequests removeObject:requestId]; return YES; @@ -266,7 +269,7 @@ static void (^reachabilityChangeBlock)(int); } // call the reachability block, if configured - if(reachabilityChangeBlock != nil) { + if (reachabilityChangeBlock != nil) { reachabilityChangeBlock(status); } }]; @@ -408,16 +411,16 @@ static void (^reachabilityChangeBlock)(int); completionBlock: (void (^)(id, NSError *))completionBlock { AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id response) { - if([self executeRequestWithId:requestId]) { + if ([self executeRequestWithId:requestId]) { [self logResponse:operation forRequest:request error:nil]; NSDictionary *responseHeaders = [[operation response] allHeaderFields]; self.HTTPResponseHeaders = responseHeaders; completionBlock(response, nil); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { - if([self executeRequestWithId:requestId]) { + if ([self executeRequestWithId:requestId]) { NSMutableDictionary *userInfo = [error.userInfo mutableCopy]; - if(operation.responseObject) { + if (operation.responseObject) { // Add in the (parsed) response body. userInfo[{{classPrefix}}ResponseObjectErrorKey] = operation.responseObject; } @@ -588,10 +591,10 @@ static void (^reachabilityChangeBlock)(int); // request cache BOOL hasHeaderParams = false; - if(headerParams != nil && [headerParams count] > 0) { + if (headerParams != nil && [headerParams count] > 0) { hasHeaderParams = true; } - if(offlineState) { + if (offlineState) { {{classPrefix}}DebugLog(@"%@ cache forced", resourcePath); [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; } @@ -604,7 +607,7 @@ static void (^reachabilityChangeBlock)(int); [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; } - if(hasHeaderParams){ + if (hasHeaderParams){ for(NSString * key in [headerParams keyEnumerator]){ [request setValue:[headerParams valueForKey:key] forHTTPHeaderField:key]; } @@ -637,36 +640,36 @@ static void (^reachabilityChangeBlock)(int); int counter = 0; NSMutableString * requestUrl = [NSMutableString stringWithFormat:@"%@", path]; - if(queryParams != nil){ + if (queryParams != nil){ for(NSString * key in [queryParams keyEnumerator]){ - if(counter == 0) separator = @"?"; + if (counter == 0) separator = @"?"; else separator = @"&"; id queryParam = [queryParams valueForKey:key]; - if([queryParam isKindOfClass:[NSString class]]){ + if ([queryParam isKindOfClass:[NSString class]]){ [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, [{{classPrefix}}ApiClient escape:key], [{{classPrefix}}ApiClient escape:[queryParams valueForKey:key]]]]; } - else if([queryParam isKindOfClass:[{{classPrefix}}QueryParamCollection class]]){ + else if ([queryParam isKindOfClass:[{{classPrefix}}QueryParamCollection class]]){ {{classPrefix}}QueryParamCollection * coll = ({{classPrefix}}QueryParamCollection*) queryParam; NSArray* values = [coll values]; NSString* format = [coll format]; - if([format isEqualToString:@"csv"]) { + if ([format isEqualToString:@"csv"]) { [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, [{{classPrefix}}ApiClient escape:key], [NSString stringWithFormat:@"%@", [values componentsJoinedByString:@","]]]]; } - else if([format isEqualToString:@"tsv"]) { + else if ([format isEqualToString:@"tsv"]) { [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, [{{classPrefix}}ApiClient escape:key], [NSString stringWithFormat:@"%@", [values componentsJoinedByString:@"\t"]]]]; } - else if([format isEqualToString:@"pipes"]) { + else if ([format isEqualToString:@"pipes"]) { [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, [{{classPrefix}}ApiClient escape:key], [NSString stringWithFormat:@"%@", [values componentsJoinedByString:@"|"]]]]; } - else if([format isEqualToString:@"multi"]) { + else if ([format isEqualToString:@"multi"]) { for(id obj in values) { [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, [{{classPrefix}}ApiClient escape:key], [NSString stringWithFormat:@"%@", obj]]]; diff --git a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache index 60358e4e9da..d84e350360f 100644 --- a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache @@ -105,7 +105,7 @@ static {{classname}}* singletonAPI = nil; {{/pathParams}} NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; - {{#queryParams}}if({{paramName}} != nil) { + {{#queryParams}}if ({{paramName}} != nil) { {{#collectionFormat}} queryParams[@"{{baseName}}"] = [[{{classPrefix}}QueryParamCollection alloc] initWithValuesAndFormat: {{baseName}} format: @"{{collectionFormat}}"]; {{/collectionFormat}} @@ -114,7 +114,7 @@ static {{classname}}* singletonAPI = nil; {{/queryParams}} NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders]; - {{#headerParams}}if({{paramName}} != nil) { + {{#headerParams}}if ({{paramName}} != nil) { headerParams[@"{{baseName}}"] = {{paramName}}; } {{/headerParams}} @@ -159,7 +159,7 @@ static {{classname}}* singletonAPI = nil; {{#requiredParamCount}} {{#requiredParams}} - if({{paramName}} == nil) { + if ({{paramName}} == nil) { // error } {{/requiredParams}} diff --git a/modules/swagger-codegen/src/main/resources/objc/model-body.mustache b/modules/swagger-codegen/src/main/resources/objc/model-body.mustache index ba76b12aeb8..3d70cebfae2 100644 --- a/modules/swagger-codegen/src/main/resources/objc/model-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/model-body.mustache @@ -6,12 +6,13 @@ - (instancetype)init { self = [super init]; - if (self) - { + + if (self) { // initalise property's default value, if any {{#vars}}{{#defaultValue}}self.{{name}} = {{{defaultValue}}}; {{/defaultValue}}{{/vars}} } + return self; } diff --git a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m index dcd33575fc6..4dce6d2fa8b 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m @@ -128,24 +128,25 @@ static void (^reachabilityChangeBlock)(int); /* * Detect `Accept` from accepts */ -+ (NSString *) selectHeaderAccept:(NSArray *)accepts -{ ++ (NSString *) selectHeaderAccept:(NSArray *)accepts { if (accepts == nil || [accepts count] == 0) { return @""; } NSMutableArray *lowerAccepts = [[NSMutableArray alloc] initWithCapacity:[accepts count]]; - [accepts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [lowerAccepts addObject:[obj lowercaseString]]; - }]; - - - if ([lowerAccepts containsObject:@"application/json"]) { - return @"application/json"; + for (NSString *string in accepts) { + NSString * lowerAccept = [string lowercaseString]; + if ([lowerAccept containsString:@"application/json"]) { + return @"application/json"; + } + [lowerAccepts addObject:lowerAccept]; } - else { - return [lowerAccepts componentsJoinedByString:@", "]; + + if (lowerAccepts.count == 1) { + return [lowerAccepts firstObject]; } + + return [lowerAccepts componentsJoinedByString:@", "]; } /* @@ -171,7 +172,7 @@ static void (^reachabilityChangeBlock)(int); } + (NSString*)escape:(id)unescaped { - if([unescaped isKindOfClass:[NSString class]]){ + if ([unescaped isKindOfClass:[NSString class]]){ return (NSString *)CFBridgingRelease (CFURLCreateStringByAddingPercentEscapes( NULL, @@ -212,7 +213,7 @@ static void (^reachabilityChangeBlock)(int); -(Boolean) executeRequestWithId:(NSNumber*) requestId { NSSet* matchingItems = [queuedRequests objectsPassingTest:^BOOL(id obj, BOOL *stop) { - if([obj intValue] == [requestId intValue]) { + if ([obj intValue] == [requestId intValue]) { return YES; } else { @@ -220,7 +221,7 @@ static void (^reachabilityChangeBlock)(int); } }]; - if(matchingItems.count == 1) { + if (matchingItems.count == 1) { SWGDebugLog(@"removed request id %@", requestId); [queuedRequests removeObject:requestId]; return YES; @@ -268,7 +269,7 @@ static void (^reachabilityChangeBlock)(int); } // call the reachability block, if configured - if(reachabilityChangeBlock != nil) { + if (reachabilityChangeBlock != nil) { reachabilityChangeBlock(status); } }]; @@ -410,16 +411,16 @@ static void (^reachabilityChangeBlock)(int); completionBlock: (void (^)(id, NSError *))completionBlock { AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id response) { - if([self executeRequestWithId:requestId]) { + if ([self executeRequestWithId:requestId]) { [self logResponse:operation forRequest:request error:nil]; NSDictionary *responseHeaders = [[operation response] allHeaderFields]; self.HTTPResponseHeaders = responseHeaders; completionBlock(response, nil); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { - if([self executeRequestWithId:requestId]) { + if ([self executeRequestWithId:requestId]) { NSMutableDictionary *userInfo = [error.userInfo mutableCopy]; - if(operation.responseObject) { + if (operation.responseObject) { // Add in the (parsed) response body. userInfo[SWGResponseObjectErrorKey] = operation.responseObject; } @@ -590,10 +591,10 @@ static void (^reachabilityChangeBlock)(int); // request cache BOOL hasHeaderParams = false; - if(headerParams != nil && [headerParams count] > 0) { + if (headerParams != nil && [headerParams count] > 0) { hasHeaderParams = true; } - if(offlineState) { + if (offlineState) { SWGDebugLog(@"%@ cache forced", resourcePath); [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; } @@ -606,7 +607,7 @@ static void (^reachabilityChangeBlock)(int); [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; } - if(hasHeaderParams){ + if (hasHeaderParams){ for(NSString * key in [headerParams keyEnumerator]){ [request setValue:[headerParams valueForKey:key] forHTTPHeaderField:key]; } @@ -639,36 +640,36 @@ static void (^reachabilityChangeBlock)(int); int counter = 0; NSMutableString * requestUrl = [NSMutableString stringWithFormat:@"%@", path]; - if(queryParams != nil){ + if (queryParams != nil){ for(NSString * key in [queryParams keyEnumerator]){ - if(counter == 0) separator = @"?"; + if (counter == 0) separator = @"?"; else separator = @"&"; id queryParam = [queryParams valueForKey:key]; - if([queryParam isKindOfClass:[NSString class]]){ + if ([queryParam isKindOfClass:[NSString class]]){ [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, [SWGApiClient escape:key], [SWGApiClient escape:[queryParams valueForKey:key]]]]; } - else if([queryParam isKindOfClass:[SWGQueryParamCollection class]]){ + else if ([queryParam isKindOfClass:[SWGQueryParamCollection class]]){ SWGQueryParamCollection * coll = (SWGQueryParamCollection*) queryParam; NSArray* values = [coll values]; NSString* format = [coll format]; - if([format isEqualToString:@"csv"]) { + if ([format isEqualToString:@"csv"]) { [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, [SWGApiClient escape:key], [NSString stringWithFormat:@"%@", [values componentsJoinedByString:@","]]]]; } - else if([format isEqualToString:@"tsv"]) { + else if ([format isEqualToString:@"tsv"]) { [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, [SWGApiClient escape:key], [NSString stringWithFormat:@"%@", [values componentsJoinedByString:@"\t"]]]]; } - else if([format isEqualToString:@"pipes"]) { + else if ([format isEqualToString:@"pipes"]) { [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, [SWGApiClient escape:key], [NSString stringWithFormat:@"%@", [values componentsJoinedByString:@"|"]]]]; } - else if([format isEqualToString:@"multi"]) { + else if ([format isEqualToString:@"multi"]) { for(id obj in values) { [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, [SWGApiClient escape:key], [NSString stringWithFormat:@"%@", obj]]]; diff --git a/samples/client/petstore/objc/SwaggerClient/SWGCategory.m b/samples/client/petstore/objc/SwaggerClient/SWGCategory.m index 7c71e4af0f4..0d10e25426e 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGCategory.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGCategory.m @@ -4,11 +4,12 @@ - (instancetype)init { self = [super init]; - if (self) - { + + if (self) { // initalise property's default value, if any } + return self; } diff --git a/samples/client/petstore/objc/SwaggerClient/SWGOrder.m b/samples/client/petstore/objc/SwaggerClient/SWGOrder.m index ba0b637eb09..abf77a7b399 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGOrder.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGOrder.m @@ -4,11 +4,12 @@ - (instancetype)init { self = [super init]; - if (self) - { + + if (self) { // initalise property's default value, if any } + return self; } diff --git a/samples/client/petstore/objc/SwaggerClient/SWGPet.m b/samples/client/petstore/objc/SwaggerClient/SWGPet.m index 388157eb0cc..f4fd77ea33f 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGPet.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGPet.m @@ -4,11 +4,12 @@ - (instancetype)init { self = [super init]; - if (self) - { + + if (self) { // initalise property's default value, if any } + return self; } diff --git a/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m b/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m index bff6c3765b1..c7c2ed175c6 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m @@ -250,7 +250,7 @@ static SWGPetApi* singletonAPI = nil; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; - if(status != nil) { + if (status != nil) { queryParams[@"status"] = [[SWGQueryParamCollection alloc] initWithValuesAndFormat: status format: @"multi"]; @@ -334,7 +334,7 @@ static SWGPetApi* singletonAPI = nil; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; - if(tags != nil) { + if (tags != nil) { queryParams[@"tags"] = [[SWGQueryParamCollection alloc] initWithValuesAndFormat: tags format: @"multi"]; @@ -622,7 +622,7 @@ static SWGPetApi* singletonAPI = nil; NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders]; - if(apiKey != nil) { + if (apiKey != nil) { headerParams[@"api_key"] = apiKey; } diff --git a/samples/client/petstore/objc/SwaggerClient/SWGTag.m b/samples/client/petstore/objc/SwaggerClient/SWGTag.m index 7a7e7550deb..b19085e2e6b 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGTag.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGTag.m @@ -4,11 +4,12 @@ - (instancetype)init { self = [super init]; - if (self) - { + + if (self) { // initalise property's default value, if any } + return self; } diff --git a/samples/client/petstore/objc/SwaggerClient/SWGUser.m b/samples/client/petstore/objc/SwaggerClient/SWGUser.m index bdb26cf36a6..ac059240e7f 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGUser.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGUser.m @@ -4,11 +4,12 @@ - (instancetype)init { self = [super init]; - if (self) - { + + if (self) { // initalise property's default value, if any } + return self; } diff --git a/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m b/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m index 75cf8d51b0e..faf0f928baf 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m @@ -331,11 +331,11 @@ static SWGUserApi* singletonAPI = nil; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; - if(username != nil) { + if (username != nil) { queryParams[@"username"] = username; } - if(password != nil) { + if (password != nil) { queryParams[@"password"] = password; }