You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/10/15 11:03:46 UTC

[GitHub] liubao68 closed pull request #946: [SCB-948] Convert proto model to string

liubao68 closed pull request #946: [SCB-948] Convert proto model to string
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/946
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/common/common-protobuf/pom.xml b/common/common-protobuf/pom.xml
index fa999abee..aa7036462 100644
--- a/common/common-protobuf/pom.xml
+++ b/common/common-protobuf/pom.xml
@@ -31,24 +31,16 @@
       <groupId>org.apache.servicecomb</groupId>
       <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 000000000..4e1b9edb2
--- /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 000000000..9618313ab
--- /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 000000000..0a57a9838
--- /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 7ee177f1b..4bb95d73d 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/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/SubProcessLogger.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/SubProcessLogger.java
index 3e3243f10..22ee9bd0c 100644
--- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/SubProcessLogger.java
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/SubProcessLogger.java
@@ -98,12 +98,17 @@ public void waitStartComplete() {
     }
 
     LOGGER.info("waiting {} start.", displayName);
+    long startTime = System.currentTimeMillis();
     for (; ; ) {
       if (startCompleted) {
         LOGGER.info("{} start completed.", displayName);
         return;
       }
 
+      if (System.currentTimeMillis() - startTime > TimeUnit.MINUTES.toMillis(1)) {
+        throw new IllegalStateException(String.format("[%s] timeout to wait for start complete.", displayName));
+      }
+
       ITUtils.forceWait(TimeUnit.MILLISECONDS, 500);
     }
   }
diff --git a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/DownloadSchema.java b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/DownloadSchema.java
index d98a7ab3d..651caaccb 100644
--- a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/DownloadSchema.java
+++ b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/DownloadSchema.java
@@ -62,7 +62,7 @@ public DownloadSchema() throws IOException {
     // for download from net stream case
     server = ServerBootstrap
         .bootstrap()
-        .setListenerPort(9000)
+        .setListenerPort(0)
         .registerHandler("/download/netInputStream", (req, resp, context) -> {
           String uri = req.getRequestLine().getUri();
           String query = URI.create(uri).getQuery();
@@ -183,7 +183,7 @@ public String getFilename() {
       @ApiResponse(code = 200, response = File.class, message = ""),
   })
   public ResponseEntity<InputStream> netInputStream(String content) throws IOException {
-    URL url = new URL("http://localhost:9000/download/netInputStream?content="
+    URL url = new URL("http://localhost:" + server.getLocalPort() + "/download/netInputStream?content="
         + URLEncoder.encode(content, StandardCharsets.UTF_8.name()));
     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
     ResponseEntity<InputStream> responseEntity = ResponseEntity
diff --git a/java-chassis-dependencies/pom.xml b/java-chassis-dependencies/pom.xml
index 58fa021c5..db0dd91a8 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 035e4b0dc..31051f3e2 100644
--- a/java-chassis-distribution/src/release/LICENSE
+++ b/java-chassis-distribution/src/release/LICENSE
@@ -311,6 +311,14 @@ This product bundles asm which is licensed under the
 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


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services