Skip to content

run goimports on generated code, and handle unhandled error #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"path/filepath"
"regexp"
"sort"

"golang.org/x/tools/imports"
)

const genPackage = "github.com/mailru/easyjson/gen"
Expand Down Expand Up @@ -41,6 +43,8 @@ type Generator struct {
LeaveTemps bool
NoFormat bool
SimpleBytes bool

LocalPrefix *string
}

// writeStub outputs an initial stub for marshalers/unmarshalers so that the package
Expand Down Expand Up @@ -215,5 +219,17 @@ func (g *Generator) Run() error {
if err != nil {
return err
}
return ioutil.WriteFile(g.OutName, out, 0644)

if g.LocalPrefix != nil {
imports.LocalPrefix = *g.LocalPrefix
}
impOuts, err := imports.Process(g.OutName, out, &imports.Options{
FormatOnly: true,
Comments: true,
})
if err != nil {
return err
}

return ioutil.WriteFile(g.OutName, impOuts, 0644)
}
2 changes: 2 additions & 0 deletions easyjson/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var specifiedName = flag.String("output_filename", "", "specify the filename of
var processPkg = flag.Bool("pkg", false, "process the whole package instead of just the given file")
var disallowUnknownFields = flag.Bool("disallow_unknown_fields", false, "return error if any unknown field in json appeared")
var skipMemberNameUnescaping = flag.Bool("disable_members_unescape", false, "don't perform unescaping of member names to improve performance")
var localPrefix = flag.String("local_prefix", "", "local prefix to use for imports")

func generate(fname string) (err error) {
fInfo, err := os.Stat(fname)
Expand Down Expand Up @@ -85,6 +86,7 @@ func generate(fname string) (err error) {
StubsOnly: *stubs,
NoFormat: *noformat,
SimpleBytes: *simpleBytes,
LocalPrefix: localPrefix,
}

if err := g.Run(); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion gen/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field
fmt.Fprintln(g.out, ws+"if m, ok := "+out+".(easyjson.Unmarshaler); ok {")
fmt.Fprintln(g.out, ws+"m.UnmarshalEasyJSON(in)")
fmt.Fprintln(g.out, ws+"} else if m, ok := "+out+".(json.Unmarshaler); ok {")
fmt.Fprintln(g.out, ws+"_ = m.UnmarshalJSON(in.Raw())")
fmt.Fprintln(g.out, ws+"if err := m.UnmarshalJSON(in.Raw()); err != nil {")
fmt.Fprintln(g.out, ws+" in.AddError(err)")
fmt.Fprintln(g.out, ws+"}")
fmt.Fprintln(g.out, ws+"} else {")
fmt.Fprintln(g.out, ws+" "+out+" = in.Interface()")
fmt.Fprintln(g.out, ws+"}")
Expand Down
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
module github.com/mailru/easyjson

go 1.20
go 1.23.0

toolchain go1.23.4

require github.com/josharian/intern v1.0.0

require (
golang.org/x/mod v0.24.0 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/tools v0.31.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/tools v0.31.0 h1:0EedkvKDbh+qistFTd0Bcwe/YLh4vHwWEkiI0toFIBU=
golang.org/x/tools v0.31.0/go.mod h1:naFTU+Cev749tSJRXJlna0T3WxKvb1kWEx15xA4SdmQ=