diff --git a/staging/src/k8s.io/code-generator/cmd/conversion-gen/generators/conversion.go b/staging/src/k8s.io/code-generator/cmd/conversion-gen/generators/conversion.go index e725b5cfd30..1e7fb794e95 100644 --- a/staging/src/k8s.io/code-generator/cmd/conversion-gen/generators/conversion.go +++ b/staging/src/k8s.io/code-generator/cmd/conversion-gen/generators/conversion.go @@ -812,17 +812,23 @@ func (g *genConversion) doMap(inType, outType *types.Type, sw *generator.Snippet sw.Do("$.|raw$(val)\n", outType.Elem) } } else { - sw.Do("newVal := new($.|raw$)\n", outType.Elem) + conversionExists := true if function, ok := g.preexists(inType.Elem, outType.Elem); ok { + sw.Do("newVal := new($.|raw$)\n", outType.Elem) sw.Do("if err := $.|raw$(&val, newVal, s); err != nil {\n", function) } else if g.convertibleOnlyWithinPackage(inType.Elem, outType.Elem) { + sw.Do("newVal := new($.|raw$)\n", outType.Elem) sw.Do("if err := "+nameTmpl+"(&val, newVal, s); err != nil {\n", argsFromType(inType.Elem, outType.Elem)) } else { - sw.Do("// TODO: Inefficient conversion - can we improve it?\n", nil) - sw.Do("if err := s.Convert(&val, newVal, 0); err != nil {\n", nil) + args := argsFromType(inType.Elem, outType.Elem) + sw.Do("// FIXME: Provide conversion function to convert $.inType|raw$ to $.outType|raw$\n", args) + sw.Do("compileErrorOnMissingConversion()\n", nil) + conversionExists = false + } + if conversionExists { + sw.Do("return err\n", nil) + sw.Do("}\n", nil) } - sw.Do("return err\n", nil) - sw.Do("}\n", nil) if inType.Key == outType.Key { sw.Do("(*out)[key] = *newVal\n", nil) } else { @@ -850,21 +856,21 @@ func (g *genConversion) doSlice(inType, outType *types.Type, sw *generator.Snipp sw.Do("(*out)[i] = $.|raw$((*in)[i])\n", outType.Elem) } } else { + conversionExists := true if function, ok := g.preexists(inType.Elem, outType.Elem); ok { sw.Do("if err := $.|raw$(&(*in)[i], &(*out)[i], s); err != nil {\n", function) } else if g.convertibleOnlyWithinPackage(inType.Elem, outType.Elem) { sw.Do("if err := "+nameTmpl+"(&(*in)[i], &(*out)[i], s); err != nil {\n", argsFromType(inType.Elem, outType.Elem)) } else { - // TODO: This triggers on metav1.ObjectMeta <-> metav1.ObjectMeta and - // similar because neither package is the target package, and - // we really don't know which package will have the conversion - // function defined. This fires on basically every object - // conversion outside of pkg/api/v1. - sw.Do("// TODO: Inefficient conversion - can we improve it?\n", nil) - sw.Do("if err := s.Convert(&(*in)[i], &(*out)[i], 0); err != nil {\n", nil) + args := argsFromType(inType.Elem, outType.Elem) + sw.Do("// FIXME: Provide conversion function to convert $.inType|raw$ to $.outType|raw$\n", args) + sw.Do("compileErrorOnMissingConversion()\n", nil) + conversionExists = false + } + if conversionExists { + sw.Do("return err\n", nil) + sw.Do("}\n", nil) } - sw.Do("return err\n", nil) - sw.Do("}\n", nil) } sw.Do("}\n", nil) } @@ -971,14 +977,19 @@ func (g *genConversion) doStruct(inType, outType *types.Type, sw *generator.Snip sw.Do("out.$.name$ = in.$.name$\n", args) continue } + conversionExists := true if g.convertibleOnlyWithinPackage(inMemberType, outMemberType) { sw.Do("if err := "+nameTmpl+"(&in.$.name$, &out.$.name$, s); err != nil {\n", args) } else { - sw.Do("// TODO: Inefficient conversion - can we improve it?\n", nil) - sw.Do("if err := s.Convert(&in.$.name$, &out.$.name$, 0); err != nil {\n", args) + args := argsFromType(inMemberType, outMemberType) + sw.Do("// FIXME: Provide conversion function to convert $.inType|raw$ to $.outType|raw$\n", args) + sw.Do("compileErrorOnMissingConversion()\n", nil) + conversionExists = false + } + if conversionExists { + sw.Do("return err\n", nil) + sw.Do("}\n", nil) } - sw.Do("return err\n", nil) - sw.Do("}\n", nil) case types.Alias: if isDirectlyAssignable(inMemberType, outMemberType) { if inMemberType == outMemberType { @@ -987,24 +998,34 @@ func (g *genConversion) doStruct(inType, outType *types.Type, sw *generator.Snip sw.Do("out.$.name$ = $.outType|raw$(in.$.name$)\n", args) } } else { + conversionExists := true if g.convertibleOnlyWithinPackage(inMemberType, outMemberType) { sw.Do("if err := "+nameTmpl+"(&in.$.name$, &out.$.name$, s); err != nil {\n", args) } else { - sw.Do("// TODO: Inefficient conversion - can we improve it?\n", nil) - sw.Do("if err := s.Convert(&in.$.name$, &out.$.name$, 0); err != nil {\n", args) + args := argsFromType(inMemberType, outMemberType) + sw.Do("// FIXME: Provide conversion function to convert $.inType|raw$ to $.outType|raw$\n", args) + sw.Do("compileErrorOnMissingConversion()\n", nil) + conversionExists = false + } + if conversionExists { + sw.Do("return err\n", nil) + sw.Do("}\n", nil) } - sw.Do("return err\n", nil) - sw.Do("}\n", nil) } default: + conversionExists := true if g.convertibleOnlyWithinPackage(inMemberType, outMemberType) { sw.Do("if err := "+nameTmpl+"(&in.$.name$, &out.$.name$, s); err != nil {\n", args) } else { - sw.Do("// TODO: Inefficient conversion - can we improve it?\n", nil) - sw.Do("if err := s.Convert(&in.$.name$, &out.$.name$, 0); err != nil {\n", args) + args := argsFromType(inMemberType, outMemberType) + sw.Do("// FIXME: Provide conversion function to convert $.inType|raw$ to $.outType|raw$\n", args) + sw.Do("compileErrorOnMissingConversion()\n", nil) + conversionExists = false + } + if conversionExists { + sw.Do("return err\n", nil) + sw.Do("}\n", nil) } - sw.Do("return err\n", nil) - sw.Do("}\n", nil) } } } @@ -1033,16 +1054,21 @@ func (g *genConversion) doPointer(inType, outType *types.Type, sw *generator.Sni sw.Do("**out = $.|raw$(**in)\n", outType.Elem) } } else { + conversionExists := true if function, ok := g.preexists(inType.Elem, outType.Elem); ok { sw.Do("if err := $.|raw$(*in, *out, s); err != nil {\n", function) } else if g.convertibleOnlyWithinPackage(inType.Elem, outType.Elem) { sw.Do("if err := "+nameTmpl+"(*in, *out, s); err != nil {\n", argsFromType(inType.Elem, outType.Elem)) } else { - sw.Do("// TODO: Inefficient conversion - can we improve it?\n", nil) - sw.Do("if err := s.Convert(*in, *out, 0); err != nil {\n", nil) + args := argsFromType(inType.Elem, outType.Elem) + sw.Do("// FIXME: Provide conversion function to convert $.inType|raw$ to $.outType|raw$\n", args) + sw.Do("compileErrorOnMissingConversion()\n", nil) + conversionExists = false + } + if conversionExists { + sw.Do("return err\n", nil) + sw.Do("}\n", nil) } - sw.Do("return err\n", nil) - sw.Do("}\n", nil) } }