-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfile.go
110 lines (92 loc) · 2.19 KB
/
file.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
package main
import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"strings"
)
// parseFile scans the input file and returns list of structs to inject custom fields to.
func parseFile(inputPath string) (areas []textArea, err error) {
areas = []textArea{}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, inputPath, nil, parser.ParseComments)
if err != nil {
return
}
for _, decl := range f.Decls {
// check if is generic declaration
genDecl, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
var typeSpec *ast.TypeSpec
for _, spec := range genDecl.Specs {
if ts, tsOK := spec.(*ast.TypeSpec); tsOK {
typeSpec = ts
break
}
}
// skip if can't get type spec
if typeSpec == nil {
continue
}
// not a struct, skip
structDecl, ok := typeSpec.Type.(*ast.StructType)
if !ok {
continue
}
area := textArea{
name: typeSpec.Name.String(),
start: int(typeSpec.Pos()),
end: int(typeSpec.End()),
insertPos: int(structDecl.Fields.Closing) - 1,
fields: []*customField{},
}
if genDecl.Doc == nil {
continue
}
// build the list of text areas from comments
for _, comment := range genDecl.Doc.List {
field := fieldFromComment(comment.Text)
if field == nil || len(field.fieldName) == 0 || len(field.fieldName) == 0 {
continue
}
// only inject private fields
firtChar := string(field.fieldName[0])
if strings.ToUpper(firtChar) == firtChar {
continue
}
area.fields = append(area.fields, field)
}
if len(area.fields) != 0 {
areas = append(areas, area)
}
}
return
}
// writeFile updates the given files with given text areas.
func writeFile(inputPath string, areas []textArea) (err error) {
f, err := os.Open(inputPath)
if err != nil {
return
}
contents, err := ioutil.ReadAll(f)
if err != nil {
return
}
if err = f.Close(); err != nil {
return
}
// inject custom fields from the end of file first to preserve order
for i := len(areas) - 1; i >= 0; i-- {
contents = injectField(contents, areas[i])
}
if err = ioutil.WriteFile(inputPath, contents, 0644); err != nil {
return
}
log.Printf("file %q is injected with custom fields", inputPath)
return
}