mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
Change conversion package so that AddKnownTypes takes pointers.
This commit is contained in:
parent
2ba6503511
commit
0c6adde92e
@ -93,7 +93,7 @@ func TestConverter_CallsRegisteredFunctions(t *testing.T) {
|
|||||||
|
|
||||||
func TestConverter_fuzz(t *testing.T) {
|
func TestConverter_fuzz(t *testing.T) {
|
||||||
newAnonType := func() interface{} {
|
newAnonType := func() interface{} {
|
||||||
return reflect.New(reflect.TypeOf(externalTypeReturn())).Interface()
|
return reflect.New(reflect.TypeOf(externalTypeReturn()).Elem()).Interface()
|
||||||
}
|
}
|
||||||
// Use the same types from the scheme test.
|
// Use the same types from the scheme test.
|
||||||
table := []struct {
|
table := []struct {
|
||||||
|
@ -85,8 +85,8 @@ func NewScheme() *Scheme {
|
|||||||
|
|
||||||
// AddKnownTypes registers all types passed in 'types' as being members of version 'version.
|
// AddKnownTypes registers all types passed in 'types' as being members of version 'version.
|
||||||
// Encode() will refuse objects unless their type has been registered with AddKnownTypes.
|
// Encode() will refuse objects unless their type has been registered with AddKnownTypes.
|
||||||
// All objects passed to types should be structs, not pointers to structs. The name that go
|
// All objects passed to types should be pointers to structs. The name that go reports for
|
||||||
// reports for the struct becomes the "kind" field when encoding.
|
// the struct becomes the "kind" field when encoding.
|
||||||
func (s *Scheme) AddKnownTypes(version string, types ...interface{}) {
|
func (s *Scheme) AddKnownTypes(version string, types ...interface{}) {
|
||||||
knownTypes, found := s.versionMap[version]
|
knownTypes, found := s.versionMap[version]
|
||||||
if !found {
|
if !found {
|
||||||
@ -95,8 +95,12 @@ func (s *Scheme) AddKnownTypes(version string, types ...interface{}) {
|
|||||||
}
|
}
|
||||||
for _, obj := range types {
|
for _, obj := range types {
|
||||||
t := reflect.TypeOf(obj)
|
t := reflect.TypeOf(obj)
|
||||||
|
if t.Kind() != reflect.Ptr {
|
||||||
|
panic("All types must be pointers to structs.")
|
||||||
|
}
|
||||||
|
t = t.Elem()
|
||||||
if t.Kind() != reflect.Struct {
|
if t.Kind() != reflect.Struct {
|
||||||
panic("All types must be structs.")
|
panic("All types must be pointers to structs.")
|
||||||
}
|
}
|
||||||
knownTypes[t.Name()] = t
|
knownTypes[t.Name()] = t
|
||||||
s.typeToVersion[t] = version
|
s.typeToVersion[t] = version
|
||||||
|
@ -90,7 +90,7 @@ func externalTypeReturn() interface{} {
|
|||||||
O *TestType2 `yaml:"O,omitempty" json:"O,omitempty"`
|
O *TestType2 `yaml:"O,omitempty" json:"O,omitempty"`
|
||||||
P []TestType2 `yaml:"Q,omitempty" json:"Q,omitempty"`
|
P []TestType2 `yaml:"Q,omitempty" json:"Q,omitempty"`
|
||||||
}
|
}
|
||||||
return TestType1{}
|
return &TestType1{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExternalInternalSame struct {
|
type ExternalInternalSame struct {
|
||||||
@ -124,8 +124,8 @@ var TestObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 100).Funcs(
|
|||||||
// Returns a new Scheme set up with the test objects.
|
// Returns a new Scheme set up with the test objects.
|
||||||
func GetTestScheme() *Scheme {
|
func GetTestScheme() *Scheme {
|
||||||
s := NewScheme()
|
s := NewScheme()
|
||||||
s.AddKnownTypes("", TestType1{}, ExternalInternalSame{})
|
s.AddKnownTypes("", &TestType1{}, &ExternalInternalSame{})
|
||||||
s.AddKnownTypes("v1", externalTypeReturn(), ExternalInternalSame{})
|
s.AddKnownTypes("v1", externalTypeReturn(), &ExternalInternalSame{})
|
||||||
s.ExternalVersion = "v1"
|
s.ExternalVersion = "v1"
|
||||||
s.InternalVersion = ""
|
s.InternalVersion = ""
|
||||||
s.MetaInsertionFactory = testMetaInsertionFactory{}
|
s.MetaInsertionFactory = testMetaInsertionFactory{}
|
||||||
|
Loading…
Reference in New Issue
Block a user