update the applyconfiguration-gen flag external-applyconfigurations to work

This commit is contained in:
David Eads 2022-08-19 12:04:28 -04:00
parent 69a723e898
commit 59df3248ed

View File

@ -28,7 +28,6 @@ import (
type externalApplyConfigurationValue struct { type externalApplyConfigurationValue struct {
externals *map[types.Name]string externals *map[types.Name]string
changed bool
} }
func NewExternalApplyConfigurationValue(externals *map[types.Name]string, def []string) *externalApplyConfigurationValue { func NewExternalApplyConfigurationValue(externals *map[types.Name]string, def []string) *externalApplyConfigurationValue {
@ -45,10 +44,6 @@ func NewExternalApplyConfigurationValue(externals *map[types.Name]string, def []
var _ flag.Value = &externalApplyConfigurationValue{} var _ flag.Value = &externalApplyConfigurationValue{}
func (s *externalApplyConfigurationValue) set(vs []string) error { func (s *externalApplyConfigurationValue) set(vs []string) error {
if !s.changed {
*s.externals = map[types.Name]string{}
}
for _, input := range vs { for _, input := range vs {
typ, pkg, err := parseExternalMapping(input) typ, pkg, err := parseExternalMapping(input)
if err != nil { if err != nil {
@ -71,6 +66,7 @@ func (s *externalApplyConfigurationValue) Set(val string) error {
if err := s.set(vs); err != nil { if err := s.set(vs); err != nil {
return err return err
} }
return nil return nil
} }
@ -114,12 +110,13 @@ func parseExternalMapping(mapping string) (typ types.Name, pkg string, err error
} }
packageTypeStr := parts[0] packageTypeStr := parts[0]
pkg = parts[1] pkg = parts[1]
ptParts := strings.Split(packageTypeStr, ".") // need to split on the *last* dot, since k8s.io (and other valid packages) have a dot in it
if len(ptParts) != 2 { lastDot := strings.LastIndex(packageTypeStr, ".")
return types.Name{}, "", fmt.Errorf("expected package and type of the form <package>#<typeName> but got %s", packageTypeStr) if lastDot == -1 || lastDot == len(packageTypeStr)-1 {
return types.Name{}, "", fmt.Errorf("expected package and type of the form <package>.<typeName> but got %s", packageTypeStr)
} }
structPkg := ptParts[0] structPkg := packageTypeStr[:lastDot]
structType := ptParts[1] structType := packageTypeStr[lastDot+1:]
return types.Name{Package: structPkg, Name: structType}, pkg, nil return types.Name{Package: structPkg, Name: structType}, pkg, nil
} }