/
struct.go
293 lines (262 loc) · 7.39 KB
/
struct.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright 2018 henrylee2cn. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aster
import (
"fmt"
"go/ast"
"go/types"
"sort"
"strings"
"github.com/henrylee2cn/structtag"
)
// ---------------------------------- TypKind = Struct ----------------------------------
// NOTE: Panic, if TypKind != Struct
func (fa *facade) structure() *types.Struct {
typ := fa.typ()
t, ok := typ.(*types.Struct)
if !ok {
panic(fmt.Sprintf("aster: structure of non-Struct TypKind: %T", typ))
}
// initiate
if fa.structFields == nil {
numFields := t.NumFields()
fa.structFields = make([]*StructField, numFields)
for expr, tv := range fa.pkg.info.Types {
if tv.Type == t {
n, ok := expr.(*ast.StructType)
if !ok {
n = expr.(*ast.CompositeLit).Type.(*ast.StructType)
}
expandFields(n.Fields)
for i := 0; i < numFields; i++ {
fa.structFields[i] = fa.pkg.newStructField(n.Fields.List[i], t.Field(i))
}
break
}
}
}
return t
}
// NumFields returns the number of fields in the struct (including blank and embedded fields).
// NOTE: Panic, if TypKind != Struct
func (fa *facade) NumFields() int {
return fa.structure().NumFields()
}
// Field returns the i'th field for 0 <= i < NumFields().
// NOTE:
// Panic, if TypKind != Struct;
// Panic, if i is not in the range [0, NumFields()).
func (fa *facade) Field(i int) *StructField {
fa.structure() // make sure initiated
if i < 0 || i >= len(fa.structFields) {
panic("aster: Field index out of bounds")
}
return fa.structFields[i]
}
// FieldByName returns the struct field with the given name
// and a boolean indicating if the field was found.
// NOTE: Panic, if TypKind != Struct
func (fa *facade) FieldByName(name string) (field *StructField, found bool) {
fa.structure() // make sure initiated
for _, field := range fa.structFields {
if field.Name() == name {
return field, true
}
}
return nil, false
}
// StructField struct field object.
type StructField struct {
node *ast.Field
obj *types.Var
tags *Tags
}
func (p *PackageInfo) newStructField(node *ast.Field, obj *types.Var) *StructField {
sf := &StructField{
node: node,
obj: obj,
tags: newTags(node),
}
return sf
}
// Name returns the field's name.
func (sf *StructField) Name() string {
return sf.obj.Name()
}
// Exported reports whether the object is exported (starts with a capital letter).
// It doesn't take into account whether the object is in a local (function) scope
// or not.
func (sf *StructField) Exported() bool {
return sf.obj.Exported()
}
// Tags returns the field's tag object.
func (sf *StructField) Tags() *Tags {
return sf.tags
}
// Anonymous reports whether the variable is an embedded field.
// Same as Embedded; only present for backward-compatibility.
func (sf *StructField) Anonymous() bool {
return sf.obj.Anonymous()
}
// Embedded reports whether the variable is an embedded field.
func (sf *StructField) Embedded() bool {
return sf.obj.Embedded()
}
// Doc returns lead comment.
func (sf *StructField) Doc() string {
if sf.node.Doc == nil {
return ""
}
return sf.node.Doc.Text()
}
// Comment returns line comment.
func (sf *StructField) Comment() string {
if sf.node.Comment == nil {
return ""
}
return sf.node.Comment.Text()
}
// A Tags is the tag string in a struct field.
//
// By convention, tag strings are a concatenation of
// optionally space-separated key:"value" pairs.
// Each key is a non-empty string consisting of non-control
// characters other than space (U+0020 ' '), quote (U+0022 '"'),
// and colon (U+003A ':'). Each value is quoted using U+0022 '"'
// characters and Go string literal syntax.
type Tags struct {
field *ast.Field
tags *structtag.Tags
}
// Tag defines a single struct's string literal tag
//
// type Tag struct {
// Key is the tag key, such as json, xml, etc..
// i.e: `json:"foo,omitempty". Here key is: "json"
// Key string
//
// Name is a part of the value
// i.e: `json:"foo,omitempty". Here name is: "foo"
// Name string
//
// Options is a part of the value. It contains a slice of tag options i.e:
// `json:"foo,omitempty". Here options is: ["omitempty"]
// Options []string
// }
//
type Tag = structtag.Tag
func newTags(field *ast.Field) *Tags {
tags := &Tags{
field: field,
}
tags.reparse()
return tags
}
func (s *Tags) reparse() (err error) {
var value string
if s.field.Tag != nil {
value = strings.Trim(s.field.Tag.Value, "`")
}
s.tags, err = structtag.Parse(value)
if err != nil {
s.tags, _ = structtag.Parse("")
}
return err
}
// String reassembles the tags into a valid literal tag field representation
func (s *Tags) String() string {
return s.tags.String()
}
// Tags returns a slice of tags. The order is the original tag order unless it
// was changed.
func (s *Tags) Tags() []*Tag {
return s.tags.Tags()
}
// Get returns the tag associated with the given key. If the key is present
// in the tag the value (which may be empty) is returned. Otherwise the
// returned value will be the empty string. The ok return value reports whether
// the tag exists or not (which the return value is nil).
func (s *Tags) Get(key string) (*Tag, error) {
return s.tags.Get(key)
}
// Keys returns a slice of tag keys. The order is the original tag order unless it
// was changed.
func (s *Tags) Keys() []string {
return s.tags.Keys()
}
// Set sets the given tag. If the tag key already exists it'll override it.
// NOTE:
// Automatically call the Flush method.
func (s *Tags) Set(tag *Tag) error {
err := s.tags.Set(tag)
if err == nil {
s.Flush()
}
return err
}
// Delete deletes the tag for the given keys.
// NOTE:
// Automatically call the Flush method.
func (s *Tags) Delete(keys ...string) {
s.tags.Delete(keys...)
s.Flush()
}
// AddOptions adds the given option for the given key. If the option already
// exists it doesn't add it again.
// NOTE:
// Automatically call the Flush method.
func (s *Tags) AddOptions(key string, options ...string) {
s.tags.AddOptions(key, options...)
s.Flush()
}
// DeleteOptions deletes the given options for the given key.
// NOTE:
// Automatically call the Flush method.
func (s *Tags) DeleteOptions(key string, options ...string) {
s.tags.DeleteOptions(key, options...)
s.Flush()
}
// Flush resets the tags object into the struct field.
func (s *Tags) Flush() {
sort.Sort(s.tags)
value := s.tags.String()
if value == "" {
s.field.Tag = nil
} else {
if s.field.Tag == nil {
s.field.Tag = &ast.BasicLit{}
}
s.field.Tag.Value = "`" + value + "`"
}
}
func expandFields(fieldList *ast.FieldList) {
if fieldList == nil {
return
}
var list = make([]*ast.Field, 0, fieldList.NumFields())
for _, field := range fieldList.List {
list = append(list, field)
if len(field.Names) > 1 {
for _, name := range field.Names[1:] {
list = append(list, &ast.Field{
Names: []*ast.Ident{cloneIdent(name)},
Type: field.Type,
Tag: cloneBasicLit(field.Tag),
})
}
field.Names = field.Names[:1]
}
}
fieldList.List = list
}