func simplifyTypeNames(types astmodel.Types, flag astmodel.TypeFlag) (astmodel.Types, error) {
	updatedNames := make(map[astmodel.TypeName]astmodel.TypeNameSet)
	for _, def := range types {
		if flag.IsOn(def.Type()) {
			embeddedName, err := parseContextualTypeName(def.Name())
			if err != nil {
				return nil, err
			}
			updatedNames[embeddedName.original] = updatedNames[embeddedName.original].Add(def.Name())
		}
	}
	r := renamer{types: types}
	renameActions := []renameAction{
		r.simplifyEmbeddedNameToOriginalName,
		r.simplifyEmbeddedNameRemoveContextAndCount,
		r.simplifyEmbeddedNameRemoveContext,
		r.simplifyEmbeddedName,
	}
	renames := make(astmodel.TypeAssociation)
	for original, associatedNames := range updatedNames {
		for _, action := range renameActions {
			result, err := action(original, associatedNames)
			if err != nil {
				return nil, err
			}
			if result != nil {
				for oldName, newName := range result {
					renames[oldName] = newName
				}
				break
			}
		}
	}
	return r.performRenames(renames, flag)
}
simplifyTypeNames simplifies contextual type names if possible.
package embeddedresources
import (
	"fmt"
	"strconv"
	"strings"
	"github.com/pkg/errors"
	"k8s.io/klog/v2"
	"github.com/Azure/k8s-infra/hack/generator/pkg/astmodel"
)
type renamer struct {
	types astmodel.Types
}
type renameAction func(original astmodel.TypeName, associatedNames astmodel.TypeNameSet) (astmodel.TypeAssociation, error)
func (r renamer) simplifyEmbeddedNameToOriginalName(original astmodel.TypeName, associatedNames astmodel.TypeNameSet) (astmodel.TypeAssociation, error) {
	_, originalExists := r.types[original]
	if originalExists || len(associatedNames) != 1 {
		return nil, nil
	}
	klog.V(4).Infof("There are no usages of %q. Collapsing %q into the original for simplicity.", original, associatedNames.Single())
	renames := make(astmodel.TypeAssociation)
	renames[associatedNames.Single()] = original
	return renames, nil
}
func (r renamer) simplifyEmbeddedNameRemoveContextAndCount(_ astmodel.TypeName, associatedNames astmodel.TypeNameSet) (astmodel.TypeAssociation, error) {
	if len(associatedNames) != 1 {
		return nil, nil
	}
	renames := make(astmodel.TypeAssociation)
	associated := associatedNames.Single()
	embeddedName, err := parseContextualTypeName(associated)
	if err != nil {
		return nil, err
	}
	embeddedName.context = ""
	embeddedName.count = 0
	renames[associated] = embeddedName.ToSimplifiedTypeName()
	klog.V(4).Infof("There is only a single context %q is used in. Renaming it to %q for simplicity.", associated, renames[associated])
	return renames, nil
}
func (r renamer) simplifyEmbeddedNameRemoveContext(_ astmodel.TypeName, associatedNames astmodel.TypeNameSet) (astmodel.TypeAssociation, error) {
	associatedCountPerContext := make(map[string]int)
	for associated := range associatedNames {
		embeddedName, err := parseContextualTypeName(associated)
		if err != nil {
			return nil, err
		}
		associatedCountPerContext[embeddedName.context] = associatedCountPerContext[embeddedName.context] + 1
	}
	if len(associatedCountPerContext) != 1 {
		return nil, nil
	}
	renames := make(astmodel.TypeAssociation)
	for associated := range associatedNames {
		embeddedName, err := parseContextualTypeName(associated)
		if err != nil {
			return nil, err
		}
		embeddedName.context = ""
		renames[associated] = embeddedName.ToSimplifiedTypeName()
	}
	return renames, nil
}
func (r renamer) simplifyEmbeddedName(_ astmodel.TypeName, associatedNames astmodel.TypeNameSet) (astmodel.TypeAssociation, error) {
	renames := make(astmodel.TypeAssociation)
	for associated := range associatedNames {
		embeddedName, err := parseContextualTypeName(associated)
		if err != nil {
			return nil, err
		}
		possibleRename := embeddedName.ToSimplifiedTypeName()
		if !possibleRename.Equals(associated) {
			renames[associated] = possibleRename
		}
	}
	return renames, nil
}
func (r renamer) performRenames(
	renames astmodel.TypeAssociation,
	flag astmodel.TypeFlag) (astmodel.Types, error) {
	result := make(astmodel.Types)
	renamingVisitor := astmodel.TypeVisitorBuilder{
		VisitTypeName: func(this *astmodel.TypeVisitor, it astmodel.TypeName, ctx interface{}) (astmodel.Type, error) {
			if newName, ok := renames[it]; ok {
				return astmodel.IdentityVisitOfTypeName(this, newName, ctx)
			}
			return astmodel.IdentityVisitOfTypeName(this, it, ctx)
		},
	}.Build()
	for _, def := range r.types {
		updatedDef, err := renamingVisitor.VisitDefinition(def, nil)
		if err != nil {
			return nil, err
		}
		updatedType, err := flag.RemoveFrom(updatedDef.Type())
		if err != nil {
			return nil, err
		}
		result.Add(updatedDef.WithType(updatedType))
	}
	return result, nil
}