status-im / nim-protobuf-serialization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Protobuf.encode cannot works with 'Option[float64]' and 'Option[float32]'

jangko opened this issue · comments

decoding works, but encoding cannot

import options
import protobuf_serialization

type
  Apple {.protobuf3.} = object
    color {. pfloat64, fieldNumber: 12.}: Option[float64]
    
var msg = Apple()
let y = Protobuf.encode(msg)

error message:

 Error: Couldnt serialize the float; all floats need their bits specified with a PFloat32 or PFloat64 call.
commented

Aside from the issue, the current intention of the library is that the Option type should not be used with protobuf3. That's because the semantics of Protobuf demand that if the wire message doesn't include a particular value, the readers should transparently get a default value for the type instead. The API should not provide any way for the user to check if the wire message included a particular field or not.

then we have bigger issue than this one, because other Option[T] beside float32 and float64 are serialized and deserialized when using {.protobuf3.} pragma.

btw @zah, what about oneof in proto language, doesn't it require user to check those optional fields exist or not?

commented

oneofcan be mapped to a case object in Nim. During deserialization, we'll automatically set the discriminator to the correct value that can be checked by the user.

Options are in a fascinating gray area. Protobuf uses PBOption for pb2 optional primitives with defaults, Option for proper data types without defaults, and then I actually left the pb3 behavior alone. Despite PB3's lack of acknowledgement of the data type, supporting it enables easier interop with existing Nim data structures. I'd personally like to keep it supported, and fix this issue.

As a side note, when it comes to serialization, Options will either be explicitly banned (compile time error) or they'll work. They won't have the dontSerialize pragma automatically attached. The bigger issue is the future of Options, but I do want to get a fix up for this later tonight. It should affect PB2 and PB3 and therefore need fixing, even if we ban Options from PB3.

Encoding/decoding works fine. It's a problem in verifySerializable. It tries to check if the float is wrapped, not understanding floats don't generally have a wrapped instance.