You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/10/15 11:03:51 UTC
[incubator-servicecomb-java-chassis] 01/03: [SCB-948] convert proto
model to string
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit 41a848d28d70fde95b9d3031a2cb72119ea5a3c4
Author: wujimin <wu...@huawei.com>
AuthorDate: Sun Sep 30 17:02:48 2018 +0800
[SCB-948] convert proto model to string
---
common/common-protobuf/pom.xml | 16 +--
.../internal/converter/ProtoToStringGenerator.java | 119 +++++++++++++++++++++
.../converter/TestProtoToStringGenerator.java | 83 ++++++++++++++
.../common/utils/StringBuilderUtils.java | 26 +++++
foundations/foundation-protobuf/pom.xml | 4 +-
java-chassis-dependencies/pom.xml | 5 +
java-chassis-distribution/src/release/LICENSE | 8 ++
7 files changed, 247 insertions(+), 14 deletions(-)
diff --git a/common/common-protobuf/pom.xml b/common/common-protobuf/pom.xml
index fa999ab..aa70364 100644
--- a/common/common-protobuf/pom.xml
+++ b/common/common-protobuf/pom.xml
@@ -32,23 +32,15 @@
<artifactId>java-chassis-core</artifactId>
</dependency>
<dependency>
- <groupId>io.protostuff</groupId>
- <artifactId>protostuff-core</artifactId>
- </dependency>
- <dependency>
- <groupId>io.protostuff</groupId>
- <artifactId>protostuff-runtime</artifactId>
- </dependency>
- <dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>common-javassist</artifactId>
</dependency>
+
<dependency>
- <groupId>com.google.protobuf</groupId>
- <artifactId>protobuf-java</artifactId>
- <version>3.3.0</version>
- <scope>test</scope>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>foundation-protobuf</artifactId>
</dependency>
+
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
diff --git a/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/internal/converter/ProtoToStringGenerator.java b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/internal/converter/ProtoToStringGenerator.java
new file mode 100644
index 0000000..4e1b9ed
--- /dev/null
+++ b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/internal/converter/ProtoToStringGenerator.java
@@ -0,0 +1,119 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.servicecomb.codec.protobuf.internal.converter;
+
+import static org.apache.servicecomb.foundation.common.utils.StringBuilderUtils.appendLine;
+
+import io.protostuff.compiler.model.Enum;
+import io.protostuff.compiler.model.EnumConstant;
+import io.protostuff.compiler.model.Field;
+import io.protostuff.compiler.model.Import;
+import io.protostuff.compiler.model.Message;
+import io.protostuff.compiler.model.Proto;
+import io.protostuff.compiler.model.Service;
+import io.protostuff.compiler.model.ServiceMethod;
+
+public class ProtoToStringGenerator {
+ private final Proto proto;
+
+ public ProtoToStringGenerator(Proto proto) {
+ this.proto = proto;
+ }
+
+ public String protoToString() {
+ StringBuilder sb = new StringBuilder();
+ appendLine(sb, "syntax = \"%s\";", proto.getSyntax());
+ for (Import importValue : proto.getImports()) {
+ appendLine(sb, "import \"%s\";", importValue.getValue());
+ }
+ appendLine(sb, "package %s;\n", proto.getPackage().getValue());
+
+ for (Message message : proto.getMessages()) {
+ messageToString(message, sb);
+ }
+
+ for (Enum enumValue : proto.getEnums()) {
+ enumToString(enumValue, sb);
+ }
+
+ for (Service service : proto.getServices()) {
+ serviceToString(service, sb);
+ }
+ return sb.toString();
+ }
+
+ private void serviceToString(Service service, StringBuilder sb) {
+ appendLine(sb, "service %s {", service.getName());
+ for (ServiceMethod serviceMethod : service.getMethods()) {
+ if (!serviceMethod.getCommentLines().isEmpty()) {
+ appendLine(sb, " //" + serviceMethod.getComments());
+ }
+ appendLine(sb, " rpc %s (%s) returns (%s);\n", serviceMethod.getName(), serviceMethod.getArgTypeName(),
+ serviceMethod.getReturnTypeName());
+ }
+ if (!service.getMethods().isEmpty()) {
+ sb.setLength(sb.length() - 1);
+ }
+ appendLine(sb, "}");
+ }
+
+ protected void enumToString(Enum enumValue, StringBuilder sb) {
+ appendLine(sb, "enum %s {", enumValue.getName());
+ for (EnumConstant enumConstant : enumValue.getConstants()) {
+ appendLine(sb, " %s = %s;", enumConstant.getName(), enumConstant.getValue());
+ }
+ sb.append("}\n\n");
+ }
+
+ private void messageToString(Message message, StringBuilder sb) {
+ appendLine(sb, "message %s {", message.getName());
+ for (Field field : message.getFields()) {
+ sb.append(" ");
+ fieldToString(field, field.isRepeated(), sb);
+ }
+ appendLine(sb, "}\n");
+ }
+
+ private void fieldToString(Field field, boolean repeated, StringBuilder sb) {
+ if (field.isMap()) {
+ fieldMapToString(field, sb);
+ return;
+ }
+
+ if (repeated) {
+ fieldRepeatedToString(field, sb);
+ return;
+ }
+
+ appendLine(sb, "%s %s = %d;", field.getTypeName(), field.getName(), field.getTag());
+ }
+
+ private void fieldRepeatedToString(Field field, StringBuilder sb) {
+ sb.append("repeated ");
+ fieldToString(field, false, sb);
+ }
+
+ private void fieldMapToString(Field field, StringBuilder sb) {
+ Message entryMessage = (Message) field.getType();
+ Field keyField = entryMessage.getField(1);
+ Field valueField = entryMessage.getField(2);
+
+ // map<string, string> name = 1;
+ appendLine(sb, "map<%s, %s> %s = %d;", keyField.getTypeName(), valueField.getTypeName(), field.getName(),
+ field.getTag());
+ }
+}
diff --git a/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/internal/converter/TestProtoToStringGenerator.java b/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/internal/converter/TestProtoToStringGenerator.java
new file mode 100644
index 0000000..9618313
--- /dev/null
+++ b/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/internal/converter/TestProtoToStringGenerator.java
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.servicecomb.codec.protobuf.internal.converter;
+
+import org.apache.servicecomb.foundation.protobuf.internal.parser.ProtoParser;
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.protostuff.compiler.model.Proto;
+
+public class TestProtoToStringGenerator {
+ static String content = "syntax = \"proto3\";\n"
+ + "import \"google/protobuf/any.proto\";\n"
+ + "package org.apache.servicecomb.foundation.protobuf.internal.model;\n"
+ + "\n"
+ + "message Root {\n"
+ + " int32 int32 = 1;\n"
+ + " int64 int64 = 2;\n"
+ + " uint32 uint32 = 3;\n"
+ + " uint64 uint64 = 4;\n"
+ + " sint32 sint32 = 5;\n"
+ + " sint64 sint64 = 6;\n"
+ + " fixed32 fixed32 = 7;\n"
+ + " fixed64 fixed64 = 8;\n"
+ + " sfixed32 sfixed32 = 9;\n"
+ + " sfixed64 sfixed64 = 10;\n"
+ + " float floatValue = 11;\n"
+ + " double doubleValue = 12;\n"
+ + " bool bool = 13;\n"
+ + " string string = 14;\n"
+ + " bytes bytes = 15;\n"
+ + " Color color = 16;\n"
+ + " User user = 17;\n"
+ + " map<string, string> ssMap = 18;\n"
+ + " map<string, User> spMap = 19;\n"
+ + " repeated string sList = 20;\n"
+ + " repeated User pList = 21;\n"
+ + " google.protobuf.Any any = 22;\n"
+ + " repeated google.protobuf.Any anys = 23;\n"
+ + " Root typeRecursive = 24;\n"
+ + "}\n"
+ + "\n"
+ + "message User {\n"
+ + " string name = 1;\n"
+ + " Root typeRecursive = 2;\n"
+ + "}\n"
+ + "\n"
+ + "enum Color {\n"
+ + " RED = 0;\n"
+ + " YELLOW = 1;\n"
+ + " BLUE = 2;\n"
+ + "}\n"
+ + "\n"
+ + "service Service {\n"
+ + " //comment\n"
+ + " rpc op1 (User) returns (Root);\n"
+ + "\n"
+ + " rpc op2 (Root) returns (User);\n"
+ + "}\n";
+
+ @Test
+ public void protoToString() {
+ ProtoParser protoParser = new ProtoParser();
+ Proto proto = protoParser.parseFromContent(content);
+ String newContent = new ProtoToStringGenerator(proto).protoToString();
+
+ Assert.assertEquals(content, newContent);
+ }
+}
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/StringBuilderUtils.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/StringBuilderUtils.java
new file mode 100644
index 0000000..0a57a98
--- /dev/null
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/StringBuilderUtils.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.servicecomb.foundation.common.utils;
+
+public final class StringBuilderUtils {
+ private StringBuilderUtils() {
+ }
+
+ public static void appendLine(StringBuilder sb, String fmt, Object... args) {
+ sb.append(String.format(fmt, args)).append("\n");
+ }
+}
diff --git a/foundations/foundation-protobuf/pom.xml b/foundations/foundation-protobuf/pom.xml
index 7ee177f..4bb95d7 100644
--- a/foundations/foundation-protobuf/pom.xml
+++ b/foundations/foundation-protobuf/pom.xml
@@ -49,12 +49,12 @@
<artifactId>jsr305</artifactId>
</dependency>
+ <!-- only for provide any.proto/empty.proto and so on -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
- <version>3.6.1</version>
- <scope>test</scope>
</dependency>
+
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-protobuf</artifactId>
diff --git a/java-chassis-dependencies/pom.xml b/java-chassis-dependencies/pom.xml
index 58fa021..db0dd91 100644
--- a/java-chassis-dependencies/pom.xml
+++ b/java-chassis-dependencies/pom.xml
@@ -1030,6 +1030,11 @@
<artifactId>metrics-prometheus</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
+ <dependency>
+ <groupId>com.google.protobuf</groupId>
+ <artifactId>protobuf-java</artifactId>
+ <version>3.5.1</version>
+ </dependency>
</dependencies>
</dependencyManagement>
diff --git a/java-chassis-distribution/src/release/LICENSE b/java-chassis-distribution/src/release/LICENSE
index 035e4b0..31051f3 100644
--- a/java-chassis-distribution/src/release/LICENSE
+++ b/java-chassis-distribution/src/release/LICENSE
@@ -312,6 +312,14 @@ For details, see http://asm.ow2.org/
You can find a copy of the License at licenses/LICENSE-asm
================================================================
+For protobuf-java com.google.protobuf:protobuf-java:jar:3.5.1
+================================================================
+This product bundles protobuf which is licensed under the
+3-Clause BSD license.
+For details, see https://github.com/protocolbuffers/protobuf
+You can find a copy of the License at https://github.com/protocolbuffers/protobuf/blob/master/LICENSE
+
+================================================================
For foundations/foundation-vertx/src/main/java/io/vertx/ext/web/impl/MimeTypesUtils.java
transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/RestBodyHandler.java
================================================================