Skip to content

Commit

Permalink
cmd/cue: support stringable types as map keys
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Mårtensson <[email protected]>
  • Loading branch information
addreas committed Feb 17, 2023
1 parent 7d89acd commit 41e78cc
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cmd/cue/cmd/get_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,14 @@ func supportedType(stack []types.Type, t types.Type) (ok bool) {
case *types.Array:
return supportedType(stack, x.Elem())
case *types.Map:
if b, ok := x.Key().Underlying().(*types.Basic); !ok || b.Kind() != types.String {
typ := x.Key().Underlying()
if b, ok := typ.(*types.Basic); !ok || b.Kind() != types.String {
ptr := types.NewPointer(typ)
for _, iface := range toString {
if types.Implements(typ, iface) || types.Implements(ptr, iface) {
return supportedType(stack, x.Elem())
}
}
return false
}
return supportedType(stack, x.Elem())
Expand Down Expand Up @@ -1115,7 +1122,9 @@ func (e *extractor) makeType(expr types.Type) (result cueast.Expr) {

case *types.Map:
if b, ok := x.Key().Underlying().(*types.Basic); !ok || b.Kind() != types.String {
panic(fmt.Sprintf("unsupported map key type %T", x.Key()))
if alt := e.altType(x.Key().Underlying()); alt == nil {
panic(fmt.Sprintf("unsupported map key type %T", x.Key()))
}
}

f := &cueast.Field{
Expand Down

0 comments on commit 41e78cc

Please sign in to comment.