del[protobuf]: not support reserved in protobuf

This commit is contained in:
godotg
2023-12-02 22:38:27 +08:00
parent 2dcbcc6441
commit 827e30e899
5 changed files with 7 additions and 434 deletions
@@ -1,36 +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;
import java.util.List;
/**
* 按名称预留的结构定义
*/
public class NameReserved extends Reserved {
/**
* 消息保留的属性名称列表
*/
private List<String> fieldNames;
public List<String> getFieldNames() {
return fieldNames;
}
public NameReserved setFieldNames(List<String> names) {
this.fieldNames = names;
return this;
}
}
@@ -32,11 +32,6 @@ public class ProtoMessage {
* 消息内嵌的Message列表
*/
private List<ProtoMessage> protoMessages;
/**
* 保留字段的设置
*/
private List<Reserved> reserveds;
/**
* 消息的备注信息
*/
@@ -58,14 +53,6 @@ public class ProtoMessage {
return this;
}
public ProtoMessage addReserved(Reserved reserved) {
if (reserved == null) {
return this;
}
getReserveds().add(reserved);
return this;
}
/**
* 消息的名称
*
@@ -139,33 +126,6 @@ public class ProtoMessage {
return this;
}
/**
* 保留字段的设置
*
* @return the reserveds
*/
public List<Reserved> getReserveds() {
if (reserveds == null) {
reserveds = new ArrayList<>();
}
return reserveds;
}
/**
* 保留字段的设置
*
* @param reserveds the reserveds to set
*/
public ProtoMessage setReserveds(List<Reserved> reserveds) {
if (reserveds instanceof ArrayList) {
this.reserveds = reserveds;
} else {
this.reserveds = new ArrayList<>();
this.reserveds.addAll(reserveds);
}
return this;
}
/**
* 消息的备注信息
*
@@ -1,220 +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;
import com.zfoo.protocol.serializer.protobuf.wire.TagReserved.StartEnd;
import com.zfoo.protocol.serializer.protobuf.wire.parser.ProtoParser;
import java.util.ArrayList;
import java.util.List;
/**
* protocol buffer中预留信息的结构定义
*/
public class Reserved {
/**
* 消息保留Field列表的表达式
*/
private String expression;
/**
* 保留Fields设置的注释信息
*/
private Comment comment;
public Reserved setExpression(String expression) {
this.expression = expression;
return this;
}
public String getExpression() {
return expression;
}
public Reserved setComment(Comment comment) {
this.comment = comment;
return this;
}
public Comment getComment() {
return comment;
}
public static Reserved parseReserved(String expression, List<Comment> comments) throws RuntimeException {
String exp = expression.trim();
if (exp.isEmpty()) {
throw new RuntimeException("reserved expression is empty");
}
Reserved reserved;
if (exp.charAt(0) == '\'' || exp.charAt(0) == '"') {
reserved = new NameReserved().setFieldNames(readFieldNames(exp));
} else {
reserved = readRagReserved(exp);
}
if (!comments.isEmpty()) {
reserved.setComment(comments.get(comments.size() - 1));
comments.clear();
}
reserved.setExpression(exp);
return reserved;
}
public static TagReserved readRagReserved(String expression) throws RuntimeException {
TagReserved reserved = new TagReserved();
String[] stags = expression.split(",");
List<Integer> tags = new ArrayList<>();
List<StartEnd> startEnds = new ArrayList<>();
for (String sTag : stags) {
int index = sTag.indexOf("to");
if (index == -1) {
int tag = ProtoParser.parseInt(sTag.trim());
tags.add(tag);
} else {
int start = ProtoParser.parseInt(sTag.substring(0, index).trim());
int end;
String sEnd = sTag.substring(index + 2).trim();
if (!"max".equalsIgnoreCase(sEnd)) {
end = ProtoParser.parseInt(sTag.substring(index + 2).trim());
} else {
end = WireFormat.MAX_TAG_VALUE;
}
StartEnd se = new StartEnd();
se.setStartTag(start).setEndTag(end);
startEnds.add(se);
}
}
reserved.setTags(tags);
reserved.setStartEnds(startEnds);
return reserved;
}
public static List<String> readFieldNames(String exp) throws RuntimeException {
ParseFieldNameResult res = readFieldName(exp, 0);
char endChar = res.getEndChar();
List<String> names = new ArrayList<>();
while (endChar != ';') {
names.add(res.getName());
if (res.getPos() >= exp.length()) {
return names;
}
res.setPos(res.getPos() + 1);
trim(exp, res);
if (res.getPos() < exp.length()) {
res = readFieldName(exp, res.getPos());
endChar = res.getEndChar();
if (endChar != ',' && endChar != '\'' && endChar != '"') {
break;
}
}
}
throw new RuntimeException("reserved not end \";\"");
}
private static ParseFieldNameResult readFieldName(String exp, int pos)
throws RuntimeException {
ParseFieldNameResult result = new ParseFieldNameResult();
result.setPos(pos);
trim(exp, result);
char startChar = result.getEndChar();
if (startChar != '\'' && startChar != '"') {
throw new RuntimeException("field name not start with ' or \"");
}
StringBuilder sb = new StringBuilder();
int start = result.getPos();
char c;
for (int i = start + 1; i < exp.length(); i++) {
c = exp.charAt(i);
if (c == startChar) {
if (sb.length() > 0) {
result.setPos(i + 1);
trim(exp, result);
result.setName(sb.toString());
return result;
} else {
result.setName(sb.toString());
return result;
}
} else {
sb.append(c);
}
}
return result;
}
private static void trim(String exp, final ParseFieldNameResult result) {
int pos = result.getPos();
char c;
for (int i = pos; i < exp.length(); i++) {
c = exp.charAt(i);
if (c != ' ' && c != '\t') {
result.setPos(i);
result.setEndChar(c);
return;
}
}
}
static class ParseFieldNameResult {
private String name;
private int pos;
private char endChar;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public ParseFieldNameResult setName(String name) {
this.name = name;
return this;
}
/**
* @return the pos
*/
public int getPos() {
return pos;
}
/**
* @param pos the pos to set
*/
public ParseFieldNameResult setPos(int pos) {
this.pos = pos;
return this;
}
/**
* @return the endChar
*/
public char getEndChar() {
return endChar;
}
/**
* @param endChar the endChar to set
*/
public ParseFieldNameResult setEndChar(char endChar) {
this.endChar = endChar;
return this;
}
}
}
@@ -1,108 +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;
import java.util.List;
/**
* 按Tag的编号预留的结构定义
*/
public class TagReserved extends Reserved {
/**
* 消息保留的tag列表
*/
private List<Integer> tags;
/**
* 保留tag的开始和结束范围列表
*/
private List<StartEnd> startEnds;
/**
* 消息保留的tag列表
*
* @return the tags
*/
public List<Integer> getTags() {
return tags;
}
/**
* 消息保留的tag列表
*
* @param tags the tags to set
* @return
*/
public TagReserved setTags(List<Integer> tags) {
this.tags = tags;
return this;
}
/**
* 保留tag的开始和结束范围列表
*
* @return the startEnds
*/
public List<StartEnd> getStartEnds() {
return startEnds;
}
/**
* 保留tag的开始和结束范围列表
*
* @param startEnds the startEnds to set
* @return
*/
public TagReserved setStartEnds(List<StartEnd> startEnds) {
this.startEnds = startEnds;
return this;
}
public static class StartEnd {
private int startTag;
private int endTag;
/**
* @return the startTag
*/
public int getStartTag() {
return startTag;
}
/**
* @param startTag the startTag to set
* @return
*/
public StartEnd setStartTag(int startTag) {
this.startTag = startTag;
return this;
}
/**
* @return the endTag
*/
public int getEndTag() {
return endTag;
}
/**
* @param endTag the endTag to set
* @return
*/
public StartEnd setEndTag(int endTag) {
this.endTag = endTag;
return this;
}
}
}
@@ -215,6 +215,11 @@ public class ProtoParser {
throw new RuntimeException(StringUtils.format("zfoo not support oneof field in message:[{}]", name));
}
private void notSupportReserved() {
var name = readToken();
throw new RuntimeException(StringUtils.format("zfoo not support reserved syntax in message:[{}]", name));
}
private void addCommentsToProto(Proto proto) {
if (CollectionUtils.isEmpty(comments)) {
return;
@@ -416,11 +421,10 @@ public class ProtoParser {
notSupportExtend();
} else if ("oneof".equals(token)) {
notSupportOneof();
} else if ("reserved".equals(token)) {
notSupportReserved();
} else if ("message".equals(token)) {
msg.addMessage(parseMessage());
} else if ("reserved".equals(token)) {
String reserved = parseReserved();
msg.addReserved(Reserved.parseReserved(reserved, comments));
} else {
Field field = parseField(Cardinality.OPTIONAL, token);
msg.addField(field);
@@ -446,33 +450,6 @@ public class ProtoParser {
}
}
private String parseReserved() throws RuntimeException {
return readExpression();
}
private String readExpression() throws RuntimeException {
StringBuilder exp = new StringBuilder();
trim();
char c;
while (pos < data.length) {
c = data[pos];
switch (c) {
case '\n':
throw new RuntimeException(ROW_MSG + row +
"] expression not end with char ';'");
case ';':
pos++;
trim();
return exp.toString();
default:
exp.append(c);
}
pos++;
}
throw new RuntimeException(ROW_MSG + row +
"] expression not end with char ';'");
}
private Field parseField(Cardinality cardinality, String fieldType) throws RuntimeException {
trim();
if (fieldType == null) {