Skip to content

Commit df2df99

Browse files
authored
Add support for slog.Level fields (#37)
Also * Bump to 1.21 for slog support
1 parent 07e1256 commit df2df99

File tree

7 files changed

+81
-4
lines changed

7 files changed

+81
-4
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Set up Go
1818
uses: actions/setup-go@v5
1919
with:
20-
go-version: '1.19'
20+
go-version-file: go.mod
2121

2222
- name: Test
2323
run: go test -v ./...

additional.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package flagsfiller
2+
3+
import (
4+
"log/slog"
5+
"reflect"
6+
)
7+
8+
func init() {
9+
RegisterSimpleType(slogLevelConverter)
10+
}
11+
12+
func slogLevelConverter(s string, _ reflect.StructTag) (slog.Level, error) {
13+
var level slog.Level
14+
err := level.UnmarshalText([]byte(s))
15+
if err != nil {
16+
return slog.LevelInfo, err
17+
}
18+
return level, nil
19+
}

addtional_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package flagsfiller_test
22

33
import (
44
"flag"
5+
"log/slog"
56
"net"
67
"net/netip"
78
"testing"
@@ -117,3 +118,59 @@ func TestTextUnmarshalerType(t *testing.T) {
117118

118119
assert.Equal(t, netip.AddrFrom4([4]byte{1, 2, 3, 4}), config.Addr)
119120
}
121+
122+
func TestSlogLevels(t *testing.T) {
123+
tests := []struct {
124+
name string
125+
value string
126+
expected slog.Level
127+
}{
128+
{
129+
name: "info",
130+
value: "info",
131+
expected: slog.LevelInfo,
132+
},
133+
{
134+
name: "error",
135+
value: "error",
136+
expected: slog.LevelError,
137+
},
138+
{
139+
name: "numeric offset",
140+
// Borrowed from https://pkg.go.dev/log/slog#Level.UnmarshalText
141+
value: "Error-8",
142+
expected: slog.LevelInfo,
143+
},
144+
}
145+
for _, test := range tests {
146+
t.Run(test.name, func(t *testing.T) {
147+
var args struct {
148+
Level slog.Level
149+
}
150+
151+
var flagset flag.FlagSet
152+
err := flagsfiller.New().Fill(&flagset, &args)
153+
require.NoError(t, err)
154+
155+
err = flagset.Parse([]string{"--level", test.value})
156+
require.NoError(t, err)
157+
158+
assert.Equal(t, test.expected, args.Level)
159+
})
160+
}
161+
}
162+
163+
func TestSlogLevelWithDefault(t *testing.T) {
164+
var args struct {
165+
Level slog.Level `default:"info"`
166+
}
167+
168+
var flagset flag.FlagSet
169+
err := flagsfiller.New().Fill(&flagset, &args)
170+
require.NoError(t, err)
171+
172+
err = flagset.Parse([]string{})
173+
require.NoError(t, err)
174+
175+
assert.Equal(t, slog.LevelInfo, args.Level)
176+
}

docs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ FlagSetFiller also supports following field types:
146146
- net.IPNet: format used by net.ParseCIDR()
147147
- net.HardwareAddr (MAC addr): format used by net.ParseMAC()
148148
- time.Time: format is the layout string used by time.Parse(), default layout is time.DateTime, could be overriden by field tag "layout"
149+
- slog.Level: parsed as specified by https://pkg.go.dev/log/slog#Level.UnmarshalText, such as "info"
149150
150151
# Environment variable mapping
151152

flagset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (f *FlagSetFiller) processField(flagSet *flag.FlagSet, fieldRef interface{}
200200
if isSupportedStruct(fieldRef) {
201201
handler := extendedTypes[getTypeName(t)]
202202
err = handler(tag, fieldRef, hasDefaultTag, tagDefault, flagSet, renamed, usage, aliases)
203-
203+
return err
204204
}
205205

206206
switch {

general.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"strings"
1212
)
1313

14-
// this is a list of addtional supported types(include struct), like time.Time, that walkFields() won't walk into,
14+
// this is a list of additional supported types(include struct), like time.Time, that walkFields() won't walk into,
1515
// the key is the is string returned by the getTypeName(<type>),
1616
// each supported type need to be added in this map in init()
1717
var extendedTypes = make(map[string]handlerFunc)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/itzg/go-flagsfiller
22

3-
go 1.19
3+
go 1.21
44

55
require (
66
github.com/iancoleman/strcase v0.3.0

0 commit comments

Comments
 (0)