-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
60 lines (55 loc) · 1.17 KB
/
type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package simplemock
import (
"go/types"
"path/filepath"
)
// TypeZeroValue returns zero value of type.
func TypeZeroValue(t types.Type) string {
switch v := t.Underlying().(type) {
case *types.Basic:
return typeBasicZeroValue(v)
case *types.Struct:
return types.TypeString(t, qualifier) + `{}`
default:
return `nil`
}
}
func typeBasicZeroValue(basic *types.Basic) string {
switch basic.Info() {
case types.IsBoolean:
return `false`
case types.IsInteger:
return `0`
case types.IsUnsigned:
return `0`
case types.IsFloat:
return `0`
case types.IsComplex:
return `0`
case types.IsString:
return `""`
default:
return `nil`
}
}
func qualifier(pkg *types.Package) string {
if pkg.Path() == "" {
return ""
}
return filepath.Base(pkg.Path())
}
// TypeString convert types.Type to string
func TypeString(t types.Type) string {
switch v := t.(type) {
case *types.Array:
return "[]" + TypeString(v.Elem())
case *types.Slice:
return "[]" + TypeString(v.Elem())
case *types.Pointer:
return "*" + TypeString(v.Elem())
case *types.Map:
return "map[" + TypeString(v.Key()) + "]" + TypeString(v.Elem())
default:
return types.TypeString(t, qualifier)
}
}