Skip to content

Commit

Permalink
Small improvements in conversion generator
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtek-t committed Apr 5, 2016
1 parent 5470488 commit 49d3c39
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 45 deletions.
1 change: 1 addition & 0 deletions cmd/libs/go2idl/conversion-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func main() {
arguments.InputDirs = []string{
"k8s.io/kubernetes/pkg/api/v1",
"k8s.io/kubernetes/pkg/api",
"k8s.io/kubernetes/pkg/conversion",
"k8s.io/kubernetes/pkg/runtime",
}

Expand Down
22 changes: 11 additions & 11 deletions pkg/api/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ func init() {
Convert_unversioned_ListMeta_To_unversioned_ListMeta,
Convert_intstr_IntOrString_To_intstr_IntOrString,
Convert_unversioned_Time_To_unversioned_Time,
Convert_string_slice_To_unversioned_Time,
Convert_Slice_string_To_unversioned_Time,
Convert_string_To_labels_Selector,
Convert_string_To_fields_Selector,
Convert_bool_ref_To_bool,
Convert_bool_To_bool_ref,
Convert_string_ref_To_string,
Convert_string_To_string_ref,
Convert_Pointer_bool_To_bool,
Convert_bool_To_Pointer_bool,
Convert_Pointer_string_To_string,
Convert_string_To_Pointer_string,
Convert_labels_Selector_To_string,
Convert_fields_Selector_To_string,
Convert_resource_Quantity_To_resource_Quantity,
)
}

