From 6b2f15b2cf342377bba4679ef2f7a094a7c29231 Mon Sep 17 00:00:00 2001 From: wing328 Date: Tue, 22 Dec 2015 00:12:18 +0800 Subject: [PATCH] add packageName support to golang --- .../codegen/languages/GoClientCodegen.java | 61 ++++++++++++++----- .../src/main/resources/go/README.mustache | 12 ++++ .../src/main/resources/go/api.mustache | 6 +- .../src/main/resources/go/model.mustache | 2 +- .../client/petstore/go/swagger/ApiClient.go | 33 ---------- samples/client/petstore/go/swagger/PetApi.go | 33 ++++++++-- .../client/petstore/go/swagger/StoreApi.go | 4 -- samples/client/petstore/go/swagger/UserApi.go | 4 -- 8 files changed, 89 insertions(+), 66 deletions(-) create mode 100644 modules/swagger-codegen/src/main/resources/go/README.mustache delete mode 100644 samples/client/petstore/go/swagger/ApiClient.go diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/GoClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/GoClientCodegen.java index 81008d552cba..a7488983d400 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/GoClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/GoClientCodegen.java @@ -16,10 +16,8 @@ import org.slf4j.LoggerFactory; public class GoClientCodegen extends DefaultCodegen implements CodegenConfig { static Logger LOGGER = LoggerFactory.getLogger(GoClientCodegen.class); - protected String invokerPackage = "swagger"; - protected String groupId = "io.swagger"; - protected String artifactId = "swagger-go-client"; - protected String artifactVersion = "1.0.0"; + protected String packageName = "swagger"; + protected String packageVersion = "1.0.0"; public CodegenType getTag() { return CodegenType.CLIENT; @@ -39,8 +37,6 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig { modelTemplateFiles.put("model.mustache", ".go"); apiTemplateFiles.put("api.mustache", ".go"); templateDir = "go"; - apiPackage = invokerPackage; - modelPackage = invokerPackage; reservedWords = new HashSet ( Arrays.asList( @@ -51,11 +47,6 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig { "continue", "for", "import", "return", "var") ); - additionalProperties.put("invokerPackage", invokerPackage); - /*additionalProperties.put("groupId", groupId); - additionalProperties.put("artifactId", artifactId); - additionalProperties.put("artifactVersion", artifactVersion);*/ - defaultIncludes = new HashSet( Arrays.asList( "map", @@ -94,16 +85,48 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig { typeMapping.put("Date", "time.Time"); typeMapping.put("DateTime", "time.Time"); typeMapping.put("password", "string"); + typeMapping.put("File", "*os.File"); typeMapping.put("file", "*os.File"); - //typeMapping.put("array", "array"); - //typeMapping.put("map", "map"); importMapping = new HashMap(); importMapping.put("time.Time", "time"); importMapping.put("*os.File", "os"); + cliOptions.clear(); + cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "Go package name (convention: lowercase).") + .defaultValue("swagger")); + cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "Go package version.") + .defaultValue("1.0.0")); + } + @Override + public void processOpts() { + //super.processOpts(); + + if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) { + setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME)); + } + else { + setPackageName("swagger"); + } + + if (additionalProperties.containsKey(CodegenConstants.PACKAGE_VERSION)) { + setPackageVersion((String) additionalProperties.get(CodegenConstants.PACKAGE_VERSION)); + } + else { + setPackageVersion("1.0.0"); + } + + additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName); + additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion); + + modelPackage = packageName; + apiPackage = packageName; + + supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); + } + @Override public String escapeReservedWord(String name) { return "_" + name; @@ -111,11 +134,11 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig { @Override public String apiFileFolder() { - return outputFolder + File.separator + invokerPackage; + return outputFolder + File.separator + packageName; } public String modelFileFolder() { - return outputFolder + File.separator + invokerPackage; + return outputFolder + File.separator + packageName; } @Override @@ -246,4 +269,12 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig { && !languageSpecificPrimitives.contains(type); } + public void setPackageName(String packageName) { + this.packageName = packageName; + } + + public void setPackageVersion(String packageVersion) { + this.packageVersion = packageVersion; + } + } diff --git a/modules/swagger-codegen/src/main/resources/go/README.mustache b/modules/swagger-codegen/src/main/resources/go/README.mustache new file mode 100644 index 000000000000..586b35ed3f5a --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/go/README.mustache @@ -0,0 +1,12 @@ +# Go API client for {{packageName}} + +## Overview +This API client was generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. By using the [swagger-spec](https://github.com/swagger-api/swagger-spec) from a remote server, you can easily generate an API client. + + +## Installation +Put the package under your project folder and add the following in import: +``` + "./{{packageName}}" +``` + diff --git a/modules/swagger-codegen/src/main/resources/go/api.mustache b/modules/swagger-codegen/src/main/resources/go/api.mustache index d009ad10ef28..ae7fb8412b0c 100644 --- a/modules/swagger-codegen/src/main/resources/go/api.mustache +++ b/modules/swagger-codegen/src/main/resources/go/api.mustache @@ -1,11 +1,9 @@ -package {{invokerPackage}} +package {{packageName}} {{#operations}} import ( -// "encoding/json" "strings" "fmt" -// "log" "github.com/dghubble/sling" {{#imports}} "{{import}}" {{/imports}} @@ -13,8 +11,6 @@ import ( type {{classname}} struct { basePath string - apiClient ApiClient - //sling *sling.Sling } func New{{classname}}() *{{classname}}{ diff --git a/modules/swagger-codegen/src/main/resources/go/model.mustache b/modules/swagger-codegen/src/main/resources/go/model.mustache index bbd7e772d0e7..83ba416883d1 100644 --- a/modules/swagger-codegen/src/main/resources/go/model.mustache +++ b/modules/swagger-codegen/src/main/resources/go/model.mustache @@ -1,4 +1,4 @@ -package {{invokerPackage}} +package {{packageName}} {{#models}} import ( diff --git a/samples/client/petstore/go/swagger/ApiClient.go b/samples/client/petstore/go/swagger/ApiClient.go deleted file mode 100644 index 420a7df21ed8..000000000000 --- a/samples/client/petstore/go/swagger/ApiClient.go +++ /dev/null @@ -1,33 +0,0 @@ -package swagger - -import ( -// "encoding/json" - "os" -// "fmt" - "log" -// "net/url" -// napping "github.com/jmcvetta/napping" -) - -type ApiClient struct { - basePath string - logger *log.Logger -} - -func NewApiClient() *ApiClient { - return &ApiClient { - basePath: "http://petstore.swagger.io/v2", - logger: log.New(os.Stderr, "", log.LstdFlags)} -} - -func NewApiClientWithBasePath(basePath string) *ApiClient { - return &ApiClient { - basePath: basePath, - logger: log.New(os.Stderr, "", log.LstdFlags)} -} - - -func (a *ApiClient) CallApi(basePath string, path string, httpMethod string, queryParams map[string]string, headerParams map[string]string, formParams map[string]string, fileParams map[string]string, bodyParams map[string]string, contentType []string) { - a.logger.Printf("Requesting %v\n%v\n%v\n%v\n%v\n%v\n%v\n%v\n%v\n", basePath, path, httpMethod, queryParams, headerParams, formParams, fileParams, bodyParams, contentType) - -} diff --git a/samples/client/petstore/go/swagger/PetApi.go b/samples/client/petstore/go/swagger/PetApi.go index a4ed5033d570..51b32061e0fe 100644 --- a/samples/client/petstore/go/swagger/PetApi.go +++ b/samples/client/petstore/go/swagger/PetApi.go @@ -1,18 +1,14 @@ package swagger import ( -// "encoding/json" "strings" "fmt" -// "log" "github.com/dghubble/sling" "os" ) type PetApi struct { basePath string - apiClient ApiClient - //sling *sling.Sling } func NewPetApi() *PetApi{ @@ -264,6 +260,35 @@ func (a PetApi) DeletePet (PetId int64, ApiKey string) (error) { //fmt.Println("DeletePet response: void, ", resp, err) return err } +/** + * downloads an image + * + * @return *os.File + */ +//func (a PetApi) DownloadFile () (*os.File, error) { +func (a PetApi) DownloadFile () (*os.File, error) { + + _sling := sling.New().Get(a.basePath) + + // create path and map variables + path := "/v2/pet/{petId}/downloadImage" + + _sling = _sling.Path(path) + + // accept header + accepts := []string { "application/octet-stream" } + for key := range accepts { + _sling = _sling.Set("Accept", accepts[key]) + break // only use the first Accept + } + + + + response := new(*os.File) + _, err := _sling.ReceiveSuccess(response) + //fmt.Println("DownloadFile response: ", response, resp, err) + return *response, err +} /** * uploads an image * diff --git a/samples/client/petstore/go/swagger/StoreApi.go b/samples/client/petstore/go/swagger/StoreApi.go index 4af27d02b7ab..c43d320077c9 100644 --- a/samples/client/petstore/go/swagger/StoreApi.go +++ b/samples/client/petstore/go/swagger/StoreApi.go @@ -1,17 +1,13 @@ package swagger import ( -// "encoding/json" "strings" "fmt" -// "log" "github.com/dghubble/sling" ) type StoreApi struct { basePath string - apiClient ApiClient - //sling *sling.Sling } func NewStoreApi() *StoreApi{ diff --git a/samples/client/petstore/go/swagger/UserApi.go b/samples/client/petstore/go/swagger/UserApi.go index c5da3c81b6d9..9907453bd392 100644 --- a/samples/client/petstore/go/swagger/UserApi.go +++ b/samples/client/petstore/go/swagger/UserApi.go @@ -1,17 +1,13 @@ package swagger import ( -// "encoding/json" "strings" "fmt" -// "log" "github.com/dghubble/sling" ) type UserApi struct { basePath string - apiClient ApiClient - //sling *sling.Sling } func NewUserApi() *UserApi{