[infinispan-dev] Protostream marshaller support for optional fields

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[infinispan-dev] Protostream marshaller support for optional fields

Galder Zamarreño
Hey Adrian,

Quick q: how is a protostream marshaller supposed to deal with optional fields?

I don't see any writer methods that deal with those... is it up to the user to put something on the wire to decide at read time whether the optional field follows or not?

Cheers,
--
Galder Zamarreño
Infinispan, Red Hat


_______________________________________________
infinispan-dev mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/infinispan-dev
Reply | Threaded
Open this post in threaded view
|

Re: [infinispan-dev] Protostream marshaller support for optional fields

Adrian Nistor
Hi Galder,

There's nothing special about optional fields. It is the required fields
that are special: they do not allow null (in the read/write methods).

If a field is optional in your protobuf schema you can write a null
value and protostream will happily interpret that as a missing value and
will not write any tag in the output protobuf stream.
Reading a missing value will return a null. The required/optional status
of a field is checked against the schema at runtime, so you cannot cheat :).

None of the above holds true for a required field. Writes will only
accept non-nulls (an exception is thrown immediately). Reads will always
return a non-null value, so you can safely use a
primitive-type-returning read method. If the input stream does not
contain a required field (probably because the encoder that produced it
was violating the schema) an exception is thrown.

Adrian

On 10/23/2017 06:51 PM, Galder Zamarreño wrote:

> Hey Adrian,
>
> Quick q: how is a protostream marshaller supposed to deal with optional fields?
>
> I don't see any writer methods that deal with those... is it up to the user to put something on the wire to decide at read time whether the optional field follows or not?
>
> Cheers,
> --
> Galder Zamarreño
> Infinispan, Red Hat
>

_______________________________________________
infinispan-dev mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/infinispan-dev
Reply | Threaded
Open this post in threaded view
|

Re: [infinispan-dev] Protostream marshaller support for optional fields

Galder Zamarreño
Ok, thanks for the clarification.

> On 24 Oct 2017, at 13:43, Adrian Nistor <[hidden email]> wrote:
>
> Hi Galder,
>
> There's nothing special about optional fields. It is the required fields that are special: they do not allow null (in the read/write methods).
>
> If a field is optional in your protobuf schema you can write a null value and protostream will happily interpret that as a missing value and will not write any tag in the output protobuf stream.
> Reading a missing value will return a null. The required/optional status of a field is checked against the schema at runtime, so you cannot cheat :).
>
> None of the above holds true for a required field. Writes will only accept non-nulls (an exception is thrown immediately). Reads will always return a non-null value, so you can safely use a primitive-type-returning read method. If the input stream does not contain a required field (probably because the encoder that produced it was violating the schema) an exception is thrown.
>
> Adrian
>
> On 10/23/2017 06:51 PM, Galder Zamarreño wrote:
>> Hey Adrian,
>>
>> Quick q: how is a protostream marshaller supposed to deal with optional fields?
>>
>> I don't see any writer methods that deal with those... is it up to the user to put something on the wire to decide at read time whether the optional field follows or not?
>>
>> Cheers,
>> --
>> Galder Zamarreño
>> Infinispan, Red Hat
>>
>

--
Galder Zamarreño
Infinispan, Red Hat


_______________________________________________
infinispan-dev mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/infinispan-dev