log/slog: implement encoding.TextAppender for Level and LevelVar

For #62384
This commit is contained in:
apocelipes 2024-08-13 21:35:41 +08:00
parent 7273509466
commit 5ad8e2e047
4 changed files with 49 additions and 4 deletions

View File

@ -3,3 +3,5 @@ pkg encoding, type BinaryAppender interface, AppendBinary([]uint8) ([]uint8, err
pkg encoding, type TextAppender interface { AppendText } #62384 pkg encoding, type TextAppender interface { AppendText } #62384
pkg encoding, type TextAppender interface, AppendText([]uint8) ([]uint8, error) #62384 pkg encoding, type TextAppender interface, AppendText([]uint8) ([]uint8, error) #62384
pkg net/url, method (*URL) AppendBinary([]uint8) ([]uint8, error) #62384 pkg net/url, method (*URL) AppendBinary([]uint8) ([]uint8, error) #62384
pkg log/slog, method (Level) AppendText([]uint8) ([]uint8, error) #62384
pkg log/slog, method (*LevelVar) AppendText([]uint8) ([]uint8, error) #62384

View File

@ -0,0 +1 @@
[Level] and [LevelVar] now implement the [encoding.TextAppender] interface.

View File

@ -98,10 +98,16 @@ func (l *Level) UnmarshalJSON(data []byte) error {
return l.parse(s) return l.parse(s)
} }
// MarshalText implements [encoding.TextMarshaler] // AppendText implements [encoding.TextAppender]
// by calling [Level.String]. // by calling [Level.String].
func (l Level) AppendText(b []byte) ([]byte, error) {
return append(b, l.String()...), nil
}
// MarshalText implements [encoding.TextMarshaler]
// by calling [Level.AppendText].
func (l Level) MarshalText() ([]byte, error) { func (l Level) MarshalText() ([]byte, error) {
return []byte(l.String()), nil return l.AppendText(nil)
} }
// UnmarshalText implements [encoding.TextUnmarshaler]. // UnmarshalText implements [encoding.TextUnmarshaler].
@ -172,10 +178,16 @@ func (v *LevelVar) String() string {
return fmt.Sprintf("LevelVar(%s)", v.Level()) return fmt.Sprintf("LevelVar(%s)", v.Level())
} }
// AppendText implements [encoding.TextAppender]
// by calling [Level.AppendText].
func (v *LevelVar) AppendText(b []byte) ([]byte, error) {
return v.Level().AppendText(b)
}
// MarshalText implements [encoding.TextMarshaler] // MarshalText implements [encoding.TextMarshaler]
// by calling [Level.MarshalText]. // by calling [LevelVar.AppendText].
func (v *LevelVar) MarshalText() ([]byte, error) { func (v *LevelVar) MarshalText() ([]byte, error) {
return v.Level().MarshalText() return v.AppendText(nil)
} }
// UnmarshalText implements [encoding.TextUnmarshaler] // UnmarshalText implements [encoding.TextUnmarshaler]

View File

@ -89,6 +89,19 @@ func TestLevelMarshalText(t *testing.T) {
} }
} }
func TestLevelAppendText(t *testing.T) {
buf := make([]byte, 4, 16)
want := LevelWarn - 3
wantData := []byte("\x00\x00\x00\x00INFO+1")
data, err := want.AppendText(buf)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, wantData) {
t.Errorf("got %s, want %s", string(data), string(wantData))
}
}
func TestLevelParse(t *testing.T) { func TestLevelParse(t *testing.T) {
for _, test := range []struct { for _, test := range []struct {
in string in string
@ -162,6 +175,23 @@ func TestLevelVarMarshalText(t *testing.T) {
} }
} }
func TestLevelVarAppendText(t *testing.T) {
var v LevelVar
v.Set(LevelWarn)
buf := make([]byte, 4, 16)
data, err := v.AppendText(buf)
if err != nil {
t.Fatal(err)
}
var v2 LevelVar
if err := v2.UnmarshalText(data[4:]); err != nil {
t.Fatal(err)
}
if g, w := v2.Level(), LevelWarn; g != w {
t.Errorf("got %s, want %s", g, w)
}
}
func TestLevelVarFlag(t *testing.T) { func TestLevelVarFlag(t *testing.T) {
fs := flag.NewFlagSet("test", flag.ContinueOnError) fs := flag.NewFlagSet("test", flag.ContinueOnError)
v := &LevelVar{} v := &LevelVar{}