-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
196 lines (165 loc) · 4.32 KB
/
main.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
package main
import (
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"unicode/utf8"
"golang.org/x/tools/go/packages"
"github.com/phelmkamp/metatag/directive"
"github.com/phelmkamp/metatag/meta"
)
const (
accessTemplate = "%s.%s"
)
var (
goFileRegEx = regexp.MustCompile(`.+\.go$`)
metaTagRegEx = regexp.MustCompile(`meta:".+"`)
)
func initFile(origPath string) *os.File {
filename := strings.Replace(origPath, ".go", "_meta.go", 1)
log.Printf("Creating file: %s\n", filename)
f, err := os.Create(filename)
if err != nil {
log.Fatalf("os.Create() failed: %v\n", err)
}
return f
}
func first(s string) (string, int) {
if s == "" {
return "", 0
}
r, n := utf8.DecodeRuneInString(s)
return string(r), n
}
func lowerFirst(s string) string {
f, n := first(s)
return strings.ToLower(f) + s[n:]
}
func upperFirst(s string) string {
f, n := first(s)
return strings.ToUpper(f) + s[n:]
}
func main() {
var root string
flag.StringVar(&root, "path", ".", "directory path to scan for *.go files")
flag.Parse()
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && goFileRegEx.MatchString(info.Name()) {
cleanPath := filepath.Clean(path)
log.Printf("Parsing file: %s\n", cleanPath)
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, cleanPath, nil, 0)
if err != nil {
return fmt.Errorf("parser.ParseFile() failed: %w", err)
}
tgt := directive.Target{
MetaFile: meta.NewFile(astFile.Name.Name),
}
cfg := &packages.Config{Mode: packages.NeedName | packages.NeedImports}
pkgs, err := packages.Load(cfg, cleanPath)
if err != nil {
return err
}
importPaths := pkgs[0].Imports
ast.Inspect(astFile, func(n ast.Node) bool {
var expr ast.Expr
switch nt := n.(type) {
case *ast.TypeSpec:
expr = nt.Type
tgt.RcvType = nt.Name.Name
}
if expr == nil {
return true
}
st, ok := expr.(*ast.StructType)
if !ok {
return true
}
log.Printf("Found struct: %s\n", tgt.RcvType)
tgt.RcvName, _ = first(tgt.RcvType)
tgt.RcvName = strings.ToLower(tgt.RcvName)
for _, f := range st.Fields.List {
if f.Tag == nil {
continue
}
metaTag := metaTagRegEx.FindString(f.Tag.Value)
if metaTag == "" {
continue
}
log.Printf("Found meta tag %s\n", metaTag)
metaTag = strings.TrimPrefix(metaTag, "meta:\"")
metaTag = strings.TrimSuffix(metaTag, "\"")
// some directives modify target, use a local copy
fldTgt := tgt
var fldPkg string
switch ft := f.Type.(type) {
case *ast.Ident:
fldTgt.FldType = ft.Name
case *ast.SelectorExpr:
// package.type
fldPkg = ft.X.(*ast.Ident).Name
fldTgt.FldType = fmt.Sprintf(accessTemplate, fldPkg, ft.Sel.Name)
case *ast.ArrayType:
switch elt := ft.Elt.(type) {
case *ast.Ident:
fldTgt.FldType = "[]" + elt.Name
case *ast.SelectorExpr:
// package.type
fldPkg = elt.X.(*ast.Ident).Name
fldTgt.FldType = fmt.Sprintf(accessTemplate, "[]"+fldPkg, elt.Sel.Name)
}
case *ast.MapType:
fldTgt.FldType = fmt.Sprintf("map[%s]%s", ft.Key.(*ast.Ident).Name, ft.Value.(*ast.Ident).Name)
case interface{}:
fldTgt.FldType = "interface{}"
default:
log.Printf("Unsupported field type: %v\n", ft)
continue
}
fldTgt.FldNames = make([]string, len(f.Names))
for i := range f.Names {
fldTgt.FldNames[i] = f.Names[i].Name
}
directive.RunAll(strings.Split(metaTag, ";"), &fldTgt)
if fldPkg != "" {
var importPath string
for _, p := range importPaths {
if p.Name == fldPkg {
importPath = p.PkgPath
}
}
log.Printf("Adding import: \"%s\"\n", importPath)
tgt.MetaFile.Imports[importPath] = struct{}{}
}
}
return true
})
if len(tgt.MetaFile.Methods) < 1 {
return nil
}
osFile := initFile(cleanPath)
defer func() {
if err := osFile.Close(); err != nil {
log.Printf("File.Close() failed: %v\n", err)
}
}()
if _, err := osFile.WriteString(tgt.MetaFile.String()); err != nil {
log.Fatalf("File.WriteString() failed: %v\n", err)
}
}
return nil
})
if err != nil {
log.Fatal(err)
}
}