Skip to content

Commit

Permalink
cmd/fillstruct: handle build tags (#52)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Billie <mbillie@digitalocean.com>
  • Loading branch information
yaaase and Mark Billie committed Feb 13, 2021
1 parent 01cf781 commit 40322ff
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
31 changes: 31 additions & 0 deletions cmd/fillstruct/fill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,37 @@ import "unsafe"
var s = myStruct{}
type myStruct struct {
a int
b bool
c complex64
d uint16
f float32
g string
h uintptr
i unsafe.Pointer
}`,
want: `myStruct{
a: 0,
b: false,
c: (0 + 0i),
d: 0,
f: 0.0,
g: "",
h: uintptr(0),
i: unsafe.Pointer(uintptr(0)),
}`,
},
{
name: "basic types with tags",
src: `// +build foo
package p
import "unsafe"
var s = myStruct{}
type myStruct struct {
a int
b bool
Expand Down
16 changes: 10 additions & 6 deletions cmd/fillstruct/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import (
"log"
"os"
"path/filepath"
"strings"

"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/buildutil"
Expand All @@ -90,7 +91,9 @@ func main() {
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")
line = flag.Int("line", 0, "line number of the struct literal, optional if -offset is present")
btags buildutil.TagsFlag
)
flag.Var(&btags, "tags", buildutil.TagsFlagDoc)
flag.Parse()

if (*offset == 0 && *line == 0) || *filename == "" {
Expand All @@ -112,12 +115,13 @@ func main() {
}

cfg := &packages.Config{
Overlay: overlay,
Mode: packages.LoadAllSyntax,
Tests: true,
Dir: filepath.Dir(path),
Fset: token.NewFileSet(),
Env: os.Environ(),
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)
Expand Down

0 comments on commit 40322ff

Please sign in to comment.