[ 
https://issues.apache.org/jira/browse/SCB-917?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16621451#comment-16621451
 ] 

ASF GitHub Bot commented on SCB-917:
------------------------------------

liubao68 closed pull request #913: [SCB-917] parse proto file to model
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/913
 
 
   

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/coverage-reports/pom.xml b/coverage-reports/pom.xml
index 6a174b3ad..98285871b 100644
--- a/coverage-reports/pom.xml
+++ b/coverage-reports/pom.xml
@@ -34,6 +34,10 @@
       <groupId>org.apache.servicecomb</groupId>
       <artifactId>foundation-common</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb</groupId>
+      <artifactId>foundation-protobuf</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.apache.servicecomb</groupId>
       <artifactId>foundation-ssl</artifactId>
diff --git a/foundations/foundation-protobuf/pom.xml 
b/foundations/foundation-protobuf/pom.xml
new file mode 100644
index 000000000..9087a0f21
--- /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 000000000..a501221da
--- /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 000000000..423a5d7af
--- /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 000000000..f3fdfd536
--- /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 000000000..ae7ad3ae4
--- /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 8bb128f88..7fa9995bc 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 66d7be4d0..bfb03c9ca 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>
@@ -749,6 +749,11 @@
         <artifactId>foundation-common</artifactId>
         <version>1.1.0-SNAPSHOT</version>
       </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>
@@ -815,6 +820,11 @@
         <artifactId>protostuff-runtime</artifactId>
         <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>
diff --git a/java-chassis-distribution/src/release/LICENSE 
b/java-chassis-distribution/src/release/LICENSE
index c9430575f..4abe814e5 100644
--- a/java-chassis-distribution/src/release/LICENSE
+++ b/java-chassis-distribution/src/release/LICENSE
@@ -356,7 +356,8 @@ Commons IO (http://commons.apache.org/io/) 
commons-io:commons-io:jar:2.4
 Commons Lang (http://commons.apache.org/lang/) 
commons-lang:commons-lang:jar:2.6
 ezmorph (http://ezmorph.sourceforge.net) net.sf.ezmorph:ezmorph:jar:1.0.6
 FindBugs-jsr305 (http://findbugs.sourceforge.net/) 
com.google.code.findbugs:jsr305:jar:3.0.1
-Guava: Google Core Libraries for Java 
(http://code.google.com/p/guava-libraries/guava) 
com.google.guava:guava:bundle:19.0
+Guava: Google Core Libraries for Java 
(http://code.google.com/p/guava-libraries/guava) 
com.google.guava:guava:bundle:21.0
+Guice (https://github.com/google/guice) com.google.inject:guice:jar:4.1.0
 Hibernate Validator Engine 
(http://hibernate.org/validator/hibernate-validator) 
org.hibernate:hibernate-validator:jar:6.0.2.Final
 hystrix-core (https://github.com/Netflix/Hystrix) 
com.netflix.hystrix:hystrix-core:jar:1.5.10
 hystrix-javanica (https://github.com/Netflix/Hystrix) 
com.netflix.hystrix:hystrix-javanica:jar:1.5.12
@@ -400,6 +401,7 @@ protostuff :: api (http://protostuff.io/protostuff-api) 
io.protostuff:protostuff
 protostuff :: collectionschema 
(http://protostuff.io/protostuff-collectionschema) 
io.protostuff:protostuff-collectionschema:jar:1.5.2
 protostuff :: core (http://protostuff.io/protostuff-core) 
io.protostuff:protostuff-core:jar:1.5.2
 protostuff :: runtime (http://protostuff.io/protostuff-runtime) 
io.protostuff:protostuff-runtime:jar:1.5.2
+protostuff :: parser 
(https://github.com/protostuff/protostuff-compiler/tree/master/protostuff-parser)
 io.protostuff:protostuff-parser:jar:2.2.25
 ribbon (https://github.com/Netflix/ribbon) com.netflix.ribbon:ribbon:jar:2.2.2
 ribbon-core (https://github.com/Netflix/ribbon) 
com.netflix.ribbon:ribbon-core:jar:2.2.2
 ribbon-httpclient (https://github.com/Netflix/ribbon) 
com.netflix.ribbon:ribbon-httpclient:jar:2.2.5


 

----------------------------------------------------------------
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:
[email protected]


> parse proto file to model
> -------------------------
>
>                 Key: SCB-917
>                 URL: https://issues.apache.org/jira/browse/SCB-917
>             Project: Apache ServiceComb
>          Issue Type: Sub-task
>          Components: Java-Chassis
>            Reporter: wujimin
>            Assignee: wujimin
>            Priority: Major
>             Fix For: java-chassis-1.1.0
>
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to