mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-09-05 22:17:35 +00:00
ref[protobuf]: refactor protobuf generate
This commit is contained in:
@@ -14,7 +14,6 @@
|
||||
package com.zfoo.protocol.serializer.protobuf;
|
||||
|
||||
import com.zfoo.protocol.collection.CollectionUtils;
|
||||
import com.zfoo.protocol.serializer.protobuf.parser.TypeProtobuf;
|
||||
import com.zfoo.protocol.serializer.protobuf.parser.Proto;
|
||||
import com.zfoo.protocol.serializer.protobuf.parser.ProtoParser;
|
||||
import com.zfoo.protocol.util.FileUtils;
|
||||
@@ -118,7 +117,7 @@ public class GeneratePbUtils {
|
||||
}
|
||||
|
||||
public static String getJavaType(String type) {
|
||||
var typeProtobuf = TypeProtobuf.typeOfProtobuf(type);
|
||||
var typeProtobuf = PbType.typeOfProtobuf(type);
|
||||
if (typeProtobuf == null) {
|
||||
return type;
|
||||
}
|
||||
@@ -131,7 +130,7 @@ public class GeneratePbUtils {
|
||||
}
|
||||
|
||||
private static String getBoxJavaType(String type) {
|
||||
var typeProtobuf = TypeProtobuf.typeOfProtobuf(type);
|
||||
var typeProtobuf = PbType.typeOfProtobuf(type);
|
||||
if (typeProtobuf == null) {
|
||||
return type;
|
||||
}
|
||||
|
||||
@@ -12,21 +12,19 @@
|
||||
|
||||
package com.zfoo.protocol.serializer.protobuf;
|
||||
|
||||
import com.zfoo.protocol.serializer.protobuf.parser.TypeProtobuf;
|
||||
|
||||
/**
|
||||
* protocol buffer中Map的Field的结构体定义
|
||||
*/
|
||||
public class PbMapField extends PbField {
|
||||
|
||||
private TypeProtobuf key;
|
||||
private PbType key;
|
||||
private String value;
|
||||
|
||||
public TypeProtobuf getKey() {
|
||||
public PbType getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(TypeProtobuf key) {
|
||||
public void setKey(PbType key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* 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
|
||||
*
|
||||
* http://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 com.zfoo.protocol.serializer.protobuf;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public enum PbType {
|
||||
FLOAT("float", PbTypeJava.FLOAT),
|
||||
DOUBLE("double", PbTypeJava.DOUBLE),
|
||||
INT32("int32", PbTypeJava.INT),
|
||||
INT64("int64", PbTypeJava.LONG),
|
||||
UINT32("uint32", PbTypeJava.INT),
|
||||
UINT64("uint64", PbTypeJava.LONG),
|
||||
SINT32("sint32", PbTypeJava.INT),
|
||||
SINT64("sint64", PbTypeJava.LONG),
|
||||
FIXED32("fixed32", PbTypeJava.INT),
|
||||
FIXED64("fixed64", PbTypeJava.LONG),
|
||||
SFIXED32("sfixed32", PbTypeJava.INT),
|
||||
SFIXED64("sfixed64", PbTypeJava.LONG),
|
||||
BOOL("bool", PbTypeJava.BOOLEAN),
|
||||
ENUM("enum", PbTypeJava.ENUM),
|
||||
STRING("string", PbTypeJava.STRING),
|
||||
BYTES("bytes", PbTypeJava.BYTES),
|
||||
MESSAGE("", PbTypeJava.MESSAGE),
|
||||
OBJECT("OBJECT", PbTypeJava.OBJECT),
|
||||
GROUP("group", PbTypeJava.MESSAGE),
|
||||
MAP("", PbTypeJava.MAP);
|
||||
|
||||
private final String value;
|
||||
private final PbTypeJava javaType;
|
||||
|
||||
private static final Map<String, PbType> map = new HashMap<>();
|
||||
|
||||
static {
|
||||
for (var ele : PbType.values()) {
|
||||
map.put(ele.value, ele);
|
||||
}
|
||||
}
|
||||
|
||||
PbType(String value, PbTypeJava javaType) {
|
||||
this.value = value;
|
||||
this.javaType = javaType;
|
||||
}
|
||||
|
||||
public PbTypeJava javaType() {
|
||||
return javaType;
|
||||
}
|
||||
|
||||
|
||||
public String value() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public static PbType typeOfProtobuf(String str) {
|
||||
return map.get(str);
|
||||
}
|
||||
}
|
||||
+4
-5
@@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 The edap Project
|
||||
*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* 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
|
||||
*
|
||||
@@ -11,13 +10,13 @@
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.serializer.protobuf.parser;
|
||||
package com.zfoo.protocol.serializer.protobuf;
|
||||
import com.zfoo.protocol.collection.ArrayUtils;
|
||||
|
||||
/**
|
||||
* java的数据类型和protocol buffer的数据类型的对应关系以及默认值
|
||||
*/
|
||||
public enum TypeJava {
|
||||
public enum PbTypeJava {
|
||||
INT("int", "Integer", 0),
|
||||
LONG("long", "Long", 0L),
|
||||
FLOAT("float", "Float", 0F),
|
||||
@@ -30,7 +29,7 @@ public enum TypeJava {
|
||||
OBJECT("Object", "Object", null),
|
||||
MAP("Map", "Map", null);
|
||||
|
||||
TypeJava(final String typeString, final String boxedType, final Object defaultValue) {
|
||||
PbTypeJava(final String typeString, final String boxedType, final Object defaultValue) {
|
||||
this.typeString = typeString;
|
||||
this.boxedType = boxedType;
|
||||
this.defaultValue = defaultValue;
|
||||
+2
-5
@@ -14,11 +14,8 @@
|
||||
package com.zfoo.protocol.serializer.protobuf.parser;
|
||||
|
||||
import com.zfoo.protocol.collection.CollectionUtils;
|
||||
import com.zfoo.protocol.serializer.protobuf.PbMapField;
|
||||
import com.zfoo.protocol.serializer.protobuf.PbOption;
|
||||
import com.zfoo.protocol.serializer.protobuf.PbField;
|
||||
import com.zfoo.protocol.serializer.protobuf.*;
|
||||
import com.zfoo.protocol.serializer.protobuf.PbField.Cardinality;
|
||||
import com.zfoo.protocol.serializer.protobuf.PbMessage;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
@@ -405,7 +402,7 @@ public class ProtoParser {
|
||||
nextLine();
|
||||
PbField field;
|
||||
if (gType != null) {
|
||||
TypeProtobuf type = TypeProtobuf.valueOf(gType.getKeyType().toUpperCase(Locale.ENGLISH));
|
||||
PbType type = PbType.valueOf(gType.getKeyType().toUpperCase(Locale.ENGLISH));
|
||||
var mapField = new PbMapField();
|
||||
mapField.setKey(type);
|
||||
mapField.setValue(gType.getValueType());
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Copyright 2021 The edap Project
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* http://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 com.zfoo.protocol.serializer.protobuf.parser;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public enum TypeProtobuf {
|
||||
FLOAT("float", TypeJava.FLOAT),
|
||||
DOUBLE("double", TypeJava.DOUBLE),
|
||||
INT32("int32", TypeJava.INT),
|
||||
INT64("int64", TypeJava.LONG),
|
||||
UINT32("uint32", TypeJava.INT),
|
||||
UINT64("uint64", TypeJava.LONG),
|
||||
SINT32("sint32", TypeJava.INT),
|
||||
SINT64("sint64", TypeJava.LONG),
|
||||
FIXED32("fixed32", TypeJava.INT),
|
||||
FIXED64("fixed64", TypeJava.LONG),
|
||||
SFIXED32("sfixed32", TypeJava.INT),
|
||||
SFIXED64("sfixed64", TypeJava.LONG),
|
||||
BOOL("bool", TypeJava.BOOLEAN),
|
||||
ENUM("enum", TypeJava.ENUM),
|
||||
STRING("string", TypeJava.STRING),
|
||||
BYTES("bytes", TypeJava.BYTES),
|
||||
MESSAGE("", TypeJava.MESSAGE),
|
||||
OBJECT("OBJECT", TypeJava.OBJECT),
|
||||
GROUP("group", TypeJava.MESSAGE),
|
||||
MAP("", TypeJava.MAP);
|
||||
|
||||
private final String value;
|
||||
private final TypeJava javaType;
|
||||
|
||||
private static final Map<String, TypeProtobuf> map = new HashMap<>();
|
||||
|
||||
static {
|
||||
for (var ele : TypeProtobuf.values()) {
|
||||
map.put(ele.value, ele);
|
||||
}
|
||||
}
|
||||
|
||||
TypeProtobuf(String value, TypeJava javaType) {
|
||||
this.value = value;
|
||||
this.javaType = javaType;
|
||||
}
|
||||
|
||||
public TypeJava javaType() {
|
||||
return javaType;
|
||||
}
|
||||
|
||||
|
||||
public String value() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public static TypeProtobuf typeOfProtobuf(String str) {
|
||||
return map.get(str);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user