diff --git a/registry/registry.go b/registry/registry.go index 8e323fe4e..9e8269bb4 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -17,6 +17,8 @@ limitations under the License. package registry import ( + "strings" + "github.com/kubernetes/deployment-manager/common" ) @@ -46,6 +48,26 @@ type Type struct { Version string } +// ParseType takes a registry name and parses it into a *registry.Type. +func ParseType(name string) *Type { + tt := &Type{} + + tList := strings.Split(name, ":") + if len(tList) == 2 { + tt.Version = tList[1] + } + + cList := strings.Split(tList[0], "/") + + if len(cList) == 1 { + tt.Name = tList[0] + } else { + tt.Collection = cList[0] + tt.Name = cList[1] + } + return tt +} + // Registry abstracts type interactions. type Registry interface { // List all the templates at the given path diff --git a/registry/registry_test.go b/registry/registry_test.go new file mode 100644 index 000000000..9f9ce5e06 --- /dev/null +++ b/registry/registry_test.go @@ -0,0 +1,28 @@ +package registry + +import ( + "testing" +) + +func TestParseType(t *testing.T) { + // TODO: Are there some real-world examples we want to valide here? + tests := map[string]*Type{ + "foo": &Type{Name: "foo"}, + "foo:v1": &Type{Name: "foo", Version: "v1"}, + "github.com/foo": &Type{Name: "foo", Collection: "github.com"}, + "github.com/foo:v1.2.3": &Type{Name: "foo", Collection: "github.com", Version: "v1.2.3"}, + } + + for in, expect := range tests { + out := ParseType(in) + if out.Name != expect.Name { + t.Errorf("Expected name to be %q, got %q", expect.Name, out.Name) + } + if out.Version != expect.Version { + t.Errorf("Expected version to be %q, got %q", expect.Version, out.Version) + } + if out.Collection != expect.Collection { + t.Errorf("Expected collection to be %q, got %q", expect.Collection, out.Collection) + } + } +}