func Convert_string_ref_To_string(in **string, out *string, s conversion.Scope) error {
func Convert_Pointer_string_To_string(in **string, out *string, s conversion.Scope) error {
if *in == nil {
*out = ""
return nil
Expand All @@ -63,7 +63,7 @@ func Convert_string_ref_To_string(in **string, out *string, s conversion.Scope)
return nil
}

func Convert_string_To_string_ref(in *string, out **string, s conversion.Scope) error {
func Convert_string_To_Pointer_string(in *string, out **string, s conversion.Scope) error {
if in == nil {
stringVar := ""
*out = &stringVar
Expand All @@ -73,7 +73,7 @@ func Convert_string_To_string_ref(in *string, out **string, s conversion.Scope)
return nil
}

func Convert_bool_ref_To_bool(in **bool, out *bool, s conversion.Scope) error {
func Convert_Pointer_bool_To_bool(in **bool, out *bool, s conversion.Scope) error {
if *in == nil {
*out = false
return nil
Expand All @@ -82,7 +82,7 @@ func Convert_bool_ref_To_bool(in **bool, out *bool, s conversion.Scope) error {
return nil
}

func Convert_bool_To_bool_ref(in *bool, out **bool, s conversion.Scope) error {
func Convert_bool_To_Pointer_bool(in *bool, out **bool, s conversion.Scope) error {
if in == nil {
boolVar := false
*out = &boolVar
Expand Down Expand Up @@ -118,8 +118,8 @@ func Convert_unversioned_Time_To_unversioned_Time(in *unversioned.Time, out *unv
return nil
}

// Convert_string_slice_To_unversioned_Time allows converting a URL query parameter value
func Convert_string_slice_To_unversioned_Time(input *[]string, out *unversioned.Time, s conversion.Scope) error {
// Convert_Slice_string_To_unversioned_Time allows converting a URL query parameter value
func Convert_Slice_string_To_unversioned_Time(input *[]string, out *unversioned.Time, s conversion.Scope) error {
str := ""
if len(*input) > 0 {
str = (*input)[0]
Expand Down
22 changes: 6 additions & 16 deletions pkg/api/v1/conversion_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -5805,12 +5805,8 @@ func autoConvert_v1_RangeAllocation_To_api_RangeAllocation(in *RangeAllocation,
return err
}
out.Range = in.Range
if in.Data != nil {
in, out := &in.Data, &out.Data
*out = make([]byte, len(*in))
copy(*out, *in)
} else {
out.Data = nil
if err := conversion.Convert_Slice_byte_To_Slice_byte(&in.Data, &out.Data, s); err != nil {
return err
}
return nil
}
Expand All @@ -5830,12 +5826,8 @@ func autoConvert_api_RangeAllocation_To_v1_RangeAllocation(in *api.RangeAllocati
return err
}
out.Range = in.Range
if in.Data != nil {
in, out := &in.Data, &out.Data
*out = make([]byte, len(*in))
copy(*out, *in)
} else {
out.Data = nil
if err := conversion.Convert_Slice_byte_To_Slice_byte(&in.Data, &out.Data, s); err != nil {
return err
}
return nil
}
Expand Down Expand Up @@ -6285,8 +6277,7 @@ func autoConvert_v1_Secret_To_api_Secret(in *Secret, out *api.Secret, s conversi
*out = make(map[string][]byte, len(*in))
for key, val := range *in {
newVal := new([]byte)
// TODO: Inefficient conversion - can we improve it?
if err := s.Convert(&val, newVal, 0); err != nil {
if err := conversion.Convert_Slice_byte_To_Slice_byte(&val, newVal, s); err != nil {
return err
}
(*out)[key] = *newVal
Expand Down Expand Up @@ -6317,8 +6308,7 @@ func autoConvert_api_Secret_To_v1_Secret(in *api.Secret, out *Secret, s conversi
*out = make(map[string][]byte, len(*in))
for key, val := range *in {
newVal := new([]byte)
// TODO: Inefficient conversion - can we improve it?
if err := s.Convert(&val, newVal, 0); err != nil {
if err := conversion.Convert_Slice_byte_To_Slice_byte(&val, newVal, s); err != nil {
return err
}
(*out)[key] = *newVal
Expand Down
8 changes: 4 additions & 4 deletions pkg/apis/componentconfig/v1alpha1/conversion_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func autoConvert_componentconfig_KubeSchedulerConfiguration_To_v1alpha1_KubeSche
out.Address = in.Address
out.AlgorithmProvider = in.AlgorithmProvider
out.PolicyConfigFile = in.PolicyConfigFile
if err := api.Convert_bool_To_bool_ref(&in.EnableProfiling, &out.EnableProfiling, s); err != nil {
if err := api.Convert_bool_To_Pointer_bool(&in.EnableProfiling, &out.EnableProfiling, s); err != nil {
return err
}
out.KubeAPIQPS = in.KubeAPIQPS
Expand All @@ -103,7 +103,7 @@ func autoConvert_componentconfig_LeaderElectionConfiguration_To_v1alpha1_LeaderE
if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found {
defaulting.(func(*componentconfig.LeaderElectionConfiguration))(in)
}
if err := api.Convert_bool_To_bool_ref(&in.LeaderElect, &out.LeaderElect, s); err != nil {
if err := api.Convert_bool_To_Pointer_bool(&in.LeaderElect, &out.LeaderElect, s); err != nil {
return err
}
if err := s.Convert(&in.LeaseDuration, &out.LeaseDuration, 0); err != nil {
Expand Down Expand Up @@ -179,7 +179,7 @@ func autoConvert_v1alpha1_KubeSchedulerConfiguration_To_componentconfig_KubeSche
out.Address = in.Address
out.AlgorithmProvider = in.AlgorithmProvider
out.PolicyConfigFile = in.PolicyConfigFile
if err := api.Convert_bool_ref_To_bool(&in.EnableProfiling, &out.EnableProfiling, s); err != nil {
if err := api.Convert_Pointer_bool_To_bool(&in.EnableProfiling, &out.EnableProfiling, s); err != nil {
return err
}
out.KubeAPIQPS = in.KubeAPIQPS
Expand All @@ -199,7 +199,7 @@ func autoConvert_v1alpha1_LeaderElectionConfiguration_To_componentconfig_LeaderE
if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found {
defaulting.(func(*LeaderElectionConfiguration))(in)
}
if err := api.Convert_bool_ref_To_bool(&in.LeaderElect, &out.LeaderElect, s); err != nil {
if err := api.Convert_Pointer_bool_To_bool(&in.LeaderElect, &out.LeaderElect, s); err != nil {
return err
}
if err := s.Convert(&in.LeaseDuration, &out.LeaseDuration, 0); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/extensions/v1beta1/conversion_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -3797,7 +3797,7 @@ func autoConvert_extensions_ThirdPartyResourceData_To_v1beta1_ThirdPartyResource
if err := Convert_api_ObjectMeta_To_v1_ObjectMeta(&in.ObjectMeta, &out.ObjectMeta, s); err != nil {
return err
}
if err := conversion.ByteSliceCopy(&in.Data, &out.Data, s); err != nil {
if err := conversion.Convert_Slice_byte_To_Slice_byte(&in.Data, &out.Data, s); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -5037,7 +5037,7 @@ func autoConvert_v1beta1_ThirdPartyResourceData_To_extensions_ThirdPartyResource
if err := Convert_v1_ObjectMeta_To_api_ObjectMeta(&in.ObjectMeta, &out.ObjectMeta, s); err != nil {
return err
}
if err := conversion.ByteSliceCopy(&in.Data, &out.Data, s); err != nil {
if err := conversion.Convert_Slice_byte_To_Slice_byte(&in.Data, &out.Data, s); err != nil {
return err
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/conversion/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func NewConverter(nameFn NameFunc) *Converter {
inputFieldMappingFuncs: make(map[reflect.Type]FieldMappingFunc),
inputDefaultFlags: make(map[reflect.Type]FieldMatchingFlags),
}
c.RegisterConversionFunc(ByteSliceCopy)
c.RegisterConversionFunc(Convert_Slice_byte_To_Slice_byte)
return c
}

Expand All @@ -114,8 +114,8 @@ func (c *Converter) DefaultMeta(t reflect.Type) (FieldMatchingFlags, *Meta) {
}
}

// ByteSliceCopy prevents recursing into every byte
func ByteSliceCopy(in *[]byte, out *[]byte, s Scope) error {
// Convert_Slice_byte_To_Slice_byte prevents recursing into every byte
func Convert_Slice_byte_To_Slice_byte(in *[]byte, out *[]byte, s Scope) error {
*out = make([]byte, len(*in))
copy(*out, *in)
return nil
Expand Down
18 changes: 9 additions & 9 deletions pkg/runtime/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ func JSONKeyMapper(key string, sourceTag, destTag reflect.StructTag) (string, st

// DefaultStringConversions are helpers for converting []string and string to real values.
var DefaultStringConversions = []interface{}{
convertStringSliceToString,
convertStringSliceToInt,
convertStringSliceToBool,
convertStringSliceToInt64,
Convert_Slice_string_To_string,
Convert_Slice_string_To_int,
Convert_Slice_string_To_bool,
Convert_Slice_string_To_int64,
}

func convertStringSliceToString(input *[]string, out *string, s conversion.Scope) error {
func Convert_Slice_string_To_string(input *[]string, out *string, s conversion.Scope) error {
if len(*input) == 0 {
*out = ""
}
*out = (*input)[0]
return nil
}

func convertStringSliceToInt(input *[]string, out *int, s conversion.Scope) error {
func Convert_Slice_string_To_int(input *[]string, out *int, s conversion.Scope) error {
if len(*input) == 0 {
*out = 0
}
Expand All @@ -67,10 +67,10 @@ func convertStringSliceToInt(input *[]string, out *int, s conversion.Scope) erro
return nil
}

// converStringSliceToBool will convert a string parameter to boolean.
// Conver_Slice_string_To_bool will convert a string parameter to boolean.
// Only the absence of a value, a value of "false", or a value of "0" resolve to false.
// Any other value (including empty string) resolves to true.
func convertStringSliceToBool(input *[]string, out *bool, s conversion.Scope) error {
func Convert_Slice_string_To_bool(input *[]string, out *bool, s conversion.Scope) error {
if len(*input) == 0 {
*out = false
return nil
Expand All @@ -84,7 +84,7 @@ func convertStringSliceToBool(input *[]string, out *bool, s conversion.Scope) er
return nil
}

func convertStringSliceToInt64(input *[]string, out *int64, s conversion.Scope) error {
func Convert_Slice_string_To_int64(input *[]string, out *int64, s conversion.Scope) error {
if len(*input) == 0 {
*out = 0
}
Expand Down

0 comments on commit 49d3c39

Please sign in to comment.