Package union
Overview ▹
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 ▹
Index ▾
Package files
Internal call graph ▹
Internal call graph ▾
In the call graph viewer below, each node is a function belonging to this package and its children are the functions it calls—perhaps dynamically.
The root nodes are the entry points of the package: functions that may be called from outside the package. There may be non-exported or anonymous functions among them if they are called dynamically from another package.
Click a node to visit that function's source code.
From there you can visit its callers by
clicking its declaring func
token.
Functions may be omitted if they were determined to be unreachable in the particular programs or tests that were analyzed.
func NewTest ¶
func NewTest(g *generator.Generator) testgen.TestPlugin
func NewUnion ¶
func NewUnion() *union
Portions of this page are modifications based on work created and shared by Google
and used according to terms described in the Creative Commons 3.0 Attribution License.
© 2018 ActiveState Software Inc. All rights reserved. Trademarks.