Skip to content

Commit

Permalink
ref[protobuf]: refactor protobuf generate
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Dec 3, 2023
1 parent 0ba1c6e commit 85fff55
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -118,7 +117,7 @@ public static String getJavaType(PbField field) {
}

public static String getJavaType(String type) {
var typeProtobuf = TypeProtobuf.typeOfProtobuf(type);
var typeProtobuf = PbType.typeOfProtobuf(type);
if (typeProtobuf == null) {
return type;
}
Expand All @@ -131,7 +130,7 @@ private static String getBoxJavaType(PbField field) {
}

private static String getBoxJavaType(String type) {
var typeProtobuf = TypeProtobuf.typeOfProtobuf(type);
var typeProtobuf = PbType.typeOfProtobuf(type);
if (typeProtobuf == null) {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
*
Expand All @@ -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),
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -405,7 +402,7 @@ private PbField parseField(Cardinality cardinality, String fieldType) throws Run
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());
Expand Down

This file was deleted.

0 comments on commit 85fff55

Please sign in to comment.