-
Notifications
You must be signed in to change notification settings - Fork 14
/
reg_import.go
44 lines (37 loc) · 984 Bytes
/
reg_import.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
// Package goqlimport is a prof of concept, just to show how to add a new column to a table
// this is slow, just for demonstration
package goqlimport
import (
"bytes"
"github.com/fzerorubigd/goql"
"github.com/fzerorubigd/goql/astdata"
"golang.org/x/tools/imports"
)
type importCheck struct {
opt *imports.Options
}
func (ic importCheck) Value(in interface{}) goql.Bool {
// it must be astdata.File
fl := in.(*astdata.File)
var src = []byte(fl.Source())
dst, err := imports.Process(fl.FullPath(), src, ic.opt)
b := err == nil && bytes.Equal(src, dst)
return goql.Bool{
Bool: b,
}
}
func newImportCheck() goql.BoolValuer {
return importCheck{
opt: &imports.Options{
Fragment: false,
AllErrors: false,
Comments: true,
TabIndent: true,
TabWidth: 8,
FormatOnly: true,
}}
}
// Register the field. Since this is slow one, let the user choose to add it or not
func Register() {
goql.RegisterField("files", "goimport", newImportCheck())
}