Skip to content

Commit

Permalink
pkg/crd: fix alias conversion to schema with gotypesalias=1
Browse files Browse the repository at this point in the history
This patch will be needed when upgrading to Go 1.23 as type alias now
generate a proper type (instead of directly being the basic type they
alias to), see https://pkg.go.dev/go/types#Alias for more info.

This just reproduces the old behavior by retrieving the underlying type
from the alias. If needed, we could treat alias more gracefully to make
the type ref link now that we have all the type information.

Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
  • Loading branch information
mtardy committed Oct 21, 2024
1 parent 7587d2c commit e443da3
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pkg/crd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,18 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("unknown type %s", ident.Name), ident))
return &apiext.JSONSchemaProps{}
}
// This reproduces the behavior we had pre gotypesalias=1 (needed if this
// project is compiled with default settings and Go >= 1.23).
if aliasInfo, isAlias := typeInfo.(*types.Alias); isAlias {
typeInfo = aliasInfo.Underlying()
}
if basicInfo, isBasic := typeInfo.(*types.Basic); isBasic {
typ, fmt, err := builtinToType(basicInfo, ctx.allowDangerousTypes)
if err != nil {
ctx.pkg.AddError(loader.ErrFromNode(err, ident))
}
// Check for type aliasing to a basic type. Note that this is no longer
// needed with gotypesalias=1 as the above isBasic check is false. See
// more https://pkg.go.dev/go/types#Alias:
// Check for type aliasing to a basic type for gotypesalias=0. See more
// in documentation https://pkg.go.dev/go/types#Alias:
// > For gotypesalias=1, alias declarations produce an Alias type.
// > Otherwise, the alias information is only in the type name, which
// > points directly to the actual (aliased) type.
Expand Down

0 comments on commit e443da3

Please sign in to comment.