mirror of https://github.com/k3s-io/k3s
Ensure public conversion name packages are imported
The name of the package a public function is in may not be inside the current package set, so it needs to be imported.pull/6/head
parent
57bc8719f9
commit
b8582f73da
|
@ -49,7 +49,7 @@ func NewConversionGenerator(scheme *Scheme, targetPkg string) ConversionGenerato
|
||||||
generatedNamePrefix: "auto",
|
generatedNamePrefix: "auto",
|
||||||
targetPkg: targetPkg,
|
targetPkg: targetPkg,
|
||||||
|
|
||||||
publicFuncs: make(map[typePair]string),
|
publicFuncs: make(map[typePair]functionName),
|
||||||
convertibles: make(map[reflect.Type]reflect.Type),
|
convertibles: make(map[reflect.Type]reflect.Type),
|
||||||
overridden: make(map[reflect.Type]bool),
|
overridden: make(map[reflect.Type]bool),
|
||||||
pkgOverwrites: make(map[string]string),
|
pkgOverwrites: make(map[string]string),
|
||||||
|
@ -64,6 +64,11 @@ func NewConversionGenerator(scheme *Scheme, targetPkg string) ConversionGenerato
|
||||||
|
|
||||||
var complexTypes []reflect.Kind = []reflect.Kind{reflect.Map, reflect.Ptr, reflect.Slice, reflect.Interface, reflect.Struct}
|
var complexTypes []reflect.Kind = []reflect.Kind{reflect.Map, reflect.Ptr, reflect.Slice, reflect.Interface, reflect.Struct}
|
||||||
|
|
||||||
|
type functionName struct {
|
||||||
|
name string
|
||||||
|
packageName string
|
||||||
|
}
|
||||||
|
|
||||||
type conversionGenerator struct {
|
type conversionGenerator struct {
|
||||||
scheme *Scheme
|
scheme *Scheme
|
||||||
|
|
||||||
|
@ -71,7 +76,7 @@ type conversionGenerator struct {
|
||||||
generatedNamePrefix string
|
generatedNamePrefix string
|
||||||
targetPkg string
|
targetPkg string
|
||||||
|
|
||||||
publicFuncs map[typePair]string
|
publicFuncs map[typePair]functionName
|
||||||
convertibles map[reflect.Type]reflect.Type
|
convertibles map[reflect.Type]reflect.Type
|
||||||
overridden map[reflect.Type]bool
|
overridden map[reflect.Type]bool
|
||||||
// If pkgOverwrites is set for a given package name, that package name
|
// If pkgOverwrites is set for a given package name, that package name
|
||||||
|
@ -246,15 +251,12 @@ func (g *conversionGenerator) rememberConversionFunction(inType, outType reflect
|
||||||
if last := strings.LastIndex(name, "."); last != -1 {
|
if last := strings.LastIndex(name, "."); last != -1 {
|
||||||
p = name[:last]
|
p = name[:last]
|
||||||
n = name[last+1:]
|
n = name[last+1:]
|
||||||
p = g.imports[p]
|
|
||||||
if len(p) > 0 {
|
|
||||||
p = p + "."
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
n = name
|
n = name
|
||||||
}
|
}
|
||||||
if isPublic(n) {
|
if isPublic(n) {
|
||||||
g.publicFuncs[typePair{inType, outType}] = p + n
|
g.AddImport(p)
|
||||||
|
g.publicFuncs[typePair{inType, outType}] = functionName{name: n, packageName: p}
|
||||||
} else {
|
} else {
|
||||||
log.Printf("WARNING: Cannot generate conversion %v -> %v, method %q is private", inType, outType, fn.Name())
|
log.Printf("WARNING: Cannot generate conversion %v -> %v, method %q is private", inType, outType, fn.Name())
|
||||||
}
|
}
|
||||||
|
@ -262,7 +264,7 @@ func (g *conversionGenerator) rememberConversionFunction(inType, outType reflect
|
||||||
log.Printf("WARNING: Cannot generate conversion %v -> %v, method is not accessible", inType, outType)
|
log.Printf("WARNING: Cannot generate conversion %v -> %v, method is not accessible", inType, outType)
|
||||||
}
|
}
|
||||||
} else if willGenerate {
|
} else if willGenerate {
|
||||||
g.publicFuncs[typePair{inType, outType}] = g.conversionFunctionName(inType, outType)
|
g.publicFuncs[typePair{inType, outType}] = functionName{name: g.conversionFunctionName(inType, outType)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -573,7 +575,15 @@ func (g *conversionGenerator) conversionFunctionName(inType, outType reflect.Typ
|
||||||
func (g *conversionGenerator) conversionFunctionCall(inType, outType reflect.Type, scopeName string, args ...string) string {
|
func (g *conversionGenerator) conversionFunctionCall(inType, outType reflect.Type, scopeName string, args ...string) string {
|
||||||
if named, ok := g.publicFuncs[typePair{inType, outType}]; ok {
|
if named, ok := g.publicFuncs[typePair{inType, outType}]; ok {
|
||||||
args[len(args)-1] = scopeName
|
args[len(args)-1] = scopeName
|
||||||
return fmt.Sprintf("%s(%s)", named, strings.Join(args, ", "))
|
name := named.name
|
||||||
|
localPackageName, ok := g.imports[named.packageName]
|
||||||
|
if !ok {
|
||||||
|
panic(fmt.Sprintf("have not defined an import for %s", named.packageName))
|
||||||
|
}
|
||||||
|
if len(named.packageName) > 0 && len(localPackageName) > 0 {
|
||||||
|
name = localPackageName + "." + name
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s(%s)", name, strings.Join(args, ", "))
|
||||||
}
|
}
|
||||||
log.Printf("WARNING: Using reflection to convert %v -> %v (no public conversion)", inType, outType)
|
log.Printf("WARNING: Using reflection to convert %v -> %v (no public conversion)", inType, outType)
|
||||||
return fmt.Sprintf("%s.Convert(%s)", scopeName, strings.Join(args, ", "))
|
return fmt.Sprintf("%s.Convert(%s)", scopeName, strings.Join(args, ", "))
|
||||||
|
|
Loading…
Reference in New Issue