Skip to content

Commit

Permalink
Move conversion.Scheme to runtime
Browse files Browse the repository at this point in the history
There is only one type registry to rule them all
  • Loading branch information
smarterclayton committed Jan 30, 2016
1 parent 0e889a1 commit 60b6666
Show file tree
Hide file tree
Showing 18 changed files with 827 additions and 1,050 deletions.
2 changes: 1 addition & 1 deletion cmd/genconversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func main() {
}

versionPath := pkgPath(gv.Group, gv.Version)
generator := kruntime.NewConversionGenerator(api.Scheme.Raw(), versionPath)
generator := kruntime.NewConversionGenerator(api.Scheme, versionPath)
apiShort := generator.AddImport(path.Join(pkgBase, "api"))
generator.AddImport(path.Join(pkgBase, "api/resource"))
// TODO(wojtek-t): Change the overwrites to a flag.
Expand Down
2 changes: 1 addition & 1 deletion cmd/gendeepcopy/deep_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func main() {
}

versionPath := pkgPath(gv.Group, gv.Version)
generator := kruntime.NewDeepCopyGenerator(api.Scheme.Raw(), versionPath, sets.NewString("k8s.io/kubernetes"))
generator := kruntime.NewDeepCopyGenerator(api.Scheme, versionPath, sets.NewString("k8s.io/kubernetes"))
generator.AddImport(path.Join(pkgBase, "api"))

if len(*overwrites) > 0 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func BenchmarkPodConversion(b *testing.B) {
b.Fatalf("Unexpected error decoding pod: %v", err)
}

scheme := api.Scheme.Raw()
scheme := api.Scheme
var result *api.Pod
for i := 0; i < b.N; i++ {
versionedObj, err := scheme.ConvertToVersion(&pod, testapi.Default.GroupVersion().String())
Expand Down Expand Up @@ -63,7 +63,7 @@ func BenchmarkNodeConversion(b *testing.B) {
b.Fatalf("Unexpected error decoding node: %v", err)
}

scheme := api.Scheme.Raw()
scheme := api.Scheme
var result *api.Node
for i := 0; i < b.N; i++ {
versionedObj, err := scheme.ConvertToVersion(&node, testapi.Default.GroupVersion().String())
Expand Down Expand Up @@ -91,7 +91,7 @@ func BenchmarkReplicationControllerConversion(b *testing.B) {
b.Fatalf("Unexpected error decoding node: %v", err)
}

scheme := api.Scheme.Raw()
scheme := api.Scheme
var result *api.ReplicationController
for i := 0; i < b.N; i++ {
versionedObj, err := scheme.ConvertToVersion(&replicationController, testapi.Default.GroupVersion().String())
Expand Down
15 changes: 13 additions & 2 deletions pkg/conversion/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ type DebugLogger interface {
Logf(format string, args ...interface{})
}

type NameFunc func(t reflect.Type) string

var DefaultNameFunc = func(t reflect.Type) string { return t.Name() }

// Converter knows how to convert one type to another.
type Converter struct {
// Map from the conversion pair to a function which can
Expand Down Expand Up @@ -77,14 +81,14 @@ type Converter struct {
}

// NewConverter creates a new Converter object.
func NewConverter() *Converter {
func NewConverter(nameFn NameFunc) *Converter {
c := &Converter{
conversionFuncs: NewConversionFuncs(),
generatedConversionFuncs: NewConversionFuncs(),
ignoredConversions: make(map[typePair]struct{}),
defaultingFuncs: make(map[reflect.Type]reflect.Value),
defaultingInterfaces: make(map[reflect.Type]interface{}),
nameFunc: func(t reflect.Type) string { return t.Name() },
nameFunc: nameFn,
structFieldDests: make(map[typeNamePair][]typeNamePair),
structFieldSources: make(map[typeNamePair][]typeNamePair),

Expand All @@ -103,6 +107,13 @@ func (c *Converter) WithConversions(fns ConversionFuncs) *Converter {
return &copied
}

// DefaultMeta returns the conversion FieldMappingFunc and meta for a given type.
func (c *Converter) DefaultMeta(t reflect.Type) (FieldMatchingFlags, *Meta) {
return c.inputDefaultFlags[t], &Meta{
KeyNameMapping: c.inputFieldMappingFuncs[t],
}
}

// ByteSliceCopy prevents recursing into every byte
func ByteSliceCopy(in *[]byte, out *[]byte, s Scope) error {
*out = make([]byte, len(*in))
Expand Down
Loading

0 comments on commit 60b6666

Please sign in to comment.