Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add postProcessFile, implement in Go generators to run gofmt #929

Merged
merged 4 commits into from
Sep 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CI/circle_parallel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ if [ "$NODE_INDEX" = "1" ]; then
mvn --quiet verify -Psamples
elif [ "$NODE_INDEX" = "2" ]; then
echo "Running node $NODE_INDEX to test ensure-up-to-date"
export GO_FMT_PATH=`which gofmt`
./bin/utils/ensure-up-to-date
else
echo "Running node $NODE_INDEX to test CI/pom.xml.circleci.java7 ..."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.oas.models.servers.ServerVariable;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -254,4 +255,6 @@ public interface CodegenConfig {

String sanitizeName(String name);

void postProcessFile(File file, String fileType);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4688,4 +4688,18 @@ public List<CodegenServerVariable> fromServerVariables(Map<String, ServerVariabl
private void setParameterNullable(CodegenParameter parameter, CodegenProperty property) {
parameter.isNullable = property.isNullable;
}

/**
* Post-process the auto-generated file, e.g. using go-fmt to format the Go code. The file type can be "model-test",
* "model-doc", "model", "api", "api-test", "api-doc", "supporting-mustache", "supporting-common",
* "openapi-generator-ignore", "openapi-generator-version"
*
* TODO: store these values in enum instead
*
* @param file file to be processed
* @param fileType file type
*/
public void postProcessFile(File file, String fileType) {
LOGGER.info("Post processing file {} ({})", file, fileType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ private void generateModelTests(List<File> files, Map<String, Object> models, St
File written = processTemplateToFile(models, templateName, filename);
if (written != null) {
files.add(written);
config.postProcessFile(written, "model-test");
}
}
}
Expand All @@ -295,6 +296,23 @@ private void generateModelDocumentation(List<File> files, Map<String, Object> mo
File written = processTemplateToFile(models, templateName, filename);
if (written != null) {
files.add(written);
config.postProcessFile(written, "model-doc");
}
}
}

private void generateModel(List<File> files, Map<String, Object> models, String modelName) throws IOException {
for (String templateName : config.modelTemplateFiles().keySet()) {
String suffix = config.modelTemplateFiles().get(templateName);
String filename = config.modelFileFolder() + File.separator + config.toModelFilename(modelName) + suffix;
if (!config.shouldOverwrite(filename)) {
LOGGER.info("Skipped overwriting " + filename);
continue;
}
File written = processTemplateToFile(models, templateName, filename);
if (written != null) {
files.add(written);
config.postProcessFile(written, "model");
}
}
}
Expand Down Expand Up @@ -442,19 +460,11 @@ private Model getParent(Model model) {

allModels.add(modelTemplate);

for (String templateName : config.modelTemplateFiles().keySet()) {
String suffix = config.modelTemplateFiles().get(templateName);
String filename = config.modelFileFolder() + File.separator + config.toModelFilename(modelName) + suffix;
if (!config.shouldOverwrite(filename)) {
LOGGER.info("Skipped overwriting " + filename);
continue;
}
File written = processTemplateToFile(models, templateName, filename);
if (written != null) {
files.add(written);
}
}
// to generate model files
generateModel(files, models, modelName);

if (generateModelTests) {
// to generate model test files
generateModelTests(files, models, modelName);
}
if (generateModelDocumentation) {
Expand Down Expand Up @@ -548,6 +558,7 @@ public int compare(CodegenOperation one, CodegenOperation another) {
File written = processTemplateToFile(operation, templateName, filename);
if (written != null) {
files.add(written);
config.postProcessFile(written, "api");
}
}

Expand All @@ -564,6 +575,7 @@ public int compare(CodegenOperation one, CodegenOperation another) {
File written = processTemplateToFile(operation, templateName, filename);
if (written != null) {
files.add(written);
config.postProcessFile(written, "api-test");
}
}
}
Expand All @@ -581,6 +593,7 @@ public int compare(CodegenOperation one, CodegenOperation another) {
File written = processTemplateToFile(operation, templateName, filename);
if (written != null) {
files.add(written);
config.postProcessFile(written, "api-doc");
}
}
}
Expand Down Expand Up @@ -651,7 +664,9 @@ public Reader getTemplate(String name) {
.compile(template);

writeToFile(outputFilename, tmpl.execute(bundle));
files.add(new File(outputFilename));
File written = new File(outputFilename);
files.add(written);
config.postProcessFile(written, "supporting-mustache");
} else {
InputStream in = null;

Expand All @@ -665,6 +680,7 @@ public Reader getTemplate(String name) {
}
File outputFile = writeInputStreamToFile(outputFilename, in, templateFile);
files.add(outputFile);
config.postProcessFile(outputFile, "supporting-common");
}
} else {
LOGGER.info("Skipped generation of " + outputFilename + " due to rule in .openapi-generator-ignore");
Expand All @@ -688,6 +704,7 @@ public Reader getTemplate(String name) {
throw new RuntimeException("Could not generate supporting file '" + openapiGeneratorIgnore + "'", e);
}
files.add(ignoreFile);
config.postProcessFile(ignoreFile, "openapi-generator-ignore");
}

if (generateMetadata) {
Expand All @@ -696,6 +713,7 @@ public Reader getTemplate(String name) {
try {
writeToFile(versionMetadata, ImplementationVersion.read());
files.add(versionMetadataFile);
config.postProcessFile(ignoreFile, "openapi-generator-version");
} catch (IOException e) {
throw new RuntimeException("Could not generate supporting file '" + versionMetadata + "'", e);
}
Expand Down Expand Up @@ -762,7 +780,7 @@ private Map<String, Object> buildSupportFileBundle(List<Object> allOperations, L
bundle.put("authMethods", authMethods);
bundle.put("hasAuthMethods", true);
}

List<CodegenServer> servers = config.fromServers(openAPI.getServers());
if (servers != null && !servers.isEmpty()) {
bundle.put("servers", servers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.io.FilenameUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.*;

public abstract class AbstractGoCodegen extends DefaultCodegen implements CodegenConfig {
Expand Down Expand Up @@ -603,4 +606,38 @@ public String toDefaultValue(Schema schema) {
return null;
}
}

@Override
public void postProcessFile(File file, String fileType) {
if (file == null) {
return;
}

// only procees the following type (or we can simply rely on the file extension to check if it's a Go file)
Set<String> supportedFileType = new HashSet<String>(
Arrays.asList(
"supporting-mustache",
"model-test",
"model",
"api-test",
"api"));
if (!supportedFileType.contains(fileType)) {
return;
}

String goFmtPath = System.getenv("GO_FMT_PATH");

// only process files with go extension
if ("go".equals(FilenameUtils.getExtension(file.toString()))) {
// currently only support "gofmt -w yourcode.go"
// another way is "go fmt path/to/your/package"
String command = goFmtPath + " -w " + file.toString();
try {
Runtime.getRuntime().exec(command);
} catch (IOException e) {
LOGGER.error("Error running the command: " + command);
}
LOGGER.info("Successfully executed: " + command);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@

package org.openapitools.codegen.languages;

import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Arrays;

public class GoClientCodegen extends AbstractGoCodegen {

private static final Logger LOGGER = LoggerFactory.getLogger(GoClientCodegen.class);

protected String packageVersion = "1.0.0";
protected String apiDocPath = "docs/";
protected String modelDocPath = "docs/";
Expand Down Expand Up @@ -66,6 +71,10 @@ public GoClientCodegen() {
public void processOpts() {
super.processOpts();

if (StringUtils.isEmpty(System.getenv("GO_FMT_PATH"))) {
LOGGER.info("Environment variable GO_FMT_PATH not defined so Go code may not be properly formatted. To define it, try 'export GO_FMT_PATH=/usr/local/bin/gofmt' (Linux/Mac)");
}

if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@

package org.openapitools.codegen.languages;

import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Arrays;

public class GoServerCodegen extends AbstractGoCodegen {

private static final Logger LOGGER = LoggerFactory.getLogger(GoServerCodegen.class);

protected String apiVersion = "1.0.0";
protected int serverPort = 8080;
protected String projectName = "openapi-server";
Expand Down Expand Up @@ -85,6 +90,10 @@ public GoServerCodegen() {
public void processOpts() {
super.processOpts();

if (StringUtils.isEmpty(System.getenv("GO_FMT_PATH"))) {
LOGGER.info("Environment variable GO_FMT_PATH not defined so Go code may not be properly formatted. To define it, try 'export GO_FMT_PATH=/usr/local/bin/gofmt' (Linux/Mac)");
}

if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.0-SNAPSHOT
3.2.3-SNAPSHOT
2 changes: 1 addition & 1 deletion samples/client/petstore/go/go-petstore-withXml/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ All URIs are relative to *http://petstore.swagger.io:80/v2*

Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*AnotherFakeApi* | [**TestSpecialTags**](docs/AnotherFakeApi.md#testspecialtags) | **Patch** /another-fake/dummy | To test special tags
*AnotherFakeApi* | [**Call123TestSpecialTags**](docs/AnotherFakeApi.md#call123testspecialtags) | **Patch** /another-fake/dummy | To test special tags
*FakeApi* | [**FakeOuterBooleanSerialize**](docs/FakeApi.md#fakeouterbooleanserialize) | **Post** /fake/outer/boolean |
*FakeApi* | [**FakeOuterCompositeSerialize**](docs/FakeApi.md#fakeoutercompositeserialize) | **Post** /fake/outer/composite |
*FakeApi* | [**FakeOuterNumberSerialize**](docs/FakeApi.md#fakeouternumberserialize) | **Post** /fake/outer/number |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -954,8 +954,8 @@ paths:
- fake
/another-fake/dummy:
patch:
description: To test special tags
operationId: test_special_tags
description: To test special tags and operation ID starting with number
operationId: 123_test_@#$%_special_tags
requestBody:
content:
application/json:
Expand Down
24 changes: 12 additions & 12 deletions samples/client/petstore/go/go-petstore-withXml/api_another_fake.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading