Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
* Copyright 2018 SmartBear Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openapitools.codegen.languages;
import com.google.common.collect.Iterables;
import com.samskivert.mustache.Mustache;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability;
import org.openapitools.codegen.meta.features.*;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.model.OperationMap;
import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.utils.ModelUtils;
import org.openapitools.codegen.utils.ProcessUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.*;
import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER;
import static org.openapitools.codegen.utils.StringUtils.camelize;
public class GoClientCodegen extends AbstractGoCodegen {
private final Logger LOGGER = LoggerFactory.getLogger(GoClientCodegen.class);
@Setter protected String packageVersion = "1.0.0";
protected String apiDocPath = "docs/";
protected String modelDocPath = "docs/";
protected String modelFileFolder = null;
public static final String WITH_XML = "withXml";
public static final String STRUCT_PREFIX = "structPrefix";
public static final String WITH_AWSV4_SIGNATURE = "withAWSV4Signature";
public static final String GENERATE_INTERFACES = "generateInterfaces";
public static final String MODEL_FILE_FOLDER = "modelFileFolder";
public static final String WITH_GO_MOD = "withGoMod";
@Setter protected String goImportAlias = "openapiclient";
protected boolean isGoSubmodule = false;
@Setter protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup
// A cache to efficiently lookup schema `toModelName()` based on the schema Key
private Map schemaKeyToModelNameCache = new HashMap<>();
public GoClientCodegen() {
super();
modifyFeatureSet(features -> features
.includeDocumentationFeatures(DocumentationFeature.Readme)
.wireFormatFeatures(EnumSet.of(WireFormatFeature.JSON, WireFormatFeature.XML))
.securityFeatures(EnumSet.of(
SecurityFeature.BasicAuth,
SecurityFeature.BearerToken,
SecurityFeature.ApiKey,
SecurityFeature.OAuth2_Implicit,
SecurityFeature.SignatureAuth,
SecurityFeature.AWSV4Signature
))
.includeGlobalFeatures(
GlobalFeature.ParameterizedServer
)
.excludeGlobalFeatures(
GlobalFeature.XMLStructureDefinitions,
GlobalFeature.Callbacks,
GlobalFeature.LinkObjects,
GlobalFeature.ParameterStyling
)
.includeSchemaSupportFeatures(
SchemaSupportFeature.anyOf,
SchemaSupportFeature.oneOf,
SchemaSupportFeature.allOf
)
.excludeSchemaSupportFeatures(
SchemaSupportFeature.Polymorphism
)
.includeParameterFeatures(
ParameterFeature.Cookie
)
.includeClientModificationFeatures(
ClientModificationFeature.BasePath,
ClientModificationFeature.UserAgent
)
.includeDataTypeFeatures(
DataTypeFeature.AnyType
)
);
generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata).stability(Stability.STABLE).build();
outputFolder = "generated-code/go";
embeddedTemplateDir = templateDir = "go";
apiTemplateFiles.put("api.mustache", ".go");
modelTemplateFiles.put("model.mustache", ".go");
apiTestTemplateFiles.put("api_test.mustache", ".go");
modelDocTemplateFiles.put("model_doc.mustache", ".md");
apiDocTemplateFiles.put("api_doc.mustache", ".md");
// default HIDE_GENERATION_TIMESTAMP to true
hideGenerationTimestamp = Boolean.TRUE;
cliOptions.add(CliOption.newBoolean(CodegenConstants.IS_GO_SUBMODULE, CodegenConstants.IS_GO_SUBMODULE_DESC));
cliOptions.add(CliOption.newBoolean(WITH_XML, "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)"));
cliOptions.add(CliOption.newBoolean(CodegenConstants.ENUM_CLASS_PREFIX, CodegenConstants.ENUM_CLASS_PREFIX_DESC));
cliOptions.add(CliOption.newBoolean(STRUCT_PREFIX, "whether to prefix struct with the class name. e.g. DeletePetOpts => PetApiDeletePetOpts"));
cliOptions.add(CliOption.newBoolean(WITH_AWSV4_SIGNATURE, "whether to include AWS v4 signature support"));
cliOptions.add(CliOption.newBoolean(GENERATE_INTERFACES, "Generate interfaces for api classes"));
// option to change the order of form/body parameter
cliOptions.add(CliOption.newBoolean(
CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS,
CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS_DESC)
.defaultValue(Boolean.FALSE.toString()));
cliOptions.add(new CliOption(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP_DESC).defaultValue("false"));
// option to change how we process + set the data in the 'additionalProperties' keyword.
CliOption disallowAdditionalPropertiesIfNotPresentOpt = CliOption.newBoolean(
CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT,
CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_DESC).defaultValue(Boolean.TRUE.toString());
Map disallowAdditionalPropertiesIfNotPresentOpts = new HashMap<>();
disallowAdditionalPropertiesIfNotPresentOpts.put("false",
"The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.");
disallowAdditionalPropertiesIfNotPresentOpts.put("true",
"Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.");
disallowAdditionalPropertiesIfNotPresentOpt.setEnum(disallowAdditionalPropertiesIfNotPresentOpts);
cliOptions.add(disallowAdditionalPropertiesIfNotPresentOpt);
this.setDisallowAdditionalPropertiesIfNotPresent(true);
cliOptions.add(CliOption.newBoolean(WITH_GO_MOD, "Generate go.mod and go.sum", true));
cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_MARSHAL_JSON, CodegenConstants.GENERATE_MARSHAL_JSON_DESC, true));
cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_UNMARSHAL_JSON, CodegenConstants.GENERATE_UNMARSHAL_JSON_DESC, true));
this.setWithGoMod(true);
}
/**
* Configures a friendly name for the generator. This will be used by the
* generator to select the library with the -g flag.
*
* @return the friendly name for the generator
*/
@Override
public String getName() {
return "go";
}
/**
* Configures the type of generator.
*
* @return the CodegenType for this generator
* @see CodegenType
*/
@Override
public CodegenType getTag() {
return CodegenType.CLIENT;
}
@Override
public String toGetter(String name) {
return "Get" + getterAndSetterCapitalize(name);
}
/**
* Returns human-friendly help for the generator. Provide the consumer with help
* tips, parameters here
*
* @return A string value for the help message
*/
@Override
public String getHelp() {
return "Generates a Go client library.";
}
@Override
public void processOpts() {
this.setLegacyDiscriminatorBehavior(false);
super.processOpts();
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
} else {
setPackageName("openapi");
}
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);
additionalProperties.put("apiDocPath", apiDocPath);
additionalProperties.put("modelDocPath", modelDocPath);
modelPackage = packageName;
apiPackage = packageName;
if (additionalProperties.containsKey(WITH_AWSV4_SIGNATURE)) {
setWithAWSV4Signature(Boolean.parseBoolean(additionalProperties.get(WITH_AWSV4_SIGNATURE).toString()));
additionalProperties.put(WITH_AWSV4_SIGNATURE, withAWSV4Signature);
}
if (additionalProperties.containsKey(WITH_XML)) {
setWithXml(Boolean.parseBoolean(additionalProperties.get(WITH_XML).toString()));
additionalProperties.put(WITH_XML, withXml);
}
if (additionalProperties.containsKey(CodegenConstants.ENUM_CLASS_PREFIX)) {
setEnumClassPrefix(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.ENUM_CLASS_PREFIX).toString()));
additionalProperties.put(CodegenConstants.ENUM_CLASS_PREFIX, enumClassPrefix);
}
if (additionalProperties.containsKey(CodegenConstants.IS_GO_SUBMODULE)) {
setIsGoSubmodule(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.IS_GO_SUBMODULE).toString()));
additionalProperties.put(CodegenConstants.IS_GO_SUBMODULE, isGoSubmodule);
}
if (additionalProperties.containsKey(STRUCT_PREFIX)) {
setStructPrefix(Boolean.parseBoolean(additionalProperties.get(STRUCT_PREFIX).toString()));
additionalProperties.put(STRUCT_PREFIX, structPrefix);
}
if (additionalProperties.containsKey(GENERATE_INTERFACES)) {
setGenerateInterfaces(Boolean.parseBoolean(additionalProperties.get(GENERATE_INTERFACES).toString()));
additionalProperties.put(GENERATE_INTERFACES, generateInterfaces);
}
// Generate the 'signing.py' module, but only if the 'HTTP signature' security scheme is specified in the OAS.
Map securitySchemeMap = openAPI != null ?
(openAPI.getComponents() != null ? openAPI.getComponents().getSecuritySchemes() : null) : null;
List authMethods = fromSecurity(securitySchemeMap);
if (ProcessUtils.hasHttpSignatureMethods(authMethods)) {
supportingFiles.add(new SupportingFile("signing.mustache", "", "signing.go"));
}
if (additionalProperties.containsKey("goImportAlias")) {
setGoImportAlias(additionalProperties.get("goImportAlias").toString());
} else {
additionalProperties.put("goImportAlias", goImportAlias);
}
if (additionalProperties.containsKey(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP)) {
setUseOneOfDiscriminatorLookup(convertPropertyToBooleanAndWriteBack(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP));
} else {
additionalProperties.put(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, getUseOneOfDiscriminatorLookup());
}
if (additionalProperties.containsKey(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT)) {
this.setDisallowAdditionalPropertiesIfNotPresent(Boolean.parseBoolean(additionalProperties
.get(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT).toString()));
}
if (additionalProperties.containsKey(MODEL_FILE_FOLDER)) {
modelFileFolder = additionalProperties.get(MODEL_FILE_FOLDER).toString();
}
if (additionalProperties.containsKey(WITH_GO_MOD)) {
setWithGoMod(Boolean.parseBoolean(additionalProperties.get(WITH_GO_MOD).toString()));
additionalProperties.put(WITH_GO_MOD, withGoMod);
} else {
additionalProperties.put(WITH_GO_MOD, true);
}
if (additionalProperties.containsKey(CodegenConstants.GENERATE_MARSHAL_JSON)) {
setGenerateMarshalJSON(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.GENERATE_MARSHAL_JSON).toString()));
}
if (additionalProperties.containsKey(CodegenConstants.GENERATE_UNMARSHAL_JSON)) {
setGenerateUnmarshalJSON(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.GENERATE_UNMARSHAL_JSON).toString()));
}
// add lambda for mustache templates to handle oneOf/anyOf naming
// e.g. []string => ArrayOfString
additionalProperties.put("lambda.type-to-name", (Mustache.Lambda) (fragment, writer) -> writer.write(typeToName(fragment.execute())));
supportingFiles.add(new SupportingFile("openapi.mustache", "api", "openapi.yaml"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.go"));
supportingFiles.add(new SupportingFile("client.mustache", "", "client.go"));
supportingFiles.add(new SupportingFile("response.mustache", "", "response.go"));
if ((boolean)additionalProperties.get(WITH_GO_MOD)) {
supportingFiles.add(new SupportingFile("go.mod.mustache", "", "go.mod"));
supportingFiles.add(new SupportingFile("go.sum.mustache", "", "go.sum"));
}
supportingFiles.add(new SupportingFile(".travis.yml", "", ".travis.yml"));
supportingFiles.add(new SupportingFile("utils.mustache", "", "utils.go"));
}
public boolean getUseOneOfDiscriminatorLookup() {
return this.useOneOfDiscriminatorLookup;
}
public void setIsGoSubmodule(boolean isGoSubmodule) {
this.isGoSubmodule = isGoSubmodule;
}
/**
* Location to write api files. You can use the apiPackage() as defined when the class is
* instantiated
*/
@Override
public String apiFileFolder() {
return outputFolder + File.separator;
}
/**
* Location of created model files (it can be overriden using --additional-properties in openapi-generator-cli
*/
@Override
public String modelFileFolder() {
String modelFileFolderPath = outputFolder + File.separator;
if(modelFileFolder != null) {
modelFileFolderPath = modelFileFolderPath + modelFileFolder + File.separator;
}
return modelFileFolderPath.replace("/", File.separator);
}
@Override
public String apiTestFileFolder() {
return outputFolder + File.separator + "test" + File.separator;
}
@Override
public String apiDocFileFolder() {
return (outputFolder + "/" + apiDocPath).replace('/', File.separatorChar);
}
@Override
public String modelDocFileFolder() {
return (outputFolder + "/" + modelDocPath).replace('/', File.separatorChar);
}
@Override
public String toModelDocFilename(String name) {
return toModelName(name);
}
@Override
public String toModelName(String name) {
if (schemaKeyToModelNameCache.containsKey(name)) {
return schemaKeyToModelNameCache.get(name);
}
// underscoring would also lowercase the whole name, thus losing acronyms which are in capitals
String camelizedName = camelize(toModel(name, false));
schemaKeyToModelNameCache.put(name, camelizedName);
return camelizedName;
}
@Override
public String escapeReservedWord(String name) {
if (this.reservedWordsMappings().containsKey(name)) {
return this.reservedWordsMappings().get(name);
}
return name + '_';
}
@Override
public String toEnumDefaultValue(String value, String datatype) {
String prefix = "";
if (enumClassPrefix) {
prefix = datatype.toUpperCase(Locale.ROOT) + "_";
}
return prefix + value;
}
@Override
public void updateCodegenPropertyEnum(CodegenProperty var) {
// make sure the inline enums have plain defaults (e.g. string, int, float)
String enumDefault = null;
if (var.isEnum && var.defaultValue != null) {
enumDefault = var.defaultValue;
}
super.updateCodegenPropertyEnum(var);
if (var.isEnum && enumDefault != null) {
var.defaultValue = enumDefault;
}
}
/**
* Determines if at least one of the allOf pieces of a schema are of type string
*
* @param schema
* @return
*/
private boolean isAllOfStringSchema(Schema schema) {
if (schema.getAllOf() != null) {
Iterator it = schema.getAllOf().iterator();
while (it.hasNext()) {
Schema childSchema = ModelUtils.getReferencedSchema(this.openAPI, it.next());
if (ModelUtils.isStringSchema(childSchema)) {
return true;
}
}
}
return false;
}
@Override
public String toDefaultValue(Schema p) {
p = ModelUtils.getReferencedSchema(this.openAPI, p);
if (ModelUtils.isStringSchema(p) || isAllOfStringSchema(p)) {
Object defaultObj = p.getDefault();
if (defaultObj != null) {
return "\"" + escapeText(String.valueOf(defaultObj)) + "\"";
}
return null;
}
return super.toDefaultValue(p);
}
@Override
public CodegenProperty fromProperty(String name, Schema p, boolean required) {
CodegenProperty prop = super.fromProperty(name, p, required);
String cc = camelize(prop.name, LOWERCASE_FIRST_LETTER);
if (isReservedWord(cc)) {
cc = escapeReservedWord(cc); // e.g. byte => byte_
}
prop.nameInCamelCase = cc;
return prop;
}
@Override
public ModelsMap postProcessModels(ModelsMap objs) {
// The superclass determines the list of required golang imports. The actual list of imports
// depends on which types are used, some of which are changed in the code below (but then preserved
// and used through x-go-base-type in templates). So super.postProcessModels
// must be invoked at the beginning of this method.
objs = super.postProcessModels(objs);
List