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/09/20 03:11:54 UTC

[incubator-servicecomb-java-chassis] 01/02: [SCB-917] parse proto file to model

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 150b18ce13077011cb70cac2e9a16e3c2fa35582
Author: wujimin <wu...@huawei.com>
AuthorDate: Tue Sep 18 00:56:57 2018 +0800

    [SCB-917] parse proto file to model
---
 coverage-reports/pom.xml                           |  4 ++
 foundations/foundation-protobuf/pom.xml            | 31 +++++++++++
 .../internal/parser/ContentFileReader.java         | 45 ++++++++++++++++
 .../protobuf/internal/parser/ProtoParser.java      | 52 ++++++++++++++++++
 .../protobuf/internal/parser/TestProtoParser.java  | 52 ++++++++++++++++++
 .../src/test/resources/protobufRoot.proto          | 62 ++++++++++++++++++++++
 foundations/pom.xml                                |  1 +
 java-chassis-dependencies/pom.xml                  | 14 ++++-
 8 files changed, 259 insertions(+), 2 deletions(-)

diff --git a/coverage-reports/pom.xml b/coverage-reports/pom.xml
index 6a174b3..9828587 100644
--- a/coverage-reports/pom.xml
+++ b/coverage-reports/pom.xml
@@ -36,6 +36,10 @@
     </dependency>
     <dependency>
       <groupId>org.apache.servicecomb</groupId>
+      <artifactId>foundation-protobuf</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb</groupId>
       <artifactId>foundation-ssl</artifactId>
     </dependency>
     <dependency>
diff --git a/foundations/foundation-protobuf/pom.xml b/foundations/foundation-protobuf/pom.xml
new file mode 100644
index 0000000..9087a0f
--- /dev/null
+++ b/foundations/foundation-protobuf/pom.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <parent>
+    <artifactId>foundations</artifactId>
+    <groupId>org.apache.servicecomb</groupId>
+    <version>1.1.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>foundation-protobuf</artifactId>
+
+  <dependencies>
+    <dependency>
+      <groupId>io.protostuff</groupId>
+      <artifactId>protostuff-parser</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>com.google.code.findbugs</groupId>
+      <artifactId>jsr305</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>com.google.protobuf</groupId>
+      <artifactId>protobuf-java</artifactId>
+      <version>3.6.1</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>
\ No newline at end of file
diff --git a/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/parser/ContentFileReader.java b/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/parser/ContentFileReader.java
new file mode 100644
index 0000000..a501221
--- /dev/null
+++ b/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/parser/ContentFileReader.java
@@ -0,0 +1,45 @@
+/*
+ * 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.protobuf.internal.parser;
+
+import javax.annotation.Nullable;
+
+import org.antlr.v4.runtime.CharStream;
+import org.antlr.v4.runtime.CharStreams;
+
+import io.protostuff.compiler.parser.FileReader;
+
+public class ContentFileReader implements FileReader {
+  private FileReader importReader;
+
+  private boolean contentReaded;
+
+  public ContentFileReader(FileReader importReader) {
+    this.importReader = importReader;
+  }
+
+  @Nullable
+  @Override
+  public CharStream read(String contentOrName) {
+    if (!contentReaded) {
+      contentReaded = true;
+      return CharStreams.fromString(contentOrName);
+    }
+
+    return importReader.read(contentOrName);
+  }
+}
diff --git a/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/parser/ProtoParser.java b/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/parser/ProtoParser.java
new file mode 100644
index 0000000..423a5d7
--- /dev/null
+++ b/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/parser/ProtoParser.java
@@ -0,0 +1,52 @@
+/*
+ * 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.protobuf.internal.parser;
+
+import java.util.Collections;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+
+import io.protostuff.compiler.ParserModule;
+import io.protostuff.compiler.model.Proto;
+import io.protostuff.compiler.parser.FileDescriptorLoader;
+import io.protostuff.compiler.parser.FileReader;
+import io.protostuff.compiler.parser.FileReaderFactory;
+import io.protostuff.compiler.parser.ProtoContext;
+
+/**
+ * can be reused
+ */
+public class ProtoParser {
+  private Injector injector = Guice.createInjector(new ParserModule());
+
+  private FileReaderFactory fileReaderFactory = injector.getInstance(FileReaderFactory.class);
+
+  private FileReader defaultReader = fileReaderFactory.create(Collections.emptyList());
+
+  private FileDescriptorLoader loader = injector.getInstance(FileDescriptorLoader.class);
+
+  public Proto parseFromContent(String content) {
+    ProtoContext context = loader.load(new ContentFileReader(defaultReader), content);
+    return context.getProto();
+  }
+
+  public Proto parse(String name) {
+    ProtoContext context = loader.load(defaultReader, name);
+    return context.getProto();
+  }
+}
diff --git a/foundations/foundation-protobuf/src/test/java/org/apache/servicecomb/foundation/protobuf/internal/parser/TestProtoParser.java b/foundations/foundation-protobuf/src/test/java/org/apache/servicecomb/foundation/protobuf/internal/parser/TestProtoParser.java
new file mode 100644
index 0000000..f3fdfd5
--- /dev/null
+++ b/foundations/foundation-protobuf/src/test/java/org/apache/servicecomb/foundation/protobuf/internal/parser/TestProtoParser.java
@@ -0,0 +1,52 @@
+/*
+ * 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.protobuf.internal.parser;
+
+import java.io.IOException;
+import java.net.URL;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.google.common.base.MoreObjects;
+
+import io.protostuff.compiler.model.Proto;
+
+public class TestProtoParser {
+  @Test
+  public void parse() throws IOException {
+    URL url = Thread.currentThread().getContextClassLoader().getResource("protobufRoot.proto");
+    String content = IOUtils.toString(url);
+
+    ProtoParser parser = new ProtoParser();
+    Proto protoFromContent = parser.parseFromContent(content);
+    Proto protoFromName = parser.parse("protobufRoot.proto");
+
+    Assert.assertNotNull(protoFromContent.getMessage("Root"));
+    Assert.assertNotNull(protoFromContent.getMessage("User"));
+
+    Assert.assertEquals(MoreObjects.toStringHelper(protoFromContent)
+            .omitNullValues()
+            .add("messages", protoFromContent.getMessages())
+            .toString(),
+        MoreObjects.toStringHelper(protoFromName)
+            .omitNullValues()
+            .add("messages", protoFromName.getMessages())
+            .toString());
+  }
+}
diff --git a/foundations/foundation-protobuf/src/test/resources/protobufRoot.proto b/foundations/foundation-protobuf/src/test/resources/protobufRoot.proto
new file mode 100644
index 0000000..ae7ad3a
--- /dev/null
+++ b/foundations/foundation-protobuf/src/test/resources/protobufRoot.proto
@@ -0,0 +1,62 @@
+/*
+  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.
+ */
+
+syntax = "proto3";
+import "google/protobuf/any.proto";
+package org.apache.servicecomb.foundation.protobuf.internal.model;
+
+message Root {
+  int32 int32 = 1;
+  int64 int64 = 2;
+  uint32 uint32 = 3;
+  uint64 uint64 = 4;
+  sint32 sint32 = 5;
+  sint64 sint64 = 6;
+  fixed32 fixed32 = 7;
+  fixed64 fixed64 = 8;
+  sfixed32 sfixed32 = 9;
+  sfixed64 sfixed64 = 10;
+  float floatValue = 11;
+  double doubleValue = 12;
+  bool bool = 13;
+  string string = 14;
+  bytes bytes = 15;
+  Color color = 16;
+
+  User user = 17;
+
+  map<string, string> ssMap = 18;
+  map<string, User> spMap = 19;
+
+  repeated string sList = 20;
+  repeated User pList = 21;
+
+  google.protobuf.Any any = 22;
+
+  Root typeRecursive = 23;
+}
+enum Color {
+  RED = 0;
+  YELLOW = 1;
+  BLUE = 2;
+}
+
+message User {
+  string name = 1;
+
+  Root typeRecursive = 2;
+}
\ No newline at end of file
diff --git a/foundations/pom.xml b/foundations/pom.xml
index 8bb128f..7fa9995 100644
--- a/foundations/pom.xml
+++ b/foundations/pom.xml
@@ -34,5 +34,6 @@
     <module>foundation-metrics</module>
     <module>foundation-ssl</module>
     <module>foundation-test-scaffolding</module>
+    <module>foundation-protobuf</module>
   </modules>
 </project>
diff --git a/java-chassis-dependencies/pom.xml b/java-chassis-dependencies/pom.xml
index 66d7be4..bfb03c9 100644
--- a/java-chassis-dependencies/pom.xml
+++ b/java-chassis-dependencies/pom.xml
@@ -185,7 +185,7 @@
       <dependency>
         <groupId>com.google.guava</groupId>
         <artifactId>guava</artifactId>
-        <version>19.0</version>
+        <version>21.0</version>
       </dependency>
 
       <dependency>
@@ -371,7 +371,7 @@
       <dependency>
         <groupId>com.google.inject</groupId>
         <artifactId>guice</artifactId>
-        <version>4.0</version>
+        <version>4.1.0</version>
       </dependency>
       <dependency>
         <groupId>org.glassfish.jersey.core</groupId>
@@ -751,6 +751,11 @@
       </dependency>
       <dependency>
         <groupId>org.apache.servicecomb</groupId>
+        <artifactId>foundation-protobuf</artifactId>
+        <version>1.1.0-SNAPSHOT</version>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.servicecomb</groupId>
         <artifactId>foundation-test-scaffolding</artifactId>
         <version>1.1.0-SNAPSHOT</version>
         <scope>test</scope>
@@ -816,6 +821,11 @@
         <version>${protostuff.version}</version>
       </dependency>
       <dependency>
+        <groupId>io.protostuff</groupId>
+        <artifactId>protostuff-parser</artifactId>
+        <version>2.2.25</version>
+      </dependency>
+      <dependency>
         <groupId>io.swagger</groupId>
         <artifactId>swagger-core</artifactId>
         <version>${swagger.version}</version>