Documentation ¶
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/cosmos/gogoproto/test/example/example.proto
Btw all the output can be seen at:
github.com/cosmos/gogoproto/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/cosmos/gogoproto/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 ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
This section is empty.