-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.go
206 lines (171 loc) · 3.46 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package jot
import (
"fmt"
"go/ast"
"go/types"
"io"
"reflect"
)
type BaseTypeSpec struct {
name string
imp *ImportSpec
}
type TypeSpec interface {
Spec
Name() string
Import() *ImportSpec
From(*ImportSpec) TypeSpec
}
type PtrSpec struct {
BaseTypeSpec
Ptr TypeSpec
}
func (s *PtrSpec) Write(w io.Writer) error {
if _, err := io.WriteString(w, "*"); err != nil {
return err
}
return s.Ptr.Write(w)
}
type ArraySpec struct {
BaseTypeSpec
Elt TypeSpec
}
func (s *ArraySpec) Write(w io.Writer) error {
if _, err := io.WriteString(w, "[]"); err != nil {
return err
}
return s.Elt.Write(w)
}
type MapSpec struct {
BaseTypeSpec
Key TypeSpec
Value TypeSpec
}
func (s *MapSpec) Write(w io.Writer) error {
if _, err := io.WriteString(w, "map["); err != nil {
return err
}
if err := s.Key.Write(w); err != nil {
return err
}
if _, err := io.WriteString(w, "]"); err != nil {
return err
}
return s.Value.Write(w)
}
type ChanDir int
const (
None ChanDir = iota
Send ChanDir = iota
Recv ChanDir = iota
)
type ChanSpec struct {
BaseTypeSpec
dir ChanDir
value TypeSpec
}
func (s *ChanSpec) Write(w io.Writer) error {
if s.dir == Send {
if _, err := io.WriteString(w, "<-"); err != nil {
return err
}
}
if _, err := io.WriteString(w, "chan"); err != nil {
return err
}
if s.dir == Recv {
if _, err := io.WriteString(w, "<-"); err != nil {
return err
}
}
if _, err := io.WriteString(w, " "); err != nil {
return err
}
return s.value.Write(w)
}
type FuncSpec struct {
BaseTypeSpec
params []TypeSpec
returns []TypeSpec
}
func (s *FuncSpec) Write(w io.Writer) error {
io.WriteString(w, "func(")
for _, p := range s.params {
p.Write(w)
}
io.WriteString(w, ")")
if len(s.returns) > 0 {
io.WriteString(w, " ")
if len(s.returns) > 1 {
io.WriteString(w, "(")
}
for _, r := range s.returns {
r.Write(w)
}
if len(s.returns) > 1 {
io.WriteString(w, ")")
}
}
return nil
}
func Ptr(typ TypeSpec) *PtrSpec {
return &PtrSpec{Ptr: typ}
}
func Array(typ TypeSpec) *ArraySpec {
return &ArraySpec{Elt: typ}
}
func Map(key TypeSpec, value TypeSpec) *MapSpec {
return &MapSpec{Key: key, Value: value}
}
func Chan(value TypeSpec) *ChanSpec {
return &ChanSpec{value: value}
}
func SendChan(value TypeSpec) *ChanSpec {
return &ChanSpec{dir: Send, value: value}
}
func RecvChan(value TypeSpec) *ChanSpec {
return &ChanSpec{dir: Recv, value: value}
}
func Func() *FuncSpec {
return &FuncSpec{}
}
func (s *FuncSpec) AddReturnType(typ TypeSpec) *FuncSpec {
s.returns = append(s.returns, typ)
return s
}
func (s *FuncSpec) AddParameter(typ TypeSpec) *FuncSpec {
s.params = append(s.params, typ)
return s
}
func Type(typ reflect.Type) TypeSpec {
return &BaseTypeSpec{name: typ.String()}
}
func TypeASTObject(obj *ast.Object) TypeSpec {
return &BaseTypeSpec{name: obj.Name}
}
func TypeTypesObject(obj types.Object) TypeSpec {
return &BaseTypeSpec{name: obj.Type().String()}
}
func TypeASTSelector(sel *ast.SelectorExpr) TypeSpec {
// XXX: LOL, hope so!
x := sel.X.(*ast.Ident).Name
f := sel.Sel.Name
return TypeString(fmt.Sprintf("%s.%s", x, f))
}
func TypeString(name string) TypeSpec {
return &BaseTypeSpec{name: name}
}
func (s *BaseTypeSpec) From(imp *ImportSpec) TypeSpec {
s.imp = imp
return s
}
func (s *BaseTypeSpec) Import() *ImportSpec {
return s.imp
}
func (s *BaseTypeSpec) Name() string {
return s.name
}
func (s *BaseTypeSpec) Write(w io.Writer) error {
_, err := io.WriteString(w, s.Name())
return err
}