funcmain() {
maskedReq:=&pb.UserInfoRequest{
UserId: "1",
}
maskedReq.
MaskOut_Email(). // masking email field in responseMaskOut_Address() // masking address field in response}
服务端侧:
// 响应裁剪示例func (uuserServer) GetUserInfo(
ctxcontext.Context,
request*pb.UserInfoRequest,
) (*pb.UserInfoResponse, error) {
// FieldMask_Filter means that the fields are included in the response,// otherwise they are omitted.// FieldMask_Prune means masked fields are not included in the response,// otherwise they are included.fm:=request.FieldMask_Filter()
// fm2 := request.FieldMask_Prune()fmt.Printf("userServer.GetUserInfo is called: %v\n", request.String())
resp:=&pb.UserInfoResponse{
UserId: "69781",
Name: "yeqown",
Email: "[email protected]",
Address: nil,
}
// judge if the field masked or not, avoid unnecessary call or calculation.iffm.MaskedOut_Address() {
// filter more, so the address field should be included.resp.Address = &pb.Address{
Country: "China",
Province: "Sichuan",
}
}
// filter the field masked out._ = fm.Mask(resp)
returnresp, nil}
// 增量更新示例func (uuserServer) UpdateUserInfo(ctxcontext.Context, request*pb.UserInfoRequest) (*pb.NonEmpty, error) {
// FieldMask_Filter means that the fields are expected to update,// otherwise they are ignored.// FieldMask_Prune means masked fields are ignored to update,// otherwise they are expected to update.fm:=request.FieldMask_Filter()
// fm2 := request.FieldMask_Prune()fmt.Printf("userServer.UpdateUserInfo is called: %v\n", request.String())
iffm.MaskedIn_UserId() {
// userId want to be updated, so you should use request.UserId to update.fmt.Println("userId want to be updated.")
}
return new(pb.NonEmpty), nil}
// Module describes the interface for a domain-specific code generation module// that can be registered with the PG* generator.typeModuleinterface {
// The Name of the Module, used when establishing the build context and used// as the base prefix for all debugger output.Name() string// InitContext is called on a Module with a pre-configured BuildContext that// should be stored and used by the Module.InitContext(cBuildContext)
// Execute is called on the module with the target Files as well as all// loaded Packages from the gatherer. The module should return a slice of// Artifacts that it would like to be generated.Execute(targetsmap[string]File, packagesmap[string]Package) []Artifact}
func (m*moduleTestSuite) Test_ForDebug() {
// please look up the README at repository root directory to see how to// generate the `debugdata` and code_generator_request binary.req, err:=os.Open("./debugdata/code_generator_request.pb.bin")
m.NoError(err)
fs:=afero.NewMemMapFs()
res:=&bytes.Buffer{}
pgs.Init(
pgs.ProtocInput(req), // use the pre-generated requestpgs.ProtocOutput(res), // capture CodeGeneratorResponsepgs.FileSystem(fs), // capture any custom files written directly to diskpgs.MutateParams(mutateLangParam("go")), // mutate params ).
RegisterModule(m.module).
Render()
}