beter code format for protobuf java files (#7857)

This commit is contained in:
William Cheng 2020-11-02 19:42:52 +08:00 committed by GitHub
parent 4f2f80766e
commit ca6fcaf92a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 101 additions and 131 deletions

View File

@ -42,10 +42,10 @@ import static org.openapitools.codegen.utils.StringUtils.camelize;
import static org.openapitools.codegen.utils.StringUtils.underscore;
public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConfig {
private static final String IMPORT = "import";
private static final String IMPORTS = "imports";
private static final String IMPORTS = "imports";
private static final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class);
@ -86,20 +86,6 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
modelPackage = "messages";
apiPackage = "services";
/*setReservedWordsLowerCase(
Arrays.asList(
// data type
"nil", "string", "boolean", "number", "userdata", "thread",
"table",
// reserved words: http://www.lua.org/manual/5.1/manual.html#2.1
"and", "break", "do", "else", "elseif",
"end", "false", "for", "function", "if",
"in", "local", "nil", "not", "or",
"repeat", "return", "then", "true", "until", "while"
)
);*/
defaultIncludes = new HashSet<String>(
Arrays.asList(
"map",
@ -129,7 +115,6 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
instantiationTypes.clear();
instantiationTypes.put("array", "repeat");
//instantiationTypes.put("map", "map");
// ref: https://developers.google.com/protocol-buffers/docs/proto
typeMapping.clear();
@ -153,24 +138,12 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
typeMapping.put("ByteArray", "bytes");
typeMapping.put("object", "TODO_OBJECT_MAPPING");
importMapping.clear();
/*
importMapping = new HashMap<String, String>();
importMapping.put("time.Time", "time");
importMapping.put("*os.File", "os");
importMapping.put("os", "io/ioutil");
*/
importMapping.clear();
modelDocTemplateFiles.put("model_doc.mustache", ".md");
apiDocTemplateFiles.put("api_doc.mustache", ".md");
cliOptions.clear();
/*cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "GraphQL package name (convention: lowercase).")
.defaultValue("openapi2graphql"));
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "GraphQL package version.")
.defaultValue("1.0.0"));
cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC)
.defaultValue(Boolean.TRUE.toString()));*/
}
@ -192,10 +165,6 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
}
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
//supportingFiles.add(new SupportingFile("root.mustache", "", packageName + ".proto"));
//supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
//supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"))
//supportingFiles.add(new SupportingFile(".travis.yml", "", ".travis.yml"));
}
@Override
@ -259,59 +228,59 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
}
return objs;
}
/**
* {@inheritDoc}
*/
@Override
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
super.postProcessAllModels(objs);
Map<String, CodegenModel> allModels = this.getAllModels(objs);
for (CodegenModel cm : allModels.values()) {
// Replicate all attributes from children to parents in case of allof, as there is no inheritance
if (!cm.allOf.isEmpty() && cm.getParentModel() != null) {
CodegenModel parentCM = cm.getParentModel();
for (CodegenProperty var : cm.getVars()) {
if (!parentVarsContainsVar(parentCM.vars, var)) {
parentCM.vars.add(var);
super.postProcessAllModels(objs);
Map<String, CodegenModel> allModels = this.getAllModels(objs);
for (CodegenModel cm : allModels.values()) {
// Replicate all attributes from children to parents in case of allof, as there is no inheritance
if (!cm.allOf.isEmpty() && cm.getParentModel() != null) {
CodegenModel parentCM = cm.getParentModel();
for (CodegenProperty var : cm.getVars()) {
if (!parentVarsContainsVar(parentCM.vars, var)) {
parentCM.vars.add(var);
}
}
}
// add all imports from child
cm.getImports().stream()
// Filter self import && child import
.filter(importFromList -> !parentCM.getClassname().equalsIgnoreCase(importFromList) && !cm.getClassname().equalsIgnoreCase(importFromList))
.forEach(importFromList -> this.addImport(objs, parentCM, importFromList));
// add all imports from child
cm.getImports().stream()
// Filter self import && child import
.filter(importFromList -> !parentCM.getClassname().equalsIgnoreCase(importFromList) && !cm.getClassname().equalsIgnoreCase(importFromList))
.forEach(importFromList -> this.addImport(objs, parentCM, importFromList));
}
}
}
return objs;
}
public void addImport(Map<String, Object> objs, CodegenModel cm, String importValue) {
String modelFileName = this.toModelFilename(importValue);
boolean skipImport = isImportAlreadyPresentInModel(objs, cm, modelFileName);
if (!skipImport) {
this.addImport(cm, importValue);
Map<String, Object> importItem = new HashMap<>();
importItem.put(IMPORT, modelFileName);
((List<Map<String, Object>>)((Map<String, Object>)objs.get(cm.getName())).get(IMPORTS)).add(importItem);
}
return objs;
}
private boolean isImportAlreadyPresentInModel(Map<String, Object> objs, CodegenModel cm, String importValue) {
boolean skipImport = false;
List<Map<String, Object>> cmImports = ((List<Map<String, Object>>)((Map<String, Object>)objs.get(cm.getName())).get(IMPORTS));
for (Map<String, Object> cmImportItem: cmImports) {
for (Entry<String, Object> cmImportItemEntry : cmImportItem.entrySet()) {
if (importValue.equals(cmImportItemEntry.getValue())) {
skipImport = true;
break;
}
public void addImport(Map<String, Object> objs, CodegenModel cm, String importValue) {
String modelFileName = this.toModelFilename(importValue);
boolean skipImport = isImportAlreadyPresentInModel(objs, cm, modelFileName);
if (!skipImport) {
this.addImport(cm, importValue);
Map<String, Object> importItem = new HashMap<>();
importItem.put(IMPORT, modelFileName);
((List<Map<String, Object>>) ((Map<String, Object>) objs.get(cm.getName())).get(IMPORTS)).add(importItem);
}
}
return skipImport;
}
}
private boolean isImportAlreadyPresentInModel(Map<String, Object> objs, CodegenModel cm, String importValue) {
boolean skipImport = false;
List<Map<String, Object>> cmImports = ((List<Map<String, Object>>) ((Map<String, Object>) objs.get(cm.getName())).get(IMPORTS));
for (Map<String, Object> cmImportItem : cmImports) {
for (Entry<String, Object> cmImportItemEntry : cmImportItem.entrySet()) {
if (importValue.equals(cmImportItemEntry.getValue())) {
skipImport = true;
break;
}
}
}
return skipImport;
}
@Override
public String escapeUnsafeCharacters(String input) {
@ -525,32 +494,33 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
}
return super.getTypeDeclaration(p);
}
private int generateFieldNumberFromString(String name) throws ProtoBufIndexComputationException {
// Max value from developers.google.com/protocol-buffers/docs/proto3#assigning_field_numbers
int fieldNumber = Math.abs(name.hashCode()%536870911);
if (19000 <= fieldNumber && fieldNumber <= 19999 ) {
int fieldNumber = Math.abs(name.hashCode() % 536870911);
if (19000 <= fieldNumber && fieldNumber <= 19999) {
LOGGER.error("Generated field number is in reserved range (19000, 19999) for %s, %d", name, fieldNumber);
throw new ProtoBufIndexComputationException("Generated field number is in reserved range (19000, 19999).");
}
return fieldNumber;
}
/**
* Checks if the var provided is already in the list of the parent's vars, matching the type and the name
*
* @param parentVars list of parent's vars
* @param var var to compare
* @param var var to compare
* @return true if the var is already in the parent's list, false otherwise
*/
private boolean parentVarsContainsVar(List<CodegenProperty> parentVars, CodegenProperty var) {
boolean containsVar = false;
for (CodegenProperty parentVar : parentVars) {
if (var.getDataType().equals(parentVar.getDataType())
&& var.getName().equals(parentVar.getName())) {
containsVar = true;
break;
}
boolean containsVar = false;
for (CodegenProperty parentVar : parentVars) {
if (var.getDataType().equals(parentVar.getDataType())
&& var.getName().equals(parentVar.getName())) {
containsVar = true;
break;
}
}
return containsVar;
}
return containsVar;
}
}

View File

@ -46,10 +46,10 @@ public class ProtobufSchemaCodegenTest {
Assert.assertTrue(featureSet.getWireFormatFeatures().contains(WireFormatFeature.PROTOBUF));
Assert.assertEquals(featureSet.getWireFormatFeatures().size(), 1);
}
@Test
public void testCodeGenWithAllOf() throws IOException {
File output = Files.createTempDirectory("test").toFile();
public void testCodeGenWithAllOf() throws IOException {
File output = Files.createTempDirectory("test").toFile();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("protobuf-schema")
@ -68,10 +68,10 @@ public class ProtobufSchemaCodegenTest {
output.delete();
}
private void assertFileEquals(Path generatedFilePath, Path expectedFilePath) throws IOException {
String generatedFile = new String(Files.readAllBytes(generatedFilePath), StandardCharsets.UTF_8);
String expectedFile = new String(Files.readAllBytes(expectedFilePath), StandardCharsets.UTF_8);
assertEquals(generatedFile, expectedFile);
}
private void assertFileEquals(Path generatedFilePath, Path expectedFilePath) throws IOException {
String generatedFile = new String(Files.readAllBytes(generatedFilePath), StandardCharsets.UTF_8);
String expectedFile = new String(Files.readAllBytes(expectedFilePath), StandardCharsets.UTF_8);
assertEquals(generatedFile, expectedFile);
}
}

View File

@ -15,10 +15,10 @@ package petstore;
message ApiResponse {
int32 code = 1;
int32 code = 3059181;
string type = 2;
string type = 3575610;
string message = 3;
string message = 418054152;
}

View File

@ -15,8 +15,8 @@ package petstore;
message Category {
int64 id = 1;
int64 id = 3355;
string name = 2;
string name = 3373707;
}

View File

@ -16,9 +16,9 @@ package petstore;
message InlineObject {
// Updated name of the pet
string name = 1;
string name = 3373707;
// Updated status of the pet
string status = 2;
string status = 355610639;
}

View File

@ -16,9 +16,9 @@ package petstore;
message InlineObject1 {
// Additional data to pass to server
string additionalMetadata = 1;
string additionalMetadata = 400408697;
// file to upload
string file = 2;
string file = 3143036;
}

View File

@ -15,13 +15,13 @@ package petstore;
message Order {
int64 id = 1;
int64 id = 3355;
int64 petId = 2;
int64 petId = 106557082;
int32 quantity = 3;
int32 quantity = 211262327;
string shipDate = 4;
string shipDate = 517554166;
// Order Status
enum StatusEnum {
@ -30,8 +30,8 @@ message Order {
DELIVERED = 2;
}
StatusEnum status = 5;
StatusEnum status = 355610639;
bool complete = 6;
bool complete = 62574280;
}

View File

@ -17,15 +17,15 @@ import public "models/tag.proto";
message Pet {
int64 id = 1;
int64 id = 3355;
Category category = 2;
Category category = 50511102;
string name = 3;
string name = 3373707;
repeated string photoUrls = 4;
repeated string photoUrls = 311086539;
repeated Tag tags = 5;
repeated Tag tags = 3552281;
// pet status in the store
enum StatusEnum {
@ -34,6 +34,6 @@ message Pet {
SOLD = 2;
}
StatusEnum status = 6;
StatusEnum status = 355610639;
}

View File

@ -15,8 +15,8 @@ package petstore;
message Tag {
int64 id = 1;
int64 id = 3355;
string name = 2;
string name = 3373707;
}

View File

@ -15,21 +15,21 @@ package petstore;
message User {
int64 id = 1;
int64 id = 3355;
string username = 2;
string username = 265713450;
string firstName = 3;
string firstName = 132835675;
string lastName = 4;
string lastName = 385857985;
string email = 5;
string email = 96619420;
string password = 6;
string password = 143243933;
string phone = 7;
string phone = 106642798;
// User Status
int32 userStatus = 8;
int32 userStatus = 517890975;
}