Added outputAsLibrary, onlyInterfaces additional-properties to go-server (#11563)

This commit is contained in:
Spencer Stolworthy 2022-03-08 10:48:05 -07:00 committed by GitHub
parent 94b1440074
commit 6bc50ee57f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 3 deletions

View File

@ -22,6 +22,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|enumClassPrefix|Prefix enum with class name| |false|
|featureCORS|Enable Cross-Origin Resource Sharing middleware| |false|
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
|onlyInterfaces|Exclude default service creators from output; only generate interfaces| |false|
|outputAsLibrary|Exclude main.go, go.mod, and Dockerfile from output| |false|
|packageName|Go package name (convention: lowercase).| |openapi|
|packageVersion|Go package version.| |1.0.0|
|router|Specify the router which should be used.|<dl><dt>**mux**</dt><dd>mux</dd><dt>**chi**</dt><dd>chi</dd></dl>|mux|

View File

@ -53,6 +53,8 @@ public class GoServerCodegen extends AbstractGoCodegen {
protected String sourceFolder = "go";
protected Boolean corsFeatureEnabled = false;
protected Boolean addResponseHeaders = false;
protected Boolean outputAsLibrary = false;
protected Boolean onlyInterfaces = false;
public GoServerCodegen() {
@ -109,6 +111,18 @@ public class GoServerCodegen extends AbstractGoCodegen {
optAddResponseHeaders.defaultValue(addResponseHeaders.toString());
cliOptions.add(optAddResponseHeaders);
// option to exclude service factories; only interfaces are rendered
CliOption optOnlyInterfaces = new CliOption("onlyInterfaces", "Exclude default service creators from output; only generate interfaces");
optOnlyInterfaces.setType("bool");
optOnlyInterfaces.defaultValue(onlyInterfaces.toString());
cliOptions.add(optOnlyInterfaces);
// option to exclude main package (main.go), Dockerfile, and go.mod files
CliOption optOutputAsLibrary = new CliOption("outputAsLibrary", "Exclude main.go, go.mod, and Dockerfile from output");
optOutputAsLibrary.setType("bool");
optOutputAsLibrary.defaultValue(outputAsLibrary.toString());
cliOptions.add(optOutputAsLibrary);
/*
* Models. You can write model files using the modelTemplateFiles map.
* if you want to create one template for file, you can do so here.
@ -214,6 +228,22 @@ public class GoServerCodegen extends AbstractGoCodegen {
additionalProperties.put("addResponseHeaders", addResponseHeaders);
}
if (additionalProperties.containsKey("onlyInterfaces")) {
this.setOnlyInterfaces(convertPropertyToBooleanAndWriteBack("onlyInterfaces"));
} else {
additionalProperties.put("onlyInterfaces", onlyInterfaces);
}
if (this.onlyInterfaces) {
apiTemplateFiles.remove("service.mustache");
}
if (additionalProperties.containsKey("outputAsLibrary")) {
this.setOutputAsLibrary(convertPropertyToBooleanAndWriteBack("outputAsLibrary"));
} else {
additionalProperties.put("outputAsLibrary", outputAsLibrary);
}
if (additionalProperties.containsKey(CodegenConstants.ENUM_CLASS_PREFIX)) {
setEnumClassPrefix(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.ENUM_CLASS_PREFIX).toString()));
if (enumClassPrefix) {
@ -238,10 +268,12 @@ public class GoServerCodegen extends AbstractGoCodegen {
* entire object tree available. If the input file has a suffix of `.mustache
* it will be processed by the template engine. Otherwise, it will be copied
*/
if (!outputAsLibrary) {
supportingFiles.add(new SupportingFile("main.mustache", "", "main.go"));
supportingFiles.add(new SupportingFile("Dockerfile.mustache", "", "Dockerfile"));
supportingFiles.add(new SupportingFile("go.mod.mustache", "", "go.mod"));
}
supportingFiles.add(new SupportingFile("openapi.mustache", "api", "openapi.yaml"));
supportingFiles.add(new SupportingFile("main.mustache", "", "main.go"));
supportingFiles.add(new SupportingFile("Dockerfile.mustache", "", "Dockerfile"));
supportingFiles.add(new SupportingFile("go.mod.mustache", "", "go.mod"));
supportingFiles.add(new SupportingFile("routers.mustache", sourceFolder, "routers.go"));
supportingFiles.add(new SupportingFile("logger.mustache", sourceFolder, "logger.go"));
supportingFiles.add(new SupportingFile("impl.mustache", sourceFolder, "impl.go"));
@ -363,6 +395,14 @@ public class GoServerCodegen extends AbstractGoCodegen {
this.addResponseHeaders = addResponseHeaders;
}
public void setOnlyInterfaces(Boolean onlyInterfaces) {
this.onlyInterfaces = onlyInterfaces;
}
public void setOutputAsLibrary(Boolean outputAsLibrary) {
this.outputAsLibrary = outputAsLibrary;
}
@Override
protected void updateModelForObject(CodegenModel m, Schema schema) {
/**