I'm having trouble accessing extended protocol buffer members. Here is the scenario:
Message Foo { optional int i = 1; }
message Bar { extend Foo {
optional int j = 10001; } }
I don't have the Bar message within any of my other protos. How can I get Bar.j in Java? All examples I've found require a Bar within a message.
Thanks!
proto file are simple: you add a message for each data structure you want to serialize, then specify a name and a type for each field in the message. Here is the . proto file that defines your messages, addressbook. proto .
Proto3 is the latest version of Protocol Buffers and includes the following changes from proto2: Field presence, also known as hasField , is removed by default for primitive fields. An unset primitive field has a language-defined default value.
Protocol buffers are a combination of the definition language (created in . proto files), the code that the proto compiler generates to interface with data, language-specific runtime libraries, and the serialization format for data that is written to a file (or sent across a network connection).
You can certainly send even a binary payload with an HTTP request, or in an HTTP response. Just write the bytes of the protocol buffer directly into the request/response, and make sure to set the content type to "application/octet-stream". The client, and server, should be able to take care of the rest easily.
This tutorial provides a basic Java programmer's introduction to working with protocol buffers. By walking through creating a simple example application, it shows you how to Define message formats in a .proto file. Use the protocol buffer compiler. Use the Java protocol buffer API to write and read messages.
The message classes generated by the protocol buffer compiler are all immutable. Once a message object is constructed, it cannot be modified, just like a Java String. To construct a message, you must first construct a builder, set any fields you want to set to your chosen values, then call the builder's build () method.
Note that no Java protocol buffer methods accept or return nulls unless otherwise specified. The protocol buffer compiler produces Java output when invoked with the --java_out= command-line flag.
Given an enum definition like: The protocol buffer compiler will generate a Java enum type called Foo with the same set of values. If you are using proto3, it also adds the special value UNRECOGNIZED to the enum type. The values of the generated enum type have the following special methods:
Extensions in Protocol Buffer don't work necessarily as you would expect, i.e. they don't match the Java inheritance mechanism.
For your problem, I have created the following foobar.proto
file:
package test;
message Foo {
optional int32 i = 1;
extensions 10 to 99999;
}
message Bar {
extend Foo {
optional int32 j = 10001;
}
}
It creates Foobar.java
, containing the classes Foobar.Bar
and Foobar.Foo
.
And here is a simple JUnit test case accessing Bar.j:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import test.Foobar.Bar;
import test.Foobar.Foo;
public class TestFooBar {
@Test
public void testFooBar() {
Foo foo = Foo.newBuilder().setI(123).setExtension(Bar.j, 456).build();
assertEquals(Integer.valueOf(456), foo.getExtension(Bar.j));
}
}
Hope that helps clarifying your problem!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With