Require *T for typeurl interaction
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
@@ -33,8 +33,8 @@ func Register(v interface{}, args ...string) {
|
||||
// TypeURL returns the type url for a registred type
|
||||
func TypeURL(v interface{}) (string, error) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
u, ok := registry[tryDereference(v)]
|
||||
mu.Unlock()
|
||||
if !ok {
|
||||
// fallback to the proto registry if it is a proto message
|
||||
pb, ok := v.(proto.Message)
|
||||
@@ -46,7 +46,10 @@ func TypeURL(v interface{}) (string, error) {
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// Is returns true if the type of the Any is the same as v
|
||||
func Is(any *types.Any, v interface{}) bool {
|
||||
// call to check that v is a pointer
|
||||
tryDereference(v)
|
||||
url, err := TypeURL(v)
|
||||
if err != nil {
|
||||
return false
|
||||
@@ -54,11 +57,9 @@ func Is(any *types.Any, v interface{}) bool {
|
||||
return any.TypeUrl == url
|
||||
}
|
||||
|
||||
// MarshalAny marshals the value v into an any with the correct TypeUrl
|
||||
func MarshalAny(v interface{}) (*types.Any, error) {
|
||||
var (
|
||||
err error
|
||||
data []byte
|
||||
)
|
||||
var data []byte
|
||||
url, err := TypeURL(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -78,6 +79,7 @@ func MarshalAny(v interface{}) (*types.Any, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UnmarshalAny unmarshals the any type into a concrete type
|
||||
func UnmarshalAny(any *types.Any) (interface{}, error) {
|
||||
t, err := getTypeByUrl(any.TypeUrl)
|
||||
if err != nil {
|
||||
@@ -120,7 +122,8 @@ func getTypeByUrl(url string) (urlType, error) {
|
||||
func tryDereference(v interface{}) reflect.Type {
|
||||
t := reflect.TypeOf(v)
|
||||
if t.Kind() == reflect.Ptr {
|
||||
t = t.Elem()
|
||||
// require check of pointer but dereference to register
|
||||
return t.Elem()
|
||||
}
|
||||
return t
|
||||
panic("v is not a pointer to a type")
|
||||
}
|
||||
|
||||
@@ -18,35 +18,7 @@ func clear() {
|
||||
registry = make(map[reflect.Type]string)
|
||||
}
|
||||
|
||||
func TestRegsiterValueGetValue(t *testing.T) {
|
||||
clear()
|
||||
expected := filepath.Join(Prefix, "test")
|
||||
Register(test{}, "test")
|
||||
|
||||
url, err := TypeURL(test{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if url != expected {
|
||||
t.Fatalf("expected %q but received %q", expected, url)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegsiterValueGetPointer(t *testing.T) {
|
||||
clear()
|
||||
expected := filepath.Join(Prefix, "test")
|
||||
Register(test{}, "test")
|
||||
|
||||
url, err := TypeURL(&test{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if url != expected {
|
||||
t.Fatalf("expected %q but received %q", expected, url)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegsiterPointerGetPointer(t *testing.T) {
|
||||
func TestRegisterPointerGetPointer(t *testing.T) {
|
||||
clear()
|
||||
expected := filepath.Join(Prefix, "test")
|
||||
Register(&test{}, "test")
|
||||
@@ -60,24 +32,10 @@ func TestRegsiterPointerGetPointer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegsiterPointerGetValue(t *testing.T) {
|
||||
clear()
|
||||
expected := filepath.Join(Prefix, "test")
|
||||
Register(&test{}, "test")
|
||||
|
||||
url, err := TypeURL(test{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if url != expected {
|
||||
t.Fatalf("expected %q but received %q", expected, url)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshal(t *testing.T) {
|
||||
clear()
|
||||
expected := filepath.Join(Prefix, "test")
|
||||
Register(test{}, "test")
|
||||
Register(&test{}, "test")
|
||||
|
||||
v := &test{
|
||||
Name: "koye",
|
||||
@@ -94,7 +52,7 @@ func TestMarshal(t *testing.T) {
|
||||
|
||||
func TestMarshalUnmarshal(t *testing.T) {
|
||||
clear()
|
||||
Register(test{}, "test")
|
||||
Register(&test{}, "test")
|
||||
|
||||
v := &test{
|
||||
Name: "koye",
|
||||
@@ -122,7 +80,7 @@ func TestMarshalUnmarshal(t *testing.T) {
|
||||
|
||||
func TestIs(t *testing.T) {
|
||||
clear()
|
||||
Register(test{}, "test")
|
||||
Register(&test{}, "test")
|
||||
|
||||
v := &test{
|
||||
Name: "koye",
|
||||
@@ -132,7 +90,7 @@ func TestIs(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !Is(any, test{}) {
|
||||
if !Is(any, &test{}) {
|
||||
t.Fatal("Is(any, test{}) should be true")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user