Panic when registering the same type but diff urls

Panic on typeurl registration when you register the same type but
different urls for that type.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2017-07-24 15:51:09 -04:00
parent c52523c4d3
commit a3769f887b
2 changed files with 20 additions and 3 deletions

View File

@ -22,13 +22,19 @@ var (
// Register a type with the base url of the type // Register a type with the base url of the type
func Register(v interface{}, args ...string) { func Register(v interface{}, args ...string) {
t := tryDereference(v) var (
t = tryDereference(v)
p = path.Join(append([]string{Prefix}, args...)...)
)
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
if _, ok := registry[t]; ok { if et, ok := registry[t]; ok {
if et != p {
panic(errors.Errorf("type registred with alternate path %q != %q", et, p))
}
return return
} }
registry[t] = path.Join(append([]string{Prefix}, args...)...) registry[t] = p
} }
// TypeURL returns the type url for a registred type // TypeURL returns the type url for a registred type

View File

@ -130,6 +130,17 @@ func TestMarshalEvent(t *testing.T) {
} }
} }
func TestRegisterDiffUrls(t *testing.T) {
clear()
defer func() {
if err := recover(); err == nil {
t.Error("registering the same type with different urls should panic")
}
}()
Register(&test{}, "test")
Register(&test{}, "test", "two")
}
func BenchmarkMarshalEvent(b *testing.B) { func BenchmarkMarshalEvent(b *testing.B) {
ev := &eventsapi.TaskStart{} ev := &eventsapi.TaskStart{}
expected, err := MarshalAny(ev) expected, err := MarshalAny(ev)