-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
173 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"sort" | ||
"strings" | ||
"text/template" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
var tmpl = template.Must(template.New("").Funcs(funcs).Parse( | ||
`// Code generated by go-simpler.org/slog-gen. DO NOT EDIT. | ||
package {{.Pkg}} | ||
{{range .Imports -}} | ||
import "{{.}}" | ||
{{end -}} | ||
{{range $_, $attr := .Attrs}} | ||
func {{snakeToCamel $attr.Key}}(value {{$attr.Type}}) slog.Attr { | ||
return slog.{{slogFunc $attr.Type}}("{{$attr.Key}}", value) | ||
} | ||
{{end}}`, | ||
)) | ||
|
||
type ( | ||
config struct { | ||
Pkg string | ||
Imports []string | ||
Attrs []attr | ||
} | ||
attr struct { | ||
Key string | ||
Type string | ||
} | ||
) | ||
|
||
func readConfig(r io.Reader) (config, error) { | ||
var cfg struct { | ||
Pkg string `yaml:"pkg"` | ||
Imports []string `yaml:"imports"` | ||
Attrs map[string]string `yaml:"attrs"` // key:type | ||
} | ||
if err := yaml.NewDecoder(r).Decode(&cfg); err != nil { | ||
return config{}, fmt.Errorf("decoding config: %w", err) | ||
} | ||
|
||
cfg.Imports = append(cfg.Imports, "log/slog") | ||
sort.Strings(cfg.Imports) | ||
|
||
// TODO: rewrite when maps.Keys() is released. | ||
keys := make([]string, 0, len(cfg.Attrs)) | ||
for key := range cfg.Attrs { | ||
keys = append(keys, key) | ||
} | ||
sort.Strings(keys) | ||
|
||
attrs := make([]attr, len(keys)) | ||
for i, key := range keys { | ||
attrs[i] = attr{Key: key, Type: cfg.Attrs[key]} | ||
} | ||
|
||
return config{ | ||
Pkg: cfg.Pkg, | ||
Imports: cfg.Imports, | ||
Attrs: attrs, | ||
}, nil | ||
} | ||
|
||
// nolint:staticcheck // SA1019: strings.Title is deprecated but works just fine here. | ||
var funcs = template.FuncMap{ | ||
"snakeToCamel": func(s string) string { | ||
parts := strings.Split(s, "_") | ||
for i := range parts { | ||
parts[i] = strings.Title(parts[i]) | ||
} | ||
return strings.Join(parts, "") | ||
}, | ||
"slogFunc": func(typ string) string { | ||
switch s := strings.Title(strings.TrimPrefix(typ, "time.")); s { | ||
case "String", "Int64", "Int", "Uint64", "Float64", "Bool", "Time", "Duration": | ||
return s | ||
default: | ||
return "Any" | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"go-simpler.org/assert" | ||
. "go-simpler.org/assert/dotimport" | ||
) | ||
|
||
func Test_tmplExecute(t *testing.T) { | ||
cfg := config{ | ||
Pkg: "test", | ||
Imports: []string{"log/slog"}, | ||
Attrs: []attr{ | ||
{Key: "foo", Type: "int"}, | ||
{Key: "bar", Type: "error"}, | ||
}, | ||
} | ||
|
||
const codegen = `// Code generated by go-simpler.org/slog-gen. DO NOT EDIT. | ||
package test | ||
import "log/slog" | ||
func Foo(value int) slog.Attr { | ||
return slog.Int("foo", value) | ||
} | ||
func Bar(value error) slog.Attr { | ||
return slog.Any("bar", value) | ||
} | ||
` | ||
|
||
var buf bytes.Buffer | ||
err := tmpl.Execute(&buf, cfg) | ||
assert.NoErr[F](t, err) | ||
assert.Equal[E](t, buf.String(), codegen) | ||
} | ||
|
||
func Test_readConfig(t *testing.T) { | ||
r := strings.NewReader(` | ||
pkg: test | ||
imports: | ||
- time | ||
attrs: | ||
foo: time.Time | ||
bar: time.Duration | ||
`) | ||
|
||
want := config{ | ||
Pkg: "test", | ||
Imports: []string{"log/slog", "time"}, | ||
Attrs: []attr{ | ||
{Key: "bar", Type: "time.Duration"}, | ||
{Key: "foo", Type: "time.Time"}, | ||
}, | ||
} | ||
|
||
got, err := readConfig(r) | ||
assert.NoErr[F](t, err) | ||
assert.Equal[E](t, got, want) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters