Skip to content

Commit

Permalink
Add Protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
T5750 committed Aug 28, 2021
1 parent 249a4f8 commit 104a564
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 2 deletions.
3 changes: 2 additions & 1 deletion doc/source/utils/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ Utils
zip
gson/index
jackson/index
property/index
property/index
protobuf/index
9 changes: 9 additions & 0 deletions doc/source/utils/protobuf/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Protocol Buffers
===============================

.. toctree::
:maxdepth: 3
:numbered: 0

protobufJava
protobufLanguageGuide
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
<UserAgentUtils.version>1.21</UserAgentUtils.version>
<commons-lang3.version>3.8.1</commons-lang3.version>
<tomcat.version>9.0.41</tomcat.version>
<protobuf.version>3.11.4</protobuf.version>
<!--<os.detected.name>linux</os.detected.name>
<os.detected.arch>x86_64</os.detected.arch>
<os.detected.classifier>linux-x86_64</os.detected.classifier>
-->
<os.detected.classifier>windows-x86_64</os.detected.classifier>
</properties>

<modules>
Expand Down Expand Up @@ -80,6 +86,11 @@
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
3 changes: 2 additions & 1 deletion utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
- [Jackson 2.x](https://github.com/FasterXML/jackson)
- [sigar-1.6.4](https://sourceforge.net/projects/sigar/files/sigar/1.6/)
- [UserAgentUtils 1.21](https://github.com/HaraldWalker/user-agent-utils)
- [Tomcat 9.0.x](https://tomcat.apache.org/download-90.cgi)
- [Tomcat 9.0.x](https://tomcat.apache.org/download-90.cgi)
- [Protobuf 3.x](https://github.com/protocolbuffers/protobuf)
25 changes: 25 additions & 0 deletions utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
</dependencies>

<build>
Expand All @@ -69,6 +73,27 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.25.0:exe:${os.detected.classifier}</pluginArtifact>
<clearOutputDirectory>false</clearOutputDirectory>
<protoSourceRoot>${project.basedir}/src/main/resources/protoc</protoSourceRoot>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
29 changes: 29 additions & 0 deletions utils/src/main/java/t5750/utils/protobuf/ProtobufJavaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package t5750.utils.protobuf;

import t5750.utils.util.Global;

public class ProtobufJavaTest {
public static void main(String[] args) {
EmployeeProto.Employee.PhoneType phoneType = EmployeeProto.Employee.PhoneType.MOBILE;
EmployeeProto.Employee.PhoneNumber phoneNumber = EmployeeProto.Employee.PhoneNumber
.newBuilder().setNumber("1234567").setType(phoneType).build();
EmployeeProto.Employee.AddressType addressType = EmployeeProto.Employee.AddressType.PERMANENT;
EmployeeProto.Employee.Address address = EmployeeProto.Employee.Address
.newBuilder().setStreet("Street").setCity("City").setZip(123456)
.setState("State").setCountry("Country").setType(addressType)
.build();
EmployeeProto.Employee employee = EmployeeProto.Employee.newBuilder()
.setId(1234).setName(Global.T5750.toUpperCase())
.setEmail(Global.T5750 + "@email.com").addPhones(phoneNumber)
.addAddress(address).build();
EmployeeProto.Organization organization = EmployeeProto.Organization
.newBuilder().addEmployee(employee).build();
for (EmployeeProto.Employee emp : organization.getEmployeeList()) {
System.out.println(emp.getId());
System.out.println(emp.getName());
System.out.println(emp.getEmail());
System.out.println(emp.getPhonesList().get(0));
System.out.println(emp.getAddressList().get(0));
}
}
}
43 changes: 43 additions & 0 deletions utils/src/main/resources/protoc/employee.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
syntax = "proto3";

option java_package = "t5750.utils.protobuf";
option java_outer_classname = "EmployeeProto";

message Employee {
int32 id = 1;
string name = 2;
string email = 3;

enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}

message PhoneNumber {
string number = 1;
PhoneType type = 2;
}

repeated PhoneNumber phones = 4;

enum AddressType {
PERMANENT = 0;
COMMUNICATION = 1;
}

message Address {
string street = 1;
string city = 2;
int32 zip = 3;
string state = 4;
string country = 5;
AddressType type = 6;
}

repeated Address address = 5;
}

message Organization {
repeated Employee employee = 1;
}

0 comments on commit 104a564

Please sign in to comment.