lundi 29 janvier 2018

Writing generic protobuf serializer in GoLang

I am trying to call the grpc method having arguments of context and custom struct. So, I am stuck how to unmarshal the byte[] into custom struct *DBLogging.Request using reflection given the function name and finally call the respective grpc method

Proto File :

    package DBLogging;

    service Logging {   
        rpc ReadLogs (Request)  returns  (Response)  {} 
    }

    message MetaData
    {  
       string Name  = 1;
       string Id    = 2;
    }

    message Request {
       MetaData LogValue  = 1;
    }

Code :

       type server struct{

        }

        func (s *server) ReadLogs(ctx context.Context, in *DBLogging.Request) (*DBLogging.Response, error) {

        }

        func GenericFoo(server interface {}, funcName string, ctx context.Context, args []byte) {

            method := reflect.ValueOf(server).MethodByName(funcName)

            // how to unmarshal the args in *DBLogging.Request using reflection

            // how to call the grpc function using about unmarshalled data and context

            response := method.Call(inputs, ctx) // how to call the grpc function here 
        }

        func main() {
                var objRequest = &DBLogging.Request{ LogValue : &DBLogging.MetaData {
                            Name         :  "Model",
                            Id :  "123|345",                
                        },          
                    }

                data, err := proto.Marshal(objReadRequest)
                GenericFoo(&server{}, "ReadLogs", context.TODO(), data) 
        }





Aucun commentaire:

Enregistrer un commentaire