Documentation ¶
Overview ¶
The onlyone plugin generates code for the onlyone extension. All fields must be nullable and only one of the fields may be set, like a union. Two methods are generated
GetValue() interface{}
and
SetValue(v interface{}) (set bool)
These provide easier interaction with a onlyone.
The onlyone extension is not called union as this causes compile errors in the C++ generated code. There can only be one ;)
It is enabled by the following extensions:
- onlyone
- onlyone_all
The onlyone plugin also generates a test given it is enabled using one of the following extensions:
- testgen
- testgen_all
Lets look at:
github.com/gogo/protobuf/test/example/example.proto
Btw all the output can be seen at:
github.com/gogo/protobuf/test/example/*
The following message:
message U { option (gogoproto.onlyone) = true; optional A A = 1; optional B B = 2; }
given to the onlyone plugin, will generate code which looks a lot like this:
func (this *U) GetValue() interface{} { if this.A != nil { return this.A } if this.B != nil { return this.B } return nil } func (this *U) SetValue(value interface{}) bool { switch vt := value.(type) { case *A: this.A = vt case *B: this.B = vt default: return false } return true }
and the following test code:
func TestUUnion(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) p := NewPopulatedU(popr) v := p.GetValue() msg := &U{} if !msg.SetValue(v) { t.Fatalf("Union: Could not set Value") } if !p.Equal(msg) { t.Fatalf("%#v !Union Equal %#v", msg, p) } }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
This section is empty.