forked from davidrjenni/reftools
/
main.go
280 lines (252 loc) · 6.53 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
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
// Copyright (c) 2023 shiba6v All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Some codes are copied from fillstruct from David R. Jenni.
// Here is the original copyright.
// Copyright (c) 2017 David R. Jenni. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"go/ast"
"go/format"
"go/token"
"go/types"
"log"
"os"
"path/filepath"
"sort"
"strings"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/buildutil"
"golang.org/x/tools/go/packages"
)
var errNotFound = errors.New("no struct literal found at selection")
// go run cmd/refillstruct/* -file ../gorefill/example/main.go -offset 493
func main() {
log.SetFlags(0)
log.SetPrefix("fillstruct: ")
var (
filename = flag.String("file", "", "filename")
modified = flag.Bool("modified", false, "read an archive of modified files from stdin")
offset = flag.Int("offset", 0, "byte offset of the struct literal, optional if -line is present")
btags buildutil.TagsFlag
)
flag.Var(&btags, "tags", buildutil.TagsFlagDoc)
flag.Parse()
if *offset == 0 || *filename == "" {
flag.PrintDefaults()
os.Exit(1)
}
path, err := absPath(*filename)
if err != nil {
log.Fatal(err)
}
var overlay map[string][]byte
if *modified {
overlay, err = buildutil.ParseOverlayArchive(os.Stdin)
if err != nil {
log.Fatalf("invalid archive: %v", err)
}
}
cfg := &packages.Config{
Overlay: overlay,
Mode: packages.LoadAllSyntax,
Tests: true,
Dir: filepath.Dir(path),
Fset: token.NewFileSet(),
BuildFlags: []string{"-tags", strings.Join([]string(btags), ",")},
Env: os.Environ(),
}
pkgs, err := packages.Load(cfg)
if err != nil {
log.Fatal(err)
}
if *offset > 0 {
err = byOffset(pkgs, path, *offset)
switch err {
case nil:
return
case errNotFound:
// try to use line information
default:
log.Fatal(err)
}
}
log.Fatal(errNotFound)
}
func absPath(filename string) (string, error) {
eval, err := filepath.EvalSymlinks(filename)
if err != nil {
return "", err
}
return filepath.Abs(eval)
}
type Field struct {
Name string
Pos token.Pos
Var *types.Var
}
func byOffset(lprog []*packages.Package, path string, offset int) error {
f, pkg, pos, err := findPos(lprog, path, offset)
if err != nil {
return err
}
lit, litInfo, err := findCompositeLit(f, pkg.TypesInfo, pos)
if err != nil {
return err
}
fields := make([]Field, 0)
sc := pkg.Types.Scope().Innermost(pos)
// 内側から外側に向かって、scopeを見る。
for sc.Parent() != nil {
for _, name := range sc.Names() {
obj := sc.Lookup(name)
// pos以降ならスキップ
if obj.Pos() > pos {
continue
}
// 変数のみ。型定義は除く。
if _, ok := obj.(*types.Var); !ok {
continue
}
var st *types.Struct
switch t := obj.Type().(type) {
case *types.Named:
if s, ok := t.Underlying().(*types.Struct); ok {
st = s
}
case *types.Struct:
st = t
default:
continue
}
if st == nil {
continue
}
// 自分自身には代入しない
if obj.Type().Underlying() == litInfo.typ.Underlying() {
continue
}
for i := 0; i < st.NumFields(); i++ {
fields = append(fields,
Field{
Name: name,
Pos: obj.Pos(),
Var: st.Field(i),
},
)
}
}
sc = sc.Parent()
}
// use other structs elements. lowercase
otherElts := make(map[string]*ast.KeyValueExpr)
// otherElts[strings.ToLower("Name")] = &ast.KeyValueExpr{
// Key: nil,
// Colon: 0,
// Value: &ast.SelectorExpr{X: &ast.Ident{Name: "p"}, Sel: &ast.Ident{Name: "Name"}},
// }
// 前から順番にコピーし、すでに存在するならスキップ。
sort.Slice(fields, func(i, j int) bool { return fields[i].Pos < fields[j].Pos })
for _, f := range fields {
if _, ok := otherElts[strings.ToLower(f.Var.Name())]; ok {
continue
}
otherElts[strings.ToLower(f.Var.Name())] = &ast.KeyValueExpr{
Key: nil,
Colon: 0,
Value: &ast.SelectorExpr{X: &ast.Ident{Name: f.Name}, Sel: &ast.Ident{Name: f.Var.Name()}},
}
}
start := lprog[0].Fset.Position(lit.Pos()).Offset
end := lprog[0].Fset.Position(lit.End()).Offset
importNames := buildImportNameMap(f)
newlit, lines := refillValue(pkg, importNames, lit, litInfo, otherElts)
out, err := prepareOutput(newlit, lines, start, end)
if err != nil {
return err
}
return json.NewEncoder(os.Stdout).Encode([]output{out})
}
func findPos(lprog []*packages.Package, path string, off int) (*ast.File, *packages.Package, token.Pos, error) {
for _, pkg := range lprog {
for _, f := range pkg.Syntax {
if file := pkg.Fset.File(f.Pos()); file.Name() == path {
if off > file.Size() {
return nil, nil, 0,
fmt.Errorf("file size (%d) is smaller than given offset (%d)",
file.Size(), off)
}
return f, pkg, file.Pos(off), nil
}
}
}
return nil, nil, 0, fmt.Errorf("could not find file %q", path)
}
func findCompositeLit(f *ast.File, info *types.Info, pos token.Pos) (*ast.CompositeLit, litInfo, error) {
var linfo litInfo
path, _ := astutil.PathEnclosingInterval(f, pos, pos)
for i, n := range path {
if lit, ok := n.(*ast.CompositeLit); ok {
linfo.name, _ = info.Types[lit].Type.(*types.Named)
linfo.typ, ok = info.Types[lit].Type.Underlying().(*types.Struct)
if !ok {
return nil, linfo, errNotFound
}
if expr, ok := path[i+1].(ast.Expr); ok {
linfo.hideType = hideType(info.Types[expr].Type)
}
return lit, linfo, nil
}
}
return nil, linfo, errNotFound
}
func hideType(t types.Type) bool {
switch t.(type) {
case *types.Array:
return true
case *types.Map:
return true
case *types.Slice:
return true
default:
return false
}
}
func buildImportNameMap(f *ast.File) map[string]string {
imports := make(map[string]string)
for _, i := range f.Imports {
if i.Name != nil && i.Name.Name != "_" {
path := i.Path.Value
imports[path[1:len(path)-1]] = i.Name.Name
}
}
return imports
}
type output struct {
Start int `json:"start"`
End int `json:"end"`
Code string `json:"code"`
}
func prepareOutput(n ast.Node, lines, start, end int) (output, error) {
fset := token.NewFileSet()
file := fset.AddFile("", -1, lines)
for i := 1; i <= lines; i++ {
file.AddLine(i)
}
var buf bytes.Buffer
if err := format.Node(&buf, fset, n); err != nil {
return output{}, err
}
return output{
Start: start,
End: end,
Code: buf.String(),
}, nil
}