A tag indicates to the decoder what is coming next in the input stream, and
allows the decoder to handle it appropriately. This is a key feature of the
protobuf wire format - this enables forward and backward compatibility, as
well omitting fields that aren't present from the serialization. The APIs
that we have are built around this. A readString(int tag) method would never
be used by the generated code.

If you really want to take this approach, you can still call readTag(),
check that the tag number is what you want, and then read the string.
However, I would suggest just using a message. You may find that over time
those two or three parameters will grow to the point that maintaining this
is unwieldy.

On Wed, Aug 11, 2010 at 10:55 PM, Prakash Rao <prakashrao1...@gmail.com>wrote:

> I don't have .proto message created for these. Most of my APIs
> takes .proto messages as parameters (as they represent my business
> data objects) and in 1-2 APIs I need to pass only 2-3 primitive
> parameters as arguments. In these cases I’m avoiding creation
> of .proto messages (as these don’t represent my business data object).
> As an alternative, I thought of using CodedOutputstream to pass
> parameters along with tag so that retrieval is easy on the receiving
> end using CodedInputStream but I don't see a way to retrieve parameter
> using tags with CodedInputStream. Currently I've the below code and
> thought the tag approach would make it simple but CodedInputStream
> doesn't support parameter retrieval based on tags.
>
> // Client Side code - which sends s1 & s2 strings to server
> int s1Size = CodedOutputStream.computeStringSizeNoTag(s1);
> codedOutputStream.writeRawVarint32(s1Size);
> codedOutputStream.writeStringNoTag(s1);
>
> int s2Size = CodedOutputStream.computeStringSizeNoTag(s2);
>

Note: computeStringSizeNoTag() gives the size of the string as well as the
encoded length. If you write the result as the encoded length, you will end
up reading more bytes than you should. You simply want to write the size of
the string.


> codedOutputStream.writeRawVarint32(s2Size);
> codedOutputStream.writeStringNoTag(s2);
>
> // Server side code - which reads s1 & s2 string
> CodedInputStream codedInputStream =
> CodedInputStream.newInstance(inputStream);
> int s1Size = codedInputStream.readRawVarint32();
> int s1SizeEntry = codedInputStream.pushLimit(s1Size);
> String s1 = codedInputStream.readString();
> codedInputStream.popLimit(s1SizeEntry);
>
> int s2Size = codedInputStream.readRawVarint32();
> int s2SizeEntry = codedInputStream.pushLimit(s2Size);
> String s2 = codedInputStream.readString();
> codedInputStream.popLimit(modifiedBySizeEntry);
>
> Regards,
> Prakash
>
> On Aug 11, 9:17 pm, Jason Hsueh <jas...@google.com> wrote:
> > Suppose those strings are all defined as optional fields in the message.
> > They may not be present in the serialization, so the decoder needs to
> read a
> > tag to determine the field number and type before handling the value.
> >
> > On Wed, Aug 11, 2010 at 8:37 AM, Prakash Rao <prakashrao1...@gmail.com
> >wrote:
> >
> >
> >
> > > Hi,
> > > I was just looking at CodedInputStream class and there are APIs to
> > > write primitives with or without tag (for example, writeString &
> > > writeStringNoTag). I thought if we set unique tags for multiple
> > > parameters then we would be able to retrieve these parameters using
> > > tag numbers specified using CodedInputStream class. I don't see any
> > > APIs to read values based on tags in CodedInputStream. Am I missing
> > > something? For the time being I’m using pushLimit & popLimit APIs to
> > > get different parameter values (based on serializedSize).
> >
> > > For example,
> > > int test(String s1, String s2, String s3) {
> > > }
> >
> > > CodedOutputStream out = CodedOutputStream.newIInstance("Some Output
> > > Stream");
> > > out.writeString(1, s1);
> > > out.writeString(2, s2);
> > > out.writeString(3, s3);
> > > out.flush();
> >
> > > Why these are not possible?
> > > CodedInputStream in = CodedInputStream.newIInstance("Some Input
> > > Stream");
> > > String s1 = in.readString(1);
> > > String s2 = in.readString(2);
> > > String s3 = in.readString(3);
> >
> > > Regards,
> > > Prakash
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "Protocol Buffers" group.
> > > To post to this group, send email to proto...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > protobuf+unsubscr...@googlegroups.com<protobuf%2bunsubscr...@googlegroups.com>
> <protobuf%2bunsubscr...@googlegroups.c­om>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/protobuf?hl=en.- Hide quoted text -
> >
> > - Show quoted text -
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To post to this group, send email to proto...@googlegroups.com.
> To unsubscribe from this group, send email to
> protobuf+unsubscr...@googlegroups.com<protobuf%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/protobuf?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to proto...@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to