How would you guys model the one_of feature described 
[here](https://developers.google.com/protocol-buffers/docs/proto3#oneof) in nim?

I was thinking along these lines: Given the proto file: 
    
    
    message ConformanceRequest {
      oneof payload {
        bytes protobuf_payload = 1;
        string json_payload = 2;
      }
      WireFormat requested_output_format = 3;
    }
    

Compile it to the following nim file: 
    
    
    PbMessage ConformanceRequest*:
      PbOneOf payload:
        protobuf_payload: PbBytes @1
        json_payload: PbString @2
      requested_output_format: WireFormat @3
    

And then let the macros then expand to something like: 
    
    
    type
      ConformanceRequest_payload_enum = enum
        not_set = 0,
        protobuf_payload,
        json_payload
    
    type
      ConformanceRequest_payload_inner {.union.} = object
        protobuf_payload: seq[uint8]
        json_payload: string
    
    type
      ConformanceRequest_payload = object
        kind: ConformanceRequest_payload_enum
        inner: ConformanceRequest_payload_inner
    
    type
      ConformanceRequest = object
        payload: ConformanceRequest_payload
        requested_output_format: WireFormat
    
    # some `.` overload on ConformanceRequest_payload to set the kind according 
to which payload you set
    # not sure if `.` overload can see if it is used as getter or setter
    

And then use it like: 
    
    
    var cr : ConformanceRequest
    assert(cr.payload.kind == not_set)
    cr.payload.json_payload = "Hello"
    assert(cr.payload.kind == json_payload)
    

I'm not really sure of the usage of the Kind enum is idiomatic nim style and 
also the manual says the union pragma doesn't support garbage collected values, 
so I guess the seq and string are off limits?

The C++ implementation generates a bunch of has_xxx() and set_xxx() functions, 
but I think that is pretty ugly, nim should do better right?

Reply via email to