├── .gitignore ├── LICENSE ├── README.md ├── code-generation ├── plugin-proto-compiler │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── io │ │ │ │ └── protostuff │ │ │ │ └── examples │ │ │ │ └── DummySearchService.java │ │ ├── proto │ │ │ └── search.proto │ │ └── stg │ │ │ └── $Service.java.stg │ │ └── test │ │ └── java │ │ └── io │ │ └── protostuff │ │ └── examples │ │ └── DummySearchServiceTest.java └── pom.xml ├── hello-world ├── pom.xml └── src │ ├── main │ ├── java │ │ └── io │ │ │ └── protostuff │ │ │ └── examples │ │ │ └── HelloService.java │ └── proto │ │ └── hello.proto │ └── test │ └── java │ └── io │ └── protostuff │ └── examples │ └── HelloServiceTest.java ├── multimodule-imports ├── a │ ├── pom.xml │ └── src │ │ └── main │ │ └── proto │ │ └── io │ │ └── pt │ │ └── a │ │ └── a.proto ├── b │ ├── pom.xml │ └── src │ │ └── main │ │ └── proto │ │ └── io │ │ └── pt │ │ └── b │ │ └── b.proto └── pom.xml ├── pom.xml └── runtime-schema-usage ├── pom.xml └── src └── test └── java └── io └── protostuff └── examples ├── Bar.java ├── Foo.java └── RuntimeSchemaUsage.java /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | pom.xml.tag 3 | pom.xml.releaseBackup 4 | pom.xml.versionsBackup 5 | pom.xml.next 6 | release.properties 7 | 8 | .idea/ 9 | *.iml -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # examples 2 | Protostuff usage examples 3 | -------------------------------------------------------------------------------- /code-generation/plugin-proto-compiler/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | code-generation 7 | io.protostuff.examples 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | plugin-proto-compiler 13 | 14 | 15 | 16 | io.protostuff 17 | protostuff-core 18 | ${protostuff.version} 19 | 20 | 21 | com.google.guava 22 | guava 23 | ${guava.version} 24 | 25 | 26 | junit 27 | junit 28 | ${junit.version} 29 | test 30 | 31 | 32 | 33 | 34 | 35 | 36 | io.protostuff 37 | protostuff-maven-plugin 38 | ${protostuff.version} 39 | 40 | 41 | 42 | ppc.check_filename_placeholder 43 | true 44 | 45 | 46 | 47 | 48 | src/main/proto 49 | target/generated-sources/proto 50 | 51 | java_bean, 52 | src/main/stg/$Service.java.stg 53 | 54 | 55 | 56 | 57 | 58 | 59 | generate-sources 60 | generate-sources 61 | 62 | compile 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /code-generation/plugin-proto-compiler/src/main/java/io/protostuff/examples/DummySearchService.java: -------------------------------------------------------------------------------- 1 | package io.protostuff.examples; 2 | 3 | import com.google.common.util.concurrent.Futures; 4 | import com.google.common.util.concurrent.ListenableFuture; 5 | 6 | import java.util.Arrays; 7 | 8 | /** 9 | * Service implementation. 10 | * 11 | * Interface for {@code SearchService} and all classes for request/responses 12 | * are generated by protostuff-maven-plugin. 13 | * 14 | * @author Konstantin Shchepanovskyi 15 | */ 16 | public class DummySearchService implements SearchService { 17 | 18 | @Override 19 | public ListenableFuture basicSearch(BasicSearchRequest request) { 20 | SearchResponse response = new SearchResponse(); 21 | response.setResultList(Arrays.asList( 22 | "result 1", 23 | "result 2" 24 | )); 25 | return Futures.immediateFuture(response); 26 | } 27 | 28 | @Override 29 | public ListenableFuture extendedSearch(ExtendedSearchRequest request) { 30 | SearchResponse response = new SearchResponse(); 31 | response.setResultList(Arrays.asList( 32 | "result 1", 33 | "result 2", 34 | "result 3" 35 | )); 36 | return Futures.immediateFuture(response); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /code-generation/plugin-proto-compiler/src/main/proto/search.proto: -------------------------------------------------------------------------------- 1 | package search; 2 | 3 | option java_package = "io.protostuff.examples"; 4 | 5 | message BasicSearchRequest { 6 | required string keyword = 1; 7 | } 8 | 9 | message ExtendedSearchRequest { 10 | repeated string keywords = 1; 11 | } 12 | 13 | message SearchResponse { 14 | repeated string result = 1; 15 | } 16 | 17 | service Search { 18 | rpc basicSearch (BasicSearchRequest) returns (SearchResponse); 19 | rpc extendedSearch (ExtendedSearchRequest) returns (SearchResponse); 20 | } 21 | -------------------------------------------------------------------------------- /code-generation/plugin-proto-compiler/src/main/stg/$Service.java.stg: -------------------------------------------------------------------------------- 1 | group service : base; 2 | 3 | service_block(service, module, options) ::= << 4 | package ; 5 | 6 | import javax.annotation.Generated; 7 | 8 | import com.google.common.util.concurrent.ListenableFuture; 9 | 10 | @Generated("") 11 | public interface Service { 12 | 13 | 14 | } 15 | 16 | >> 17 | 18 | service_methods(service, module, options) ::= << 19 | 20 | }; 22 | separator="\n\n"> 23 | >> 24 | 25 | service_method(method, service, module, options) ::= << 26 | ListenableFuture\<\> ( request); 27 | >> 28 | -------------------------------------------------------------------------------- /code-generation/plugin-proto-compiler/src/test/java/io/protostuff/examples/DummySearchServiceTest.java: -------------------------------------------------------------------------------- 1 | package io.protostuff.examples; 2 | 3 | import com.google.common.util.concurrent.ListenableFuture; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | import java.util.Arrays; 8 | 9 | /** 10 | * Tests for {@code DummySearchService} 11 | * 12 | * @author Kostiantyn Shchepanovskyi 13 | */ 14 | public class DummySearchServiceTest { 15 | 16 | private final DummySearchService service = new DummySearchService(); 17 | 18 | @Test 19 | public void testBasicSearch() throws Exception { 20 | ListenableFuture result = service.basicSearch(new BasicSearchRequest()); 21 | Assert.assertEquals(Arrays.asList("result 1", "result 2"), result.get().getResultList()); 22 | } 23 | 24 | @Test 25 | public void testExtendedSearch() throws Exception { 26 | ListenableFuture result = service.extendedSearch(new ExtendedSearchRequest()); 27 | Assert.assertEquals(Arrays.asList("result 1", "result 2", "result 3"), result.get().getResultList()); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /code-generation/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 4.0.0 7 | pom 8 | 9 | 10 | protostuff-examples 11 | io.protostuff.examples 12 | 1.0-SNAPSHOT 13 | 14 | 15 | code-generation 16 | 17 | 18 | plugin-proto-compiler 19 | 20 | 21 | -------------------------------------------------------------------------------- /hello-world/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | protostuff-examples 7 | io.protostuff.examples 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | hello-world 13 | 14 | 15 | 16 | io.protostuff 17 | protostuff-core 18 | ${protostuff.version} 19 | 20 | 21 | junit 22 | junit 23 | ${junit.version} 24 | 25 | 26 | 27 | 28 | 29 | 30 | io.protostuff 31 | protostuff-maven-plugin 32 | ${protostuff.version} 33 | 34 | 35 | 36 | 37 | src/main/proto 38 | target/generated-sources/proto 39 | java_bean 40 | 41 | 42 | generate_field_map 43 | true 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | generate-sources 52 | generate-sources 53 | 54 | compile 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /hello-world/src/main/java/io/protostuff/examples/HelloService.java: -------------------------------------------------------------------------------- 1 | package io.protostuff.examples; 2 | 3 | import io.protostuff.LinkedBuffer; 4 | import io.protostuff.ProtobufIOUtil; 5 | import io.protostuff.Schema; 6 | 7 | import java.io.ByteArrayOutputStream; 8 | import java.io.IOException; 9 | 10 | /** 11 | * Sample {@code HelloService} implementation. 12 | * 13 | * @author Kostiantyn Shchepanovskyi 14 | */ 15 | public class HelloService { 16 | 17 | 18 | public static final Schema SEARCH_REQUEST_SCHEMA = HelloRequest.getSchema(); 19 | public static final Schema SEARCH_RESPONSE_SCHEMA = HelloResponse.getSchema(); 20 | 21 | /** 22 | * Sample "hello" rpc method handler. 23 | * 24 | * @param requestData {@code HelloRequest} binary array encoded using Protocol Buffers 25 | * @return {@code HelloResponse} binary array encoded using Protocol Buffers 26 | */ 27 | public byte[] hello(byte[] requestData) throws IOException { 28 | HelloRequest request = deserialize(requestData); 29 | HelloResponse response = hello(request); 30 | return serialize(response); 31 | } 32 | 33 | private byte[] serialize(HelloResponse response) throws IOException { 34 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 35 | LinkedBuffer buffer = LinkedBuffer.allocate(); 36 | ProtobufIOUtil.writeTo(outputStream, response, SEARCH_RESPONSE_SCHEMA, buffer); 37 | return outputStream.toByteArray(); 38 | } 39 | 40 | private HelloRequest deserialize(byte[] requestData) { 41 | HelloRequest request = SEARCH_REQUEST_SCHEMA.newMessage(); 42 | ProtobufIOUtil.mergeFrom(requestData, request, SEARCH_REQUEST_SCHEMA); 43 | return request; 44 | } 45 | 46 | /** 47 | * Service method implementation. 48 | */ 49 | private HelloResponse hello(HelloRequest request) { 50 | HelloResponse response = new HelloResponse(); 51 | String name = request.getName(); 52 | response.setGreeting("Hello, " + name); 53 | return response; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /hello-world/src/main/proto/hello.proto: -------------------------------------------------------------------------------- 1 | package search; 2 | 3 | option java_package = "io.protostuff.examples"; 4 | 5 | message HelloRequest { 6 | optional string name = 1; 7 | } 8 | 9 | message HelloResponse { 10 | optional string greeting = 1; 11 | } 12 | 13 | service HelloService { 14 | rpc hello (HelloRequest) returns (HelloResponse); 15 | } 16 | -------------------------------------------------------------------------------- /hello-world/src/test/java/io/protostuff/examples/HelloServiceTest.java: -------------------------------------------------------------------------------- 1 | package io.protostuff.examples; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | /** 8 | * Tests for {@code HelloService} 9 | * 10 | * @author Kostiantyn Shchepanovskyi 11 | */ 12 | public class HelloServiceTest { 13 | 14 | public static final byte[] REQUEST = new byte[]{0x0A, 0x03, '4', '2', '!'}; 15 | public static final byte[] RESPONSE = new byte[]{0x0A, 0x0A, 'H', 'e', 'l', 'l', 'o', ',', ' ', '4', '2', '!'}; 16 | 17 | private HelloService service; 18 | 19 | @Before 20 | public void setUp() throws Exception { 21 | service = new HelloService(); 22 | } 23 | 24 | @Test 25 | public void testSearch() throws Exception { 26 | byte[] responseData = service.hello(REQUEST); 27 | Assert.assertArrayEquals(RESPONSE, responseData); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /multimodule-imports/a/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | multimodule-imports 7 | io.protostuff.examples 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | a 13 | 14 | 15 | 16 | io.protostuff 17 | protostuff-core 18 | 1.3.6 19 | 20 | 21 | 22 | 23 | 24 | 25 | ${project.basedir}/src/main/proto 26 | 27 | 28 | 29 | 30 | io.protostuff 31 | protostuff-maven-plugin 32 | ${protostuff.version} 33 | 34 | 35 | 36 | 37 | ${project.basedir}/src/main/proto 38 | ${project.build.directory}/generated-sources/proto 39 | java_bean 40 | 41 | 42 | generate_field_map 43 | true 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | generate-sources 52 | generate-sources 53 | 54 | compile 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /multimodule-imports/a/src/main/proto/io/pt/a/a.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package io.pt.a; 4 | 5 | message A { 6 | optional string text = 1; 7 | } 8 | -------------------------------------------------------------------------------- /multimodule-imports/b/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | multimodule-imports 7 | io.protostuff.examples 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | b 13 | 14 | 15 | 16 | 17 | 18 | io.protostuff.examples 19 | a 20 | ${project.version} 21 | 22 | 23 | io.protostuff 24 | protostuff-core 25 | 1.3.6 26 | 27 | 28 | 29 | 30 | 31 | 32 | ${project.basedir}/src/main/proto 33 | 34 | 35 | 36 | 37 | io.protostuff 38 | protostuff-maven-plugin 39 | ${protostuff.version} 40 | 41 | 42 | 43 | 44 | ${project.basedir}/src/main/proto 45 | ${project.build.directory}/generated-sources/proto 46 | java_bean 47 | 48 | 49 | generate_field_map 50 | true 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | generate-sources 59 | generate-sources 60 | 61 | compile 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /multimodule-imports/b/src/main/proto/io/pt/b/b.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package io.pt.b; 4 | 5 | import "io/pt/a/a.proto"; 6 | 7 | message B { 8 | // use message defined in module "a" 9 | optional A a = 1; 10 | } 11 | -------------------------------------------------------------------------------- /multimodule-imports/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | protostuff-examples 7 | io.protostuff.examples 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | multimodule-imports 13 | pom 14 | 15 | a 16 | b 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.protostuff.examples 8 | protostuff-examples 9 | 1.0-SNAPSHOT 10 | pom 11 | 12 | 13 | UTF-8 14 | 1.3.7 15 | 18.0 16 | 4.12 17 | 18 | 19 | 20 | code-generation 21 | hello-world 22 | multimodule-imports 23 | runtime-schema-usage 24 | 25 | 26 | 27 | 28 | 29 | 30 | maven-compiler-plugin 31 | 3.2 32 | 33 | 1.8 34 | 1.8 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /runtime-schema-usage/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | protostuff-examples 7 | io.protostuff.examples 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | runtime-schema-usage 13 | 14 | 15 | 16 | 17 | io.protostuff 18 | protostuff-core 19 | ${protostuff.version} 20 | 21 | 22 | io.protostuff 23 | protostuff-runtime 24 | ${protostuff.version} 25 | 26 | 27 | junit 28 | junit 29 | ${junit.version} 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /runtime-schema-usage/src/test/java/io/protostuff/examples/Bar.java: -------------------------------------------------------------------------------- 1 | package io.protostuff.examples; 2 | 3 | /** 4 | * @author Kostiantyn Shchepanovskyi 5 | */ 6 | public class Bar { 7 | 8 | private final int id; 9 | private final String name; 10 | 11 | private Bar(Builder builder) { 12 | id = builder.id; 13 | name = builder.name; 14 | } 15 | 16 | public static Builder newBuilder() { 17 | return new Builder(); 18 | } 19 | 20 | public int getId() { 21 | return id; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | @Override 29 | public boolean equals(Object o) { 30 | if (this == o) return true; 31 | if (o == null || getClass() != o.getClass()) return false; 32 | 33 | Bar bar = (Bar) o; 34 | 35 | if (id != bar.id) return false; 36 | return !(name != null ? !name.equals(bar.name) : bar.name != null); 37 | 38 | } 39 | 40 | @Override 41 | public int hashCode() { 42 | int result = id; 43 | result = 31 * result + (name != null ? name.hashCode() : 0); 44 | return result; 45 | } 46 | 47 | @Override 48 | public String toString() { 49 | return "Bar{" + 50 | "id=" + id + 51 | ", name='" + name + '\'' + 52 | '}'; 53 | } 54 | 55 | public static final class Builder { 56 | private int id; 57 | private String name; 58 | 59 | private Builder() { 60 | } 61 | 62 | public Builder id(int id) { 63 | this.id = id; 64 | return this; 65 | } 66 | 67 | public Builder name(String name) { 68 | this.name = name; 69 | return this; 70 | } 71 | 72 | public Bar build() { 73 | return new Bar(this); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /runtime-schema-usage/src/test/java/io/protostuff/examples/Foo.java: -------------------------------------------------------------------------------- 1 | package io.protostuff.examples; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * @author Kostiantyn Shchepanovskyi 7 | */ 8 | public class Foo { 9 | 10 | private Bar bar; 11 | private Map map; 12 | 13 | public Bar getBar() { 14 | return bar; 15 | } 16 | 17 | public void setBar(Bar bar) { 18 | this.bar = bar; 19 | } 20 | 21 | public Map getMap() { 22 | return map; 23 | } 24 | 25 | public void setMap(Map map) { 26 | this.map = map; 27 | } 28 | 29 | @Override 30 | public boolean equals(Object o) { 31 | if (this == o) return true; 32 | if (o == null || getClass() != o.getClass()) return false; 33 | 34 | Foo foo = (Foo) o; 35 | 36 | if (bar != null ? !bar.equals(foo.bar) : foo.bar != null) return false; 37 | return !(map != null ? !map.equals(foo.map) : foo.map != null); 38 | 39 | } 40 | 41 | @Override 42 | public int hashCode() { 43 | int result = bar != null ? bar.hashCode() : 0; 44 | result = 31 * result + (map != null ? map.hashCode() : 0); 45 | return result; 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return "Foo{" + 51 | "bar=" + bar + 52 | ", map=" + map + 53 | '}'; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /runtime-schema-usage/src/test/java/io/protostuff/examples/RuntimeSchemaUsage.java: -------------------------------------------------------------------------------- 1 | package io.protostuff.examples; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import java.io.ByteArrayOutputStream; 7 | import java.util.HashMap; 8 | import java.util.Map; 9 | 10 | import io.protostuff.LinkedBuffer; 11 | import io.protostuff.ProtobufIOUtil; 12 | import io.protostuff.Schema; 13 | import io.protostuff.runtime.RuntimeSchema; 14 | 15 | /** 16 | * @author Kostiantyn Shchepanovskyi 17 | */ 18 | public class RuntimeSchemaUsage { 19 | 20 | private final LinkedBuffer BUFFER = LinkedBuffer.allocate();; 21 | private final Schema SCHEMA = RuntimeSchema.getSchema(Foo.class);; 22 | 23 | @Test 24 | public void serializeAndDeserialize() throws Exception { 25 | Foo foo = createFooInstance(); 26 | byte[] bytes = serialize(foo); 27 | Foo x = deserialize(bytes); 28 | Assert.assertEquals(foo, x); 29 | } 30 | 31 | private byte[] serialize(Foo foo) throws java.io.IOException { 32 | ByteArrayOutputStream temp = new ByteArrayOutputStream(); 33 | ProtobufIOUtil.writeTo(temp, foo, SCHEMA, BUFFER); 34 | return temp.toByteArray(); 35 | } 36 | 37 | private Foo deserialize(byte[] bytes) { 38 | Foo tmp = SCHEMA.newMessage(); 39 | ProtobufIOUtil.mergeFrom(bytes, tmp, SCHEMA); 40 | return tmp; 41 | } 42 | 43 | private Foo createFooInstance() { 44 | Foo foo; 45 | foo = new Foo(); 46 | foo.setBar(Bar.newBuilder() 47 | .id(42) 48 | .name("Bar") 49 | .build()); 50 | Map map = new HashMap<>(); 51 | map.put(1, "One"); 52 | map.put(42, "Forty Two"); 53 | foo.setMap(map); 54 | return foo; 55 | } 56 | 57 | 58 | } 59 | --------------------------------------------------------------------------------