Replace events/convert with typeurl

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby
2017-07-05 16:05:49 -07:00
parent a60511d5aa
commit 448dc0dfa8
9 changed files with 122 additions and 195 deletions

View File

@@ -3,15 +3,16 @@ package typeurl
import (
"encoding/json"
"errors"
"path/filepath"
"path"
"reflect"
"strings"
"sync"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
)
const Namespace = "types.containerd.io"
const Prefix = "types.containerd.io"
var (
mu sync.Mutex
@@ -28,16 +29,21 @@ func Register(v interface{}, args ...string) {
if _, ok := registry[t]; ok {
panic(ErrRegistered)
}
registry[t] = filepath.Join(append([]string{Namespace}, args...)...)
registry[t] = path.Join(append([]string{Prefix}, args...)...)
}
// TypeUrl returns the type url for a registred type
func TypeUrl(v interface{}) (string, error) {
// 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)]
if !ok {
return "", ErrNotExists
// fallback to the proto registry if it is a proto message
pb, ok := v.(proto.Message)
if !ok {
return "", ErrNotExists
}
return path.Join(Prefix, proto.MessageName(pb)), nil
}
return u, nil
}
@@ -47,7 +53,7 @@ func MarshalAny(v interface{}) (*types.Any, error) {
err error
data []byte
)
url, err := TypeUrl(v)
url, err := TypeURL(v)
if err != nil {
return nil, err
}
@@ -71,23 +77,38 @@ func UnmarshalAny(any *types.Any) (interface{}, error) {
if err != nil {
return nil, err
}
v := reflect.New(t).Interface()
switch dt := v.(type) {
case proto.Message:
err = proto.Unmarshal(any.Value, dt)
default:
v := reflect.New(t.t).Interface()
if t.isProto {
err = proto.Unmarshal(any.Value, v.(proto.Message))
} else {
err = json.Unmarshal(any.Value, v)
}
return v, err
}
func getTypeByUrl(url string) (reflect.Type, error) {
type urlType struct {
t reflect.Type
isProto bool
}
func getTypeByUrl(url string) (urlType, error) {
for t, u := range registry {
if u == url {
return t, nil
return urlType{
t: t,
}, nil
}
}
return nil, ErrNotExists
// fallback to proto registry
t := proto.MessageType(strings.TrimPrefix(url, Prefix+"/"))
if t != nil {
return urlType{
// get the underlying Elem because proto returns a pointer to the type
t: t.Elem(),
isProto: true,
}, nil
}
return urlType{}, ErrNotExists
}
func tryDereference(v interface{}) reflect.Type {

View File

@@ -1,9 +1,13 @@
package typeurl
import (
"fmt"
"path/filepath"
"reflect"
"testing"
eventsapi "github.com/containerd/containerd/api/services/events/v1"
events "github.com/containerd/containerd/events"
)
type test struct {
@@ -17,10 +21,10 @@ func clear() {
func TestRegsiterValueGetValue(t *testing.T) {
clear()
expected := filepath.Join(Namespace, "test")
expected := filepath.Join(Prefix, "test")
Register(test{}, "test")
url, err := TypeUrl(test{})
url, err := TypeURL(test{})
if err != nil {
t.Fatal(err)
}
@@ -31,10 +35,10 @@ func TestRegsiterValueGetValue(t *testing.T) {
func TestRegsiterValueGetPointer(t *testing.T) {
clear()
expected := filepath.Join(Namespace, "test")
expected := filepath.Join(Prefix, "test")
Register(test{}, "test")
url, err := TypeUrl(&test{})
url, err := TypeURL(&test{})
if err != nil {
t.Fatal(err)
}
@@ -45,10 +49,10 @@ func TestRegsiterValueGetPointer(t *testing.T) {
func TestRegsiterPointerGetPointer(t *testing.T) {
clear()
expected := filepath.Join(Namespace, "test")
expected := filepath.Join(Prefix, "test")
Register(&test{}, "test")
url, err := TypeUrl(&test{})
url, err := TypeURL(&test{})
if err != nil {
t.Fatal(err)
}
@@ -59,10 +63,10 @@ func TestRegsiterPointerGetPointer(t *testing.T) {
func TestRegsiterPointerGetValue(t *testing.T) {
clear()
expected := filepath.Join(Namespace, "test")
expected := filepath.Join(Prefix, "test")
Register(&test{}, "test")
url, err := TypeUrl(test{})
url, err := TypeURL(test{})
if err != nil {
t.Fatal(err)
}
@@ -73,7 +77,7 @@ func TestRegsiterPointerGetValue(t *testing.T) {
func TestMarshal(t *testing.T) {
clear()
expected := filepath.Join(Namespace, "test")
expected := filepath.Join(Prefix, "test")
Register(test{}, "test")
v := &test{
@@ -116,3 +120,55 @@ func TestMarshalUnmarshal(t *testing.T) {
t.Fatal("invalid age")
}
}
func TestMarshalEvent(t *testing.T) {
for _, testcase := range []struct {
event events.Event
url string
}{
{
event: &eventsapi.TaskStart{},
url: "types.containerd.io/containerd.services.events.v1.TaskStart",
},
{
event: &eventsapi.NamespaceUpdate{},
url: "types.containerd.io/containerd.services.events.v1.NamespaceUpdate",
},
} {
t.Run(fmt.Sprintf("%T", testcase.event), func(t *testing.T) {
a, err := MarshalAny(testcase.event)
if err != nil {
t.Fatal(err)
}
if a.TypeUrl != testcase.url {
t.Fatalf("unexpected url: %v != %v", a.TypeUrl, testcase.url)
}
v, err := UnmarshalAny(a)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(v, testcase.event) {
t.Fatalf("round trip failed %v != %v", v, testcase.event)
}
})
}
}
func BenchmarkMarshalEvent(b *testing.B) {
ev := &eventsapi.TaskStart{}
expected, err := MarshalAny(ev)
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
a, err := MarshalAny(ev)
if err != nil {
b.Fatal(err)
}
if a.TypeUrl != expected.TypeUrl {
b.Fatalf("incorrect type url: %v != %v", a, expected)
}
}
}