-
Notifications
You must be signed in to change notification settings - Fork 1
/
value.go
191 lines (184 loc) · 3.92 KB
/
value.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package congo
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"log"
"strconv"
"unsafe"
"golang.org/x/tools/go/ssa"
)
func zero(ty types.Type) interface{} {
switch t := ty.(type) {
case *types.Basic:
if t.Kind() == types.UntypedNil {
panic("untyped nil has no zero value")
}
switch t.Kind() {
case types.Bool:
return false
case types.Int:
return int(0)
case types.Int8:
return int8(0)
case types.Int16:
return int16(0)
case types.Int32:
return int32(0)
case types.Int64:
return int64(0)
case types.Uint:
return uint(0)
case types.Uint8:
return uint8(0)
case types.Uint16:
return uint16(0)
case types.Uint32:
return uint32(0)
case types.Uint64:
return uint64(0)
case types.Uintptr:
return uintptr(0)
case types.Float32:
return float32(0)
case types.Float64:
return float64(0)
case types.Complex64:
return complex64(0)
case types.Complex128:
return complex128(0)
case types.String:
return ""
case types.UnsafePointer:
return unsafe.Pointer(nil)
default:
panic(fmt.Sprint("zero for unexpected type:", t))
}
case *types.Pointer:
return (*interface{})(nil)
case *types.Array:
a := make([]interface{}, t.Len())
for i := range a {
a[i] = zero(t.Elem())
}
return a
case *types.Named:
return zero(t.Underlying())
case *types.Interface:
panic("unimplemented")
case *types.Slice:
return []interface{}(nil)
case *types.Struct:
s := make([]interface{}, t.NumFields())
for i := range s {
s[i] = zero(t.Field(i).Type())
}
return s
case *types.Tuple:
if t.Len() == 1 {
return zero(t.At(0).Type())
}
s := make([]interface{}, t.Len())
for i := range s {
s[i] = zero(t.At(i).Type())
}
return s
case *types.Chan:
return chan interface{}(nil)
case *types.Map:
return map[interface{}][]interface{}(nil)
case *types.Signature:
return (*ssa.Function)(nil)
}
panic(fmt.Sprint("zero: unexpected ", ty))
}
func type2ASTExpr(ty types.Type) ast.Expr {
switch ty := ty.(type) {
case *types.Basic:
return ast.NewIdent(ty.Name())
case *types.Named:
return &ast.SelectorExpr{
X: ast.NewIdent(ty.Obj().Pkg().Name()),
Sel: ast.NewIdent(ty.Obj().Id()),
}
case *types.Pointer:
return &ast.StarExpr{
X: type2ASTExpr(ty.Elem()),
}
default:
panic("unimplemented")
}
}
func value2ASTExpr(v interface{}, ty types.Type) ast.Expr {
switch ty := ty.(type) {
case *types.Basic:
info := ty.Info()
switch {
case info&types.IsBoolean > 0:
s := "false"
if v.(bool) {
s = "true"
}
return ast.NewIdent(s)
case info&types.IsInteger > 0:
return &ast.BasicLit{
Kind: token.INT,
Value: fmt.Sprintf("%v", v),
}
case info&types.IsString > 0:
return &ast.BasicLit{
Kind: token.STRING,
Value: strconv.Quote(v.(string)),
}
default:
panic("unimplemented")
}
case *types.Pointer:
p := v.(*interface{})
if p == nil {
return ast.NewIdent("nil")
}
if basicTy, ok := ty.Elem().(*types.Basic); ok {
return &ast.CallExpr{
Fun: ast.NewIdent(basicTy.Name() + "ptr"),
Args: []ast.Expr{
value2ASTExpr(*p, basicTy),
},
}
}
namedTy, ok := ty.Elem().(*types.Named)
if !ok {
log.Fatalf("pointer of unnamed and non-basic type is not supported")
panic("unimplemented")
}
typeName := namedTy.Obj()
name := typeName.Name()
pkgName := typeName.Pkg().Name()
elem := ty.Elem().Underlying()
switch ty := elem.(type) {
case *types.Struct:
n := ty.NumFields()
elts := make([]ast.Expr, 0, n)
for i := 0; i < n; i++ {
e := ty.Field(i)
elts = append(elts, &ast.KeyValueExpr{
Key: ast.NewIdent(e.Name()),
Value: value2ASTExpr(((*p).([]interface{}))[i], e.Type()),
})
}
return &ast.UnaryExpr{
Op: token.AND,
X: &ast.CompositeLit{
Type: &ast.SelectorExpr{
X: ast.NewIdent(pkgName),
Sel: ast.NewIdent(name),
},
Elts: elts,
},
}
}
panic("unimplemented")
}
panic("unimplemented")
}