Skip to content

Commit

Permalink
feat: use goimports to format generated code (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane committed Oct 21, 2023
1 parent 7886151 commit 07ec3cb
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![goreportcard](https://goreportcard.com/badge/go-simpler.org/sloggen)](https://goreportcard.com/report/go-simpler.org/sloggen)
[![codecov](https://codecov.io/gh/go-simpler/sloggen/branch/main/graph/badge.svg)](https://codecov.io/gh/go-simpler/sloggen)

Generate domain-specific helpers for `log/slog`.
Generate various helpers for `log/slog`.

## 📌 About

Expand Down
14 changes: 8 additions & 6 deletions example/example.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ go 1.21
require (
go-simpler.org/assert v0.7.0
go-simpler.org/errorsx v0.8.0
golang.org/x/tools v0.14.0
gopkg.in/yaml.v3 v3.0.1
)

require (
golang.org/x/mod v0.13.0 // indirect
golang.org/x/sys v0.13.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ go-simpler.org/assert v0.7.0 h1:OzWWZqfNxt8cLS+MlUp6Tgk1HjPkmgdKBq9qvy8lZsA=
go-simpler.org/assert v0.7.0/go.mod h1:74Eqh5eI6vCK6Y5l3PI8ZYFXG4Sa+tkr70OIPJAUr28=
go-simpler.org/errorsx v0.8.0 h1:grPV5R4OmfZ7bKiZ/X+VKyOOlgPFK0N2AP6TTz+0gCY=
go-simpler.org/errorsx v0.8.0/go.mod h1:Mh3dw+eRSRHll1apcEmXjSps8K0fAWMaNlGWEnOaiNU=
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
4 changes: 2 additions & 2 deletions sloggen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
_ "embed"
"flag"
"fmt"
"go/format"
"io"
"log/slog"
"slices"
"strconv"
"strings"
"text/template"

"golang.org/x/tools/imports"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -221,7 +221,7 @@ func writeCode(w io.Writer, cfg *config) error {
if err := tmpl.Execute(&buf, cfg); err != nil {
return fmt.Errorf("executing template: %w", err)
}
src, err := format.Source(buf.Bytes())
src, err := imports.Process("", buf.Bytes(), nil)
if err != nil {
return fmt.Errorf("formatting code: %w", err)
}
Expand Down
16 changes: 9 additions & 7 deletions sloggen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ func Test_writeCode(t *testing.T) {
package test
import "fmt"
import "log/slog"
import "strings"
import "time"
import (
"fmt"
"log/slog"
"strings"
"time"
)
const LevelCustom = slog.Level(1)
Expand Down Expand Up @@ -136,8 +138,8 @@ func TestExample(t *testing.T) {
logger.Alert(ctx, "bar")
logger.Log(ctx, level, "baz")
assert.Equal[E](t, "\n"+buf.String(), `
level=INFO source=sloggen_test.go:135 msg=foo group.key=value
level=ALERT source=sloggen_test.go:136 msg=bar group.key=value
level=ALERT source=sloggen_test.go:137 msg=baz group.key=value
level=INFO source=sloggen_test.go:137 msg=foo group.key=value
level=ALERT source=sloggen_test.go:138 msg=bar group.key=value
level=ALERT source=sloggen_test.go:139 msg=baz group.key=value
`)
}

0 comments on commit 07ec3cb

Please sign in to comment.