From e7f68287c114e4b429feaf77b3bc396c147bf607 Mon Sep 17 00:00:00 2001 From: Guo Huang Date: Mon, 9 May 2016 22:01:32 -0700 Subject: [PATCH] updated function to support multiple collection formats --- .../src/main/resources/go/api.mustache | 4 ++-- .../src/main/resources/go/api_client.mustache | 17 +++++++++++++---- .../petstore/go/go-petstore/api_client.go | 17 +++++++++++++---- .../client/petstore/go/go-petstore/pet_api.go | 4 ++-- .../client/petstore/go/go-petstore/user_api.go | 4 ++-- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/go/api.mustache b/modules/swagger-codegen/src/main/resources/go/api.mustache index 6402a0ced7..ee828484ef 100644 --- a/modules/swagger-codegen/src/main/resources/go/api.mustache +++ b/modules/swagger-codegen/src/main/resources/go/api.mustache @@ -86,11 +86,11 @@ func (a {{classname}}) {{nickname}}({{#allParams}}{{paramName}} {{{dataType}}}{{ queryParams.Add("{{paramName}}", value) } } else { - queryParams.Add("{{paramName}}", a.Configuration.APIClient.ParameterToString({{paramName}})) + queryParams.Add("{{paramName}}", a.Configuration.APIClient.ParameterToString({{paramName}}, collectionFormat)) } {{/isListContainer}} {{^isListContainer}} - queryParams.Add("{{paramName}}", a.Configuration.APIClient.ParameterToString({{paramName}})) + queryParams.Add("{{paramName}}", a.Configuration.APIClient.ParameterToString({{paramName}}, "")) {{/isListContainer}} {{/queryParams}}{{/hasQueryParams}} diff --git a/modules/swagger-codegen/src/main/resources/go/api_client.mustache b/modules/swagger-codegen/src/main/resources/go/api_client.mustache index c4e6c63522..2fa6d14ef2 100644 --- a/modules/swagger-codegen/src/main/resources/go/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/go/api_client.mustache @@ -79,12 +79,21 @@ func (c *APIClient) CallAPI(path string, method string, return nil, fmt.Errorf("invalid method %v", method) } -func (c *APIClient) ParameterToString(obj interface{}) string { +func (c *APIClient) ParameterToString(obj interface{},collectionFormat string) string { if reflect.TypeOf(obj).String() == "[]string" { - return strings.Join(obj.([]string), ",") - } else { - return obj.(string) + switch collectionFormat { + case "pipes": + return strings.Join(obj.([]string), "|") + case "ssv": + return strings.Join(obj.([]string), " ") + case "tsv": + return strings.Join(obj.([]string), "\t") + case "csv" : + return strings.Join(obj.([]string), ",") + } } + + return obj.(string) } func prepareRequest(postBody interface{}, diff --git a/samples/client/petstore/go/go-petstore/api_client.go b/samples/client/petstore/go/go-petstore/api_client.go index 25e91cabbd..fe73dc5682 100644 --- a/samples/client/petstore/go/go-petstore/api_client.go +++ b/samples/client/petstore/go/go-petstore/api_client.go @@ -79,12 +79,21 @@ func (c *APIClient) CallAPI(path string, method string, return nil, fmt.Errorf("invalid method %v", method) } -func (c *APIClient) ParameterToString(obj interface{}) string { +func (c *APIClient) ParameterToString(obj interface{},collectionFormat string) string { if reflect.TypeOf(obj).String() == "[]string" { - return strings.Join(obj.([]string), ",") - } else { - return obj.(string) + switch collectionFormat { + case "pipes": + return strings.Join(obj.([]string), "|") + case "ssv": + return strings.Join(obj.([]string), " ") + case "tsv": + return strings.Join(obj.([]string), "\t") + case "csv" : + return strings.Join(obj.([]string), ",") + } } + + return obj.(string) } func prepareRequest(postBody interface{}, diff --git a/samples/client/petstore/go/go-petstore/pet_api.go b/samples/client/petstore/go/go-petstore/pet_api.go index 613006aef1..34efe36d9b 100644 --- a/samples/client/petstore/go/go-petstore/pet_api.go +++ b/samples/client/petstore/go/go-petstore/pet_api.go @@ -207,7 +207,7 @@ func (a PetApi) FindPetsByStatus(status []string) ([]Pet, *APIResponse, error) { queryParams.Add("status", value) } } else { - queryParams.Add("status", a.Configuration.APIClient.ParameterToString(status)) + queryParams.Add("status", a.Configuration.APIClient.ParameterToString(status, collectionFormat)) } @@ -279,7 +279,7 @@ func (a PetApi) FindPetsByTags(tags []string) ([]Pet, *APIResponse, error) { queryParams.Add("tags", value) } } else { - queryParams.Add("tags", a.Configuration.APIClient.ParameterToString(tags)) + queryParams.Add("tags", a.Configuration.APIClient.ParameterToString(tags, collectionFormat)) } diff --git a/samples/client/petstore/go/go-petstore/user_api.go b/samples/client/petstore/go/go-petstore/user_api.go index 150bea6752..af0ea7b85f 100644 --- a/samples/client/petstore/go/go-petstore/user_api.go +++ b/samples/client/petstore/go/go-petstore/user_api.go @@ -368,8 +368,8 @@ func (a UserApi) LoginUser(username string, password string) (*string, *APIRespo for key := range a.Configuration.DefaultHeader { headerParams[key] = a.Configuration.DefaultHeader[key] } - queryParams.Add("username", a.Configuration.APIClient.ParameterToString(username)) - queryParams.Add("password", a.Configuration.APIClient.ParameterToString(password)) + queryParams.Add("username", a.Configuration.APIClient.ParameterToString(username, "")) + queryParams.Add("password", a.Configuration.APIClient.ParameterToString(password, "")) // to determine the Content-Type header