-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseutil.go
192 lines (162 loc) · 3.84 KB
/
parseutil.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
package main
import (
"errors"
"fmt"
"go/types"
"regexp"
"github.com/fatih/structtag"
"github.com/iancoleman/strcase"
"github.com/samber/lo"
"golang.org/x/tools/go/packages"
)
type field struct {
Name string
Alias string
Depth int
}
type parseFieldsParams struct {
format string
tag string
tagRegex string
tagFormat string
tagStrict bool
embedded bool
excluded []string
}
func parseType(path, name string) (string, types.Type, error) {
cfg := &packages.Config{
Mode: packages.NeedTypes | packages.NeedImports | packages.NeedModule,
}
pkgs, err := packages.Load(cfg, path)
if err != nil {
return "", nil, err
}
err = validatePackages(pkgs)
if err != nil {
return "", nil, err
}
obj := pkgs[0].Types.Scope().Lookup(name)
if obj == nil || obj.Type() == nil {
return "", nil, fmt.Errorf("type %s not found", name)
}
return obj.Pkg().Name(), obj.Type(), nil
}
func parseFields(typ types.Type, params parseFieldsParams, depth int) ([]field, error) {
strct, ok := typ.Underlying().(*types.Struct)
if !ok {
return nil, fmt.Errorf("type %s is not a struct", typ)
}
depth++
var fields []field
for i := 0; i < strct.NumFields(); i++ {
if !strct.Field(i).IsField() && !strct.Field(i).Exported() {
continue
}
isExcluded := lo.Contains(params.excluded, strct.Field(i).Name())
if isExcluded {
continue
}
if strct.Field(i).Embedded() {
if !params.embedded {
continue
}
embeddedFields, err := parseFields(strct.Field(i).Type(), params, depth)
if err != nil {
return nil, err
}
fields = append(fields, embeddedFields...)
} else {
alias := strct.Field(i).Name()
format := params.format
if params.tag != "" {
tags, err := structtag.Parse(strct.Tag(i))
if err != nil {
return nil, err
}
var matchedTag *structtag.Tag
for _, t := range tags.Tags() {
if t.Key == params.tag {
matchedTag = t
break
}
}
if matchedTag != nil {
if params.tagRegex == "" {
alias = matchedTag.Name
format = params.tagFormat
} else {
regex, err := regexp.Compile(params.tagRegex)
if err != nil {
return nil, err
}
matches := regex.FindStringSubmatch(matchedTag.Value())
if len(matches) < 2 {
if params.tagStrict {
return nil, fmt.Errorf(
"tag %s of field %s does not match regex %s",
matchedTag.Value(),
strct.Field(i).Name(),
params.tagRegex,
)
}
} else {
format = params.tagFormat
alias = matches[1]
}
}
} else {
if params.tagStrict {
return nil, fmt.Errorf("tag %s not found for field %s", params.tag, strct.Field(i).Name())
}
}
}
switch format {
case formatAsIs:
case formatSnakeCase:
alias = strcase.ToSnake(alias)
case formatCamelCase:
alias = strcase.ToLowerCamel(alias)
case formatPascalCase:
alias = strcase.ToCamel(alias)
default:
return nil, fmt.Errorf("invalid format %s", params.format)
}
fields = append(fields, field{
Name: strct.Field(i).Name(),
Alias: alias,
Depth: depth,
})
}
}
// The fields with the lowest depth are the most important.
for l := 0; l < len(fields); l++ {
for k := 0; k < len(fields); k++ {
if fields[l].Name == fields[k].Name && l != k {
if fields[l].Depth > fields[k].Depth {
fields = append(fields[:l], fields[l+1:]...)
} else {
fields = append(fields[:k], fields[k+1:]...)
}
}
}
}
return fields, nil
}
func validatePackages(pkgs []*packages.Package) error {
var err error
packages.Visit(pkgs, nil, func(pkg *packages.Package) {
for _, e := range pkg.Errors {
err = e
}
})
if err != nil {
return err
}
if len(pkgs) == 0 {
return errors.New("no packages found")
}
if len(pkgs) > 1 {
return fmt.Errorf("multiple packages found")
}
return nil
}