Created
August 17, 2019 15:11
-
-
Save abserari/fc4f46bd0087a6688a540d2e47950c0e to your computer and use it in GitHub Desktop.
reflect call method grpc method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CallUnary | |
// [3]reflect.Value in[0] is context.Context, in[1] is your message type which generated by pb file. | |
// in[2] is grpc.CallOption | |
func (c *Client) CallUnary(methodName string,in []reflect.Value)(interface{},error) { | |
m, ok := reflect.TypeOf(c.c).MethodByName(methodName) | |
if !ok{ | |
log.Fatal("method not found") | |
return nil,errors.New("method not found") | |
} | |
if len(in) != 3{ | |
return nil, errors.New("mistake arguement") | |
} | |
out:= m.Func.Call(in) | |
if len(out) != 2{ | |
return nil, errors.New("mistake method") | |
} | |
return out[0].Interface(),out[1].Interface().(error) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment