del[protobuf]: del wire format

This commit is contained in:
godotg
2023-12-03 11:45:00 +08:00
parent d6632f1dda
commit ebfeb84d3b
7 changed files with 97 additions and 194 deletions
@@ -17,8 +17,8 @@ import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.serializer.protobuf.builder.JavaBuilder;
import com.zfoo.protocol.serializer.protobuf.wire.Option;
import com.zfoo.protocol.serializer.protobuf.wire.ProtoMessage;
import com.zfoo.protocol.serializer.protobuf.wire.parser.Proto;
import com.zfoo.protocol.serializer.protobuf.wire.parser.ProtoParser;
import com.zfoo.protocol.serializer.protobuf.parser.Proto;
import com.zfoo.protocol.serializer.protobuf.parser.ProtoParser;
import com.zfoo.protocol.util.FileUtils;
import com.zfoo.protocol.util.StringUtils;
@@ -14,10 +14,10 @@
package com.zfoo.protocol.serializer.protobuf.builder;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.serializer.protobuf.parser.JavaType;
import com.zfoo.protocol.serializer.protobuf.wire.*;
import com.zfoo.protocol.serializer.protobuf.wire.PbField.Type;
import com.zfoo.protocol.serializer.protobuf.wire.WireFormat.JavaType;
import com.zfoo.protocol.serializer.protobuf.wire.parser.Proto;
import com.zfoo.protocol.serializer.protobuf.parser.Proto;
import com.zfoo.protocol.util.StringUtils;
import java.util.*;
@@ -0,0 +1,62 @@
/*
* 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.parser;
/**
* @author godotg
*/
import com.zfoo.protocol.collection.ArrayUtils;
/**
* java的数据类型和protocol buffer的数据类型的对应关系以及默认值
*/
public enum JavaType {
INT("int", "Integer", 0),
LONG("long", "Long", 0L),
FLOAT("float", "Float", 0F),
DOUBLE("double", "Double", 0D),
BOOLEAN("boolean", "Boolean", false),
STRING("String", "String", ""),
BYTES("byte[]", "byte[]", ArrayUtils.EMPTY_BYTE_ARRAY),
ENUM("enum", "enum", null),
MESSAGE("", "", null),
OBJECT("Object", "Object", null),
MAP("Map", "Map", null);
JavaType(final String typeString, final String boxedType, final Object defaultValue) {
this.typeString = typeString;
this.boxedType = boxedType;
this.defaultValue = defaultValue;
}
public String getTypeString() {
return this.typeString;
}
/**
* The default value for fields of this type, if it's a primitive type.
*/
public Object defaultValue() {
return this.defaultValue;
}
public String getBoxedType() {
return this.boxedType;
}
private final Object defaultValue;
private final String typeString;
private final String boxedType;
}
@@ -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,7 +10,7 @@
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.protocol.serializer.protobuf.wire.parser;
package com.zfoo.protocol.serializer.protobuf.parser;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.serializer.protobuf.wire.*;
@@ -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,7 +10,7 @@
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.protocol.serializer.protobuf.wire.parser;
package com.zfoo.protocol.serializer.protobuf.parser;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.serializer.protobuf.wire.*;
@@ -22,7 +21,6 @@ import com.zfoo.protocol.util.StringUtils;
import java.util.*;
import static com.zfoo.protocol.serializer.protobuf.wire.WireFormat.isValidTag;
/**
* proto文件的解析器
@@ -286,22 +284,6 @@ public class ProtoParser {
return tag;
}
/**
* 读取proto Field的数字tag值
*
* @param stag
* @return
* @throws RuntimeException
*/
public static int validTag(String stag) throws RuntimeException {
int tag = parseInt(stag);
if (!isValidTag(tag)) {
throw new RuntimeException("tag [" + tag + "] not enabled");
} else {
return tag;
}
}
/**
* 读取一个标识符
*
@@ -429,7 +411,7 @@ public class ProtoParser {
readValueSeparator('=');
trim();
ProtoValue pv = readValueUtilSemicolon();
int tag = validTag(pv.getValue());
int tag = parseInt(pv.getValue());
trim();
Comment comment = new Comment();
if (!comments.isEmpty()) {
@@ -919,4 +901,5 @@ public class ProtoParser {
}
return false;
}
}
@@ -13,6 +13,8 @@
package com.zfoo.protocol.serializer.protobuf.wire;
import com.zfoo.protocol.serializer.protobuf.parser.JavaType;
/**
* protocol buffer协议消息体属性数据类型定义
*/
@@ -51,36 +53,36 @@ public class PbField {
public enum Type {
FLOAT("float", WireFormat.JavaType.FLOAT),
DOUBLE("double", WireFormat.JavaType.DOUBLE),
INT32("int32", WireFormat.JavaType.INT),
INT64("int64", WireFormat.JavaType.LONG),
UINT32("uint32", WireFormat.JavaType.INT),
UINT64("uint64", WireFormat.JavaType.LONG),
SINT32("sint32", WireFormat.JavaType.INT),
SINT64("sint64", WireFormat.JavaType.LONG),
FIXED32("fixed32", WireFormat.JavaType.INT),
FIXED64("fixed64", WireFormat.JavaType.LONG),
SFIXED32("sfixed32", WireFormat.JavaType.INT),
SFIXED64("sfixed64", WireFormat.JavaType.LONG),
BOOL("bool", WireFormat.JavaType.BOOLEAN),
ENUM("enum", WireFormat.JavaType.ENUM),
STRING("string", WireFormat.JavaType.STRING),
BYTES("bytes", WireFormat.JavaType.BYTES),
MESSAGE("", WireFormat.JavaType.MESSAGE),
OBJECT("OBJECT", WireFormat.JavaType.OBJECT),
GROUP("group", WireFormat.JavaType.MESSAGE),
MAP("", WireFormat.JavaType.MAP);
FLOAT("float", JavaType.FLOAT),
DOUBLE("double", JavaType.DOUBLE),
INT32("int32", JavaType.INT),
INT64("int64", JavaType.LONG),
UINT32("uint32", JavaType.INT),
UINT64("uint64", JavaType.LONG),
SINT32("sint32", JavaType.INT),
SINT64("sint64", JavaType.LONG),
FIXED32("fixed32", JavaType.INT),
FIXED64("fixed64", JavaType.LONG),
SFIXED32("sfixed32", JavaType.INT),
SFIXED64("sfixed64", JavaType.LONG),
BOOL("bool", JavaType.BOOLEAN),
ENUM("enum", JavaType.ENUM),
STRING("string", JavaType.STRING),
BYTES("bytes", JavaType.BYTES),
MESSAGE("", JavaType.MESSAGE),
OBJECT("OBJECT", JavaType.OBJECT),
GROUP("group", JavaType.MESSAGE),
MAP("", JavaType.MAP);
private final String value;
private final WireFormat.JavaType javaType;
private final JavaType javaType;
Type(String value, WireFormat.JavaType javaType) {
Type(String value,JavaType javaType) {
this.value = value;
this.javaType = javaType;
}
public WireFormat.JavaType javaType() {
public JavaType javaType() {
return javaType;
}
@@ -1,143 +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.wire;
/**
* 协议格式定义相关的常量等
*/
public class WireFormat {
private WireFormat() {
}
/**
* 定长32位占用的字节长度
*/
public static final int FIXED_32_SIZE = 4;
/**
* 定长64位占用的字节长度
*/
public static final int FIXED_64_SIZE = 8;
/**
* 变长整型数据最大査勇的字节数
*/
public static final int MAX_VARINT_SIZE = 5;
/**
* 定长长整型最大占用的字节数
*/
public static final int MAX_VARLONG_SIZE = 10;
/**
* 最小的Tag值
*/
public static final int MIN_TAG_VALUE = 1;
/**
* 最大的Tag值
*/
public static final int MAX_TAG_VALUE = (1 << 29) - 1; // 536,870,911
/**
* 系统预留Tag的开始值
*/
public static final int RESERVED_TAG_VALUE_START = 19000;
/**
* 系统预留Tag的结束值
*/
public static final int RESERVED_TAG_VALUE_END = 19999;
/**
* Tag的数据类型占用的bit个数
*/
static final int TAG_TYPE_BITS = 3;
/**
* Tag类型的掩码
*/
static final int TAG_TYPE_MASK = (1 << TAG_TYPE_BITS) - 1;
/**
* 空的byte数组
*/
static final byte[] EMPTY_BYTES = new byte[0];
/**
* Tag值是否符合规则
*
* @param tag 给定的Tag值
* @return 是否符合规则
*/
public static boolean isValidTag(int tag) {
return (tag >= MIN_TAG_VALUE && tag < RESERVED_TAG_VALUE_START)
|| (tag > RESERVED_TAG_VALUE_END && tag <= MAX_TAG_VALUE);
}
/**
* 从int值盅获取该Tag的数据类型的int值
*
* @param tag 给定的int值
* @return
*/
public static int getTagWireType(final int tag) {
return tag & TAG_TYPE_MASK;
}
/**
* 获取给定的int值中Tag的值
*
* @param tag 解码出的tag的int值
* @return
*/
public static int getTagFieldNumber(final int tag) {
return tag >>> TAG_TYPE_BITS;
}
/**
* java的数据类型和protocol buffer的数据类型的对应关系以及默认值
*/
public enum JavaType {
INT("int", "Integer", 0),
LONG("long", "Long", 0L),
FLOAT("float", "Float", 0F),
DOUBLE("double", "Double", 0D),
BOOLEAN("boolean", "Boolean", false),
STRING("String", "String", ""),
BYTES("byte[]", "byte[]", EMPTY_BYTES),
ENUM("enum", "enum", null),
MESSAGE("", "", null),
OBJECT("Object", "Object", null),
MAP("Map", "Map", null);
JavaType(final String typeString, final String boxedType, final Object defaultValue) {
this.typeString = typeString;
this.boxedType = boxedType;
this.defaultValue = defaultValue;
}
public String getTypeString() {
return this.typeString;
}
/**
* The default value for fields of this type, if it's a primitive type.
*/
public Object defaultValue() {
return this.defaultValue;
}
public String getBoxedType() {
return this.boxedType;
}
private final Object defaultValue;
private final String typeString;
private final String boxedType;
}
}