|
| 1 | +package io.swagger.codegen.v3.generators.go; |
| 2 | + |
| 3 | +import io.swagger.codegen.v3.CliOption; |
| 4 | +import io.swagger.codegen.v3.CodegenType; |
| 5 | +import io.swagger.codegen.v3.SupportingFile; |
| 6 | +import org.apache.commons.lang3.StringUtils; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.util.Arrays; |
| 10 | + |
| 11 | +public class GoClientCodegen extends AbstractGoCodegen { |
| 12 | + protected String packageVersion = "1.0.0"; |
| 13 | + protected String apiDocPath = "docs/"; |
| 14 | + protected String modelDocPath = "docs/"; |
| 15 | + public static final String WITH_XML = "withXml"; |
| 16 | + |
| 17 | + public GoClientCodegen() { |
| 18 | + this.outputFolder = "generated-code/go"; |
| 19 | + this.modelTemplateFiles.put("model.mustache", ".go"); |
| 20 | + this.apiTemplateFiles.put("api.mustache", ".go"); |
| 21 | + this.modelDocTemplateFiles.put("model_doc.mustache", ".md"); |
| 22 | + this.apiDocTemplateFiles.put("api_doc.mustache", ".md"); |
| 23 | + this.embeddedTemplateDir = this.templateDir = "go"; |
| 24 | + this.hideGenerationTimestamp = Boolean.TRUE; |
| 25 | + this.setReservedWordsLowerCase(Arrays.asList("string", "bool", "uint", "uint8", "uint16", "uint32", "uint64", "int", "int8", "int16", "int32", "int64", "float32", "float64", "complex64", "complex128", "rune", "byte", "uintptr", "break", "default", "func", "interface", "select", "case", "defer", "go", "map", "struct", "chan", "else", "goto", "package", "switch", "const", "fallthrough", "if", "range", "type", "continue", "for", "import", "return", "var", "error", "ApiResponse", "nil")); |
| 26 | + this.cliOptions.add((new CliOption("packageVersion", "Go package version.")).defaultValue("1.0.0")); |
| 27 | + this.cliOptions.add(CliOption.newBoolean("withXml", "whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)")); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public String getDefaultTemplateDir() { |
| 32 | + return "go"; |
| 33 | + } |
| 34 | + |
| 35 | + public void processOpts() { |
| 36 | + super.processOpts(); |
| 37 | + |
| 38 | + if (StringUtils.isBlank(templateDir)) { |
| 39 | + embeddedTemplateDir = templateDir = getTemplateDir(); |
| 40 | + } |
| 41 | + |
| 42 | + if (this.additionalProperties.containsKey("packageName")) { |
| 43 | + this.setPackageName((String)this.additionalProperties.get("packageName")); |
| 44 | + } else { |
| 45 | + this.setPackageName("swagger"); |
| 46 | + } |
| 47 | + |
| 48 | + if (this.additionalProperties.containsKey("packageVersion")) { |
| 49 | + this.setPackageVersion((String)this.additionalProperties.get("packageVersion")); |
| 50 | + } else { |
| 51 | + this.setPackageVersion("1.0.0"); |
| 52 | + } |
| 53 | + |
| 54 | + this.additionalProperties.put("packageName", this.packageName); |
| 55 | + this.additionalProperties.put("packageVersion", this.packageVersion); |
| 56 | + this.additionalProperties.put("apiDocPath", this.apiDocPath); |
| 57 | + this.additionalProperties.put("modelDocPath", this.modelDocPath); |
| 58 | + this.modelPackage = this.packageName; |
| 59 | + this.apiPackage = this.packageName; |
| 60 | + this.supportingFiles.add(new SupportingFile("swagger.mustache", "api", "swagger.yaml")); |
| 61 | + this.supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); |
| 62 | + this.supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); |
| 63 | + this.supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore")); |
| 64 | + this.supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.go")); |
| 65 | + this.supportingFiles.add(new SupportingFile("client.mustache", "", "client.go")); |
| 66 | + this.supportingFiles.add(new SupportingFile("response.mustache", "", "response.go")); |
| 67 | + this.supportingFiles.add(new SupportingFile(".travis.yml", "", ".travis.yml")); |
| 68 | + if (this.additionalProperties.containsKey("withXml")) { |
| 69 | + this.setWithXml(Boolean.parseBoolean(this.additionalProperties.get("withXml").toString())); |
| 70 | + if (this.withXml) { |
| 71 | + this.additionalProperties.put("withXml", "true"); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + public CodegenType getTag() { |
| 78 | + return CodegenType.CLIENT; |
| 79 | + } |
| 80 | + |
| 81 | + public String getName() { |
| 82 | + return "go"; |
| 83 | + } |
| 84 | + |
| 85 | + public String getHelp() { |
| 86 | + return "Generates a Go client library (beta)."; |
| 87 | + } |
| 88 | + |
| 89 | + public String apiFileFolder() { |
| 90 | + return this.outputFolder + File.separator; |
| 91 | + } |
| 92 | + |
| 93 | + public String modelFileFolder() { |
| 94 | + return this.outputFolder + File.separator; |
| 95 | + } |
| 96 | + |
| 97 | + public String apiDocFileFolder() { |
| 98 | + return (this.outputFolder + "/" + this.apiDocPath).replace('/', File.separatorChar); |
| 99 | + } |
| 100 | + |
| 101 | + public String modelDocFileFolder() { |
| 102 | + return (this.outputFolder + "/" + this.modelDocPath).replace('/', File.separatorChar); |
| 103 | + } |
| 104 | + |
| 105 | + public String toModelDocFilename(String name) { |
| 106 | + return this.toModelName(name); |
| 107 | + } |
| 108 | + |
| 109 | + public String toApiDocFilename(String name) { |
| 110 | + return this.toApiName(name); |
| 111 | + } |
| 112 | + |
| 113 | + public void setPackageVersion(String packageVersion) { |
| 114 | + this.packageVersion = packageVersion; |
| 115 | + } |
| 116 | +} |
| 117 | + |
0 commit comments