Skip to content

Commit

Permalink
cmd/generate-fastjson: use x/tools/go/packages (#1)
Browse files Browse the repository at this point in the history
Use golang.org/x/tools/go/packages for parsing
Go packages, which works with Go modules.
  • Loading branch information
axw authored May 11, 2020
1 parent 676cec7 commit dca77b8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ go:
- stable
- "1.8.x"

install:
- go get

script:
- go test -v ./...
- go test -v
47 changes: 13 additions & 34 deletions cmd/generate-fastjson/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@ import (
"flag"
"fmt"
"go/ast"
"go/build"
"go/format"
"go/importer"
"go/parser"
"go/token"
"go/types"
"io"
"log"
"os"
"path/filepath"
"reflect"
"sort"
"strings"

"golang.org/x/tools/go/packages"
)

const (
Expand Down Expand Up @@ -73,37 +71,18 @@ func main() {
}
}

cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
cfg := &packages.Config{
Mode: packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo,
}
buildPkg, err := build.Default.Import(flag.Arg(0), cwd, build.ImportComment)
pkgs, err := packages.Load(cfg, flag.Arg(0))
if err != nil {
log.Fatal(err)
}

fset := token.NewFileSet()
files := make([]*ast.File, len(buildPkg.GoFiles))
for i, goFile := range buildPkg.GoFiles {
filename := filepath.Join(buildPkg.Dir, goFile)
f, err := parser.ParseFile(fset, filename, nil, parser.ParseComments|parser.DeclarationErrors)
if err != nil {
log.Fatal(err)
}
files[i] = f
}

cfg := types.Config{
IgnoreFuncBodies: true,
Importer: importer.Default(),
}
info := types.Info{
Defs: make(map[*ast.Ident]types.Object),
fmt.Fprintf(os.Stderr, "load: %v\n", err)
os.Exit(1)
}
pkg, err := cfg.Check(buildPkg.ImportPath, fset, files, &info)
if err != nil {
log.Fatal(err)
if packages.PrintErrors(pkgs) > 0 {
os.Exit(1)
}
pkg := pkgs[0]

var buf bytes.Buffer
fmt.Fprintf(&buf, `
Expand All @@ -114,10 +93,10 @@ package %s
import (
%q
)
`[1:], pkg.Name(), fastjsonPath)
`[1:], pkg.Types.Name(), fastjsonPath)

var generated int
for _, f := range files {
for _, f := range pkg.Syntax {
for _, decl := range f.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.TYPE {
Expand All @@ -128,7 +107,7 @@ import (
if !ok {
continue
}
obj := info.Defs[typeSpec.Name]
obj := pkg.TypesInfo.Defs[typeSpec.Name]
if obj == nil || !obj.Exported() {
continue
}
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module go.elastic.co/fastjson

require github.com/pkg/errors v0.8.0
go 1.14

require (
github.com/pkg/errors v0.8.0
golang.org/x/tools v0.0.0-20200509030707-2212a7e161a5
)
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200509030707-2212a7e161a5 h1:MeC2gMlMdkd67dn17MEby3rGXRxZtWeiRXOnISfTQ74=
golang.org/x/tools v0.0.0-20200509030707-2212a7e161a5/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 comments on commit dca77b8

Please sign in to comment.