Package face
Overview ▹
Overview ▾
The face plugin generates a function will be generated which can convert a structure which satisfies an interface (face) to the specified structure. This interface contains getters for each of the fields in the struct. The specified struct is also generated with the getters. This means that getters should be turned off so as not to conflict with face getters. This allows it to satisfy its own face.
It is enabled by the following extensions:
- face - face_all
Turn off getters by using the following extensions:
- getters - getters_all
The face plugin also generates a test given it is enabled using one of the following extensions:
- testgen - testgen_all
Let us 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 A { option (gogoproto.face) = true; option (gogoproto.goproto_getters) = false; optional string Description = 1 [(gogoproto.nullable) = false]; optional int64 Number = 2 [(gogoproto.nullable) = false]; optional bytes Id = 3 [(gogoproto.customtype) = "github.com/gogo/protobuf/test/custom.Uuid", (gogoproto.nullable) = false]; }
given to the face plugin, will generate the following code:
type AFace interface { Proto() github_com_gogo_protobuf_proto.Message GetDescription() string GetNumber() int64 GetId() github_com_gogo_protobuf_test_custom.Uuid } func (this *A) Proto() github_com_gogo_protobuf_proto.Message { return this } func (this *A) TestProto() github_com_gogo_protobuf_proto.Message { return NewAFromFace(this) } func (this *A) GetDescription() string { return this.Description } func (this *A) GetNumber() int64 { return this.Number } func (this *A) GetId() github_com_gogo_protobuf_test_custom.Uuid { return this.Id } func NewAFromFace(that AFace) *A { this := &A{} this.Description = that.GetDescription() this.Number = that.GetNumber() this.Id = that.GetId() return this }
and the following test code:
func TestAFace(t *testing7.T) { popr := math_rand7.New(math_rand7.NewSource(time7.Now().UnixNano())) p := NewPopulatedA(popr, true) msg := p.TestProto() if !p.Equal(msg) { t.Fatalf("%#v !Face Equal %#v", msg, p) } }
The struct A, representing the message, will also be generated just like always. As you can see A satisfies its own Face, AFace.
Creating another struct which satisfies AFace is very easy. Simply create all these methods specified in AFace. Implementing The Proto method is done with the helper function NewAFromFace:
func (this *MyStruct) Proto() proto.Message { return NewAFromFace(this) }
just the like TestProto method which is used to test the NewAFromFace function.
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 NewPlugin ¶
func NewPlugin() *plugin
func NewTest ¶
func NewTest(g *generator.Generator) testgen.TestPlugin
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.