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

@ -86,20 +86,6 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
modelPackage = "messages"; modelPackage = "messages";
apiPackage = "services"; 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>( defaultIncludes = new HashSet<String>(
Arrays.asList( Arrays.asList(
"map", "map",
@ -129,7 +115,6 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
instantiationTypes.clear(); instantiationTypes.clear();
instantiationTypes.put("array", "repeat"); instantiationTypes.put("array", "repeat");
//instantiationTypes.put("map", "map");
// ref: https://developers.google.com/protocol-buffers/docs/proto // ref: https://developers.google.com/protocol-buffers/docs/proto
typeMapping.clear(); typeMapping.clear();
@ -154,23 +139,11 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
typeMapping.put("object", "TODO_OBJECT_MAPPING"); typeMapping.put("object", "TODO_OBJECT_MAPPING");
importMapping.clear(); importMapping.clear();
/*
importMapping = new HashMap<String, String>();
importMapping.put("time.Time", "time");
importMapping.put("*os.File", "os");
importMapping.put("os", "io/ioutil");
*/
modelDocTemplateFiles.put("model_doc.mustache", ".md"); modelDocTemplateFiles.put("model_doc.mustache", ".md");
apiDocTemplateFiles.put("api_doc.mustache", ".md"); apiDocTemplateFiles.put("api_doc.mustache", ".md");
cliOptions.clear(); 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("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 @Override
@ -295,14 +264,14 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
this.addImport(cm, importValue); this.addImport(cm, importValue);
Map<String, Object> importItem = new HashMap<>(); Map<String, Object> importItem = new HashMap<>();
importItem.put(IMPORT, modelFileName); importItem.put(IMPORT, modelFileName);
((List<Map<String, Object>>)((Map<String, Object>)objs.get(cm.getName())).get(IMPORTS)).add(importItem); ((List<Map<String, Object>>) ((Map<String, Object>) objs.get(cm.getName())).get(IMPORTS)).add(importItem);
} }
} }
private boolean isImportAlreadyPresentInModel(Map<String, Object> objs, CodegenModel cm, String importValue) { private boolean isImportAlreadyPresentInModel(Map<String, Object> objs, CodegenModel cm, String importValue) {
boolean skipImport = false; boolean skipImport = false;
List<Map<String, Object>> cmImports = ((List<Map<String, Object>>)((Map<String, Object>)objs.get(cm.getName())).get(IMPORTS)); 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 (Map<String, Object> cmImportItem : cmImports) {
for (Entry<String, Object> cmImportItemEntry : cmImportItem.entrySet()) { for (Entry<String, Object> cmImportItemEntry : cmImportItem.entrySet()) {
if (importValue.equals(cmImportItemEntry.getValue())) { if (importValue.equals(cmImportItemEntry.getValue())) {
skipImport = true; skipImport = true;
@ -528,8 +497,8 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
private int generateFieldNumberFromString(String name) throws ProtoBufIndexComputationException { private int generateFieldNumberFromString(String name) throws ProtoBufIndexComputationException {
// Max value from developers.google.com/protocol-buffers/docs/proto3#assigning_field_numbers // Max value from developers.google.com/protocol-buffers/docs/proto3#assigning_field_numbers
int fieldNumber = Math.abs(name.hashCode()%536870911); int fieldNumber = Math.abs(name.hashCode() % 536870911);
if (19000 <= fieldNumber && fieldNumber <= 19999 ) { if (19000 <= fieldNumber && fieldNumber <= 19999) {
LOGGER.error("Generated field number is in reserved range (19000, 19999) for %s, %d", name, fieldNumber); 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)."); throw new ProtoBufIndexComputationException("Generated field number is in reserved range (19000, 19999).");
} }
@ -538,6 +507,7 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
/** /**
* Checks if the var provided is already in the list of the parent's vars, matching the type and the name * 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 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 * @return true if the var is already in the parent's list, false otherwise

View File

@ -15,10 +15,10 @@ package petstore;
message ApiResponse { 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 { message Category {
int64 id = 1; int64 id = 3355;
string name = 2; string name = 3373707;
} }

View File

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

View File

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

View File

@ -15,13 +15,13 @@ package petstore;
message Order { 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 // Order Status
enum StatusEnum { enum StatusEnum {
@ -30,8 +30,8 @@ message Order {
DELIVERED = 2; 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 { 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 // pet status in the store
enum StatusEnum { enum StatusEnum {
@ -34,6 +34,6 @@ message Pet {
SOLD = 2; SOLD = 2;
} }
StatusEnum status = 6; StatusEnum status = 355610639;
} }

View File

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

View File

@ -15,21 +15,21 @@ package petstore;
message User { 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 // User Status
int32 userStatus = 8; int32 userStatus = 517890975;
} }