/
run.go
76 lines (74 loc) · 1.49 KB
/
run.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
package bson
import (
"bytes"
"flag"
"fmt"
"github.com/15mga/kiwi/util"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/fs"
"os"
"path/filepath"
"strings"
)
func Run() {
dir := flag.String("d", "", "pb out dir")
flag.Parse()
root := *dir
err := filepath.WalkDir(root, func(fullName string, d fs.DirEntry, err error) error {
if fullName == root || d.IsDir() {
return nil
}
bs, err := os.ReadFile(fullName)
if err != nil {
return err
}
fileSet := token.NewFileSet()
f, err := parser.ParseFile(fileSet, fullName, bs, parser.ParseComments)
if err != nil {
return err
}
for _, d := range f.Decls {
gd, ok := d.(*ast.GenDecl)
if !ok || gd.Tok != token.TYPE {
continue
}
for _, spec := range gd.Specs {
ts := spec.(*ast.TypeSpec)
st, ok := ts.Type.(*ast.StructType)
if !ok {
continue
}
for _, fd := range st.Fields.List {
if len(fd.Names) != 1 || fd.Tag == nil {
continue
}
fn := fd.Names[0].Name
val := fd.Tag.Value
if strings.Index(val, "bson:") > -1 {
continue
}
valLen := len(val)
is := ""
if fn == "Id" {
is = " bson:\"_id\""
} else {
is = fmt.Sprintf(" bson:\"%s\"", util.ToUnderline(fn))
}
fd.Tag.Value = val[:valLen-1] + is + val[valLen-1:]
}
}
}
var buff bytes.Buffer
err = format.Node(&buff, fileSet, f)
if err != nil {
return err
}
return os.WriteFile(fullName, buff.Bytes(), fs.ModePerm)
})
if err != nil {
panic(err)
}
}