Introduce ObjectConvertor for conversion to known API versions

Will allow clients to transform internal objects to a version
suitable for external output.
This commit is contained in:
Clayton Coleman
2014-11-01 18:38:02 -04:00
parent 2d54dfe249
commit 778a50d00b
4 changed files with 102 additions and 0 deletions

View File

@@ -240,6 +240,45 @@ func TestMultipleNames(t *testing.T) {
}
}
func TestKnownTypes(t *testing.T) {
s := GetTestScheme()
if len(s.KnownTypes("v2")) != 0 {
t.Errorf("should have no known types for v2")
}
types := s.KnownTypes("v1")
for _, s := range []string{"TestType1", "TestType2", "TestType3", "ExternalInternalSame"} {
if _, ok := types[s]; !ok {
t.Errorf("missing type %q", s)
}
}
}
func TestConvertToVersion(t *testing.T) {
s := GetTestScheme()
tt := &TestType1{A: "I'm not a pointer object"}
other, err := s.ConvertToVersion(tt, "v1")
if err != nil {
t.Fatalf("Failure: %v", err)
}
converted, ok := other.(*ExternalTestType1)
if !ok {
t.Fatalf("Got wrong type")
}
if tt.A != converted.A {
t.Fatalf("Failed to convert object correctly: %#v", converted)
}
}
func TestConvertToVersionErr(t *testing.T) {
s := GetTestScheme()
tt := TestType1{A: "I'm not a pointer object"}
_, err := s.ConvertToVersion(tt, "v1")
if err == nil {
t.Fatalf("unexpected non-error")
}
}
func TestEncode_NonPtr(t *testing.T) {
s := GetTestScheme()
tt := TestType1{A: "I'm not a pointer object"}