@@ -281,9 +281,11 @@ func (e *extractor) usedPkg(pkg string) {
281
281
}
282
282
283
283
var (
284
- typeAny = types .NewInterfaceType (nil , nil ).Complete () // interface{}
285
- typeBytes = types .NewSlice (types .Typ [types .Byte ]) // []byte
286
- typeError = types .Universe .Lookup ("error" ).Type () // error
284
+ typeAny = types .Universe .Lookup ("any" ).Type () // any or interface{}
285
+ typeByte = types .Universe .Lookup ("byte" ).Type () // byte
286
+ typeBytes = types .NewSlice (typeByte ) // []byte
287
+ typeString = types .Universe .Lookup ("string" ).Type () // string
288
+ typeError = types .Universe .Lookup ("error" ).Type () // error
287
289
)
288
290
289
291
// Note that we can leave positions, packages, and parameter/result names empty.
@@ -804,12 +806,13 @@ func (e *extractor) reportDecl(x *ast.GenDecl) (a []cueast.Decl) {
804
806
}
805
807
806
808
typ := e .pkg .TypesInfo .TypeOf (name )
807
- if s := typ . String (); ! strings . Contains ( s , "untyped" ) {
808
- switch s {
809
- case "byte" , "string" , "error" :
810
- default :
811
- cv = cueast . NewBinExpr ( cuetoken . AND , e . makeType ( typ ), cv )
809
+ switch typ {
810
+ case typeByte , typeString , typeError :
811
+ default :
812
+ if basic , ok := typ .( * types. Basic ); ok && basic . Info () & types . IsUntyped != 0 {
813
+ break // untyped basic types do not make valid identifiers
812
814
}
815
+ cv = cueast .NewBinExpr (cuetoken .AND , e .makeType (typ ), cv )
813
816
}
814
817
815
818
f .Value = cv
@@ -963,7 +966,7 @@ func supportedType(stack []types.Type, t types.Type) (ok bool) {
963
966
t = t .Underlying ()
964
967
switch x := t .(type ) {
965
968
case * types.Basic :
966
- return x .String () != "invalid type"
969
+ return x .Kind () != types . Invalid
967
970
case * types.Named :
968
971
return true
969
972
case * types.Pointer :
@@ -1042,9 +1045,8 @@ func (e *extractor) makeType(expr types.Type) (result cueast.Expr) {
1042
1045
return e .ident ("_" , false )
1043
1046
}
1044
1047
// Check for builtin packages.
1045
- // TODO: replace these literal types with a reference to the fixed
1046
- switch obj .Type ().String () {
1047
- case "time.Time" :
1048
+ switch {
1049
+ case obj .Pkg ().Path () == "time" && obj .Name () == "Time" :
1048
1050
ref := e .ident (e .pkgNames [obj .Pkg ().Path ()].name , false )
1049
1051
var name * cueast.Ident
1050
1052
if ref .Name != "time" {
@@ -1053,7 +1055,7 @@ func (e *extractor) makeType(expr types.Type) (result cueast.Expr) {
1053
1055
ref .Node = cueast .NewImport (name , "time" )
1054
1056
return cueast .NewSel (ref , obj .Name ())
1055
1057
1056
- case "math/big. Int" :
1058
+ case obj . Pkg (). Path () == "math/big" && obj . Name () == " Int" :
1057
1059
return e .ident ("int" , false )
1058
1060
1059
1061
default :
@@ -1151,16 +1153,18 @@ func (e *extractor) makeType(expr types.Type) (result cueast.Expr) {
1151
1153
return st
1152
1154
1153
1155
case * types.Slice :
1154
- // TODO: should this be x.Elem().Underlying().String()? One could
1155
- // argue either way.
1156
- if x .Elem ().String () == "byte" {
1156
+ // Note that []byte is treated different from []uint8,
1157
+ // even though byte is an alias for the basic type uint8.
1158
+ // TODO: reconsider this; both encoding/json and the future v2
1159
+ // encode []uint8, or anything assignable to []byte, as bytes.
1160
+ if x .Elem () == typeByte {
1157
1161
return e .ident ("bytes" , false )
1158
1162
}
1159
1163
return cueast .NewList (& cueast.Ellipsis {Type : e .makeType (x .Elem ())})
1160
1164
1161
1165
case * types.Array :
1162
- if x .Elem (). String () == "byte" {
1163
- // TODO: no way to constraint lengths of bytes for now, as regexps
1166
+ if x .Elem () == typeByte {
1167
+ // TODO: no way to constrain lengths of bytes for now, as regexps
1164
1168
// operate on Unicode, not bytes. So we need
1165
1169
// fmt.Fprint(e.w, fmt.Sprintf("=~ '^\C{%d}$'", x.Len())),
1166
1170
// but regexp does not support that.
@@ -1202,7 +1206,7 @@ func (e *extractor) makeType(expr types.Type) (result cueast.Expr) {
1202
1206
case types .Complex64 , types .Complex128 :
1203
1207
return e .ident ("_" , false )
1204
1208
}
1205
- return e .ident (x .String (), false )
1209
+ return e .ident (x .Name (), false )
1206
1210
1207
1211
case * types.Union :
1208
1212
var exprs []cueast.Expr
0 commit comments