Skip to content

Commit 41e78cc

Browse files
committed
cmd/cue: support stringable types as map keys
Signed-off-by: Andreas Mårtensson <[email protected]>
1 parent 7d89acd commit 41e78cc

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Diff for: cmd/cue/cmd/get_go.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,14 @@ func supportedType(stack []types.Type, t types.Type) (ok bool) {
962962
case *types.Array:
963963
return supportedType(stack, x.Elem())
964964
case *types.Map:
965-
if b, ok := x.Key().Underlying().(*types.Basic); !ok || b.Kind() != types.String {
965+
typ := x.Key().Underlying()
966+
if b, ok := typ.(*types.Basic); !ok || b.Kind() != types.String {
967+
ptr := types.NewPointer(typ)
968+
for _, iface := range toString {
969+
if types.Implements(typ, iface) || types.Implements(ptr, iface) {
970+
return supportedType(stack, x.Elem())
971+
}
972+
}
966973
return false
967974
}
968975
return supportedType(stack, x.Elem())
@@ -1115,7 +1122,9 @@ func (e *extractor) makeType(expr types.Type) (result cueast.Expr) {
11151122

11161123
case *types.Map:
11171124
if b, ok := x.Key().Underlying().(*types.Basic); !ok || b.Kind() != types.String {
1118-
panic(fmt.Sprintf("unsupported map key type %T", x.Key()))
1125+
if alt := e.altType(x.Key().Underlying()); alt == nil {
1126+
panic(fmt.Sprintf("unsupported map key type %T", x.Key()))
1127+
}
11191128
}
11201129

11211130
f := &cueast.Field{

0 commit comments

Comments
 (0)