Implement encoding.TextUnmarshaler and encoding.TextMarshaler for Level (#440)
This commit is contained in:
parent
2a13872817
commit
60f57432ed
16
log.go
16
log.go
|
@ -99,6 +99,7 @@
|
|||
package zerolog
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
@ -189,6 +190,21 @@ func ParseLevel(levelStr string) (Level, error) {
|
|||
return Level(i), nil
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding.TextUnmarshaler to allow for easy reading from toml/yaml/json formats
|
||||
func (l *Level) UnmarshalText(text []byte) error {
|
||||
if l == nil {
|
||||
return errors.New("can't unmarshal a nil *Level")
|
||||
}
|
||||
var err error
|
||||
*l, err = ParseLevel(string(text))
|
||||
return err
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler to allow for easy writing into toml/yaml/json formats
|
||||
func (l Level) MarshalText() ([]byte, error) {
|
||||
return []byte(LevelFieldMarshalFunc(l)), nil
|
||||
}
|
||||
|
||||
// A Logger represents an active logging object that generates lines
|
||||
// of JSON output to an io.Writer. Each logging operation makes a single
|
||||
// call to the Writer's Write method. There is no guarantee on access
|
||||
|
|
65
log_test.go
65
log_test.go
|
@ -907,6 +907,33 @@ func TestLevel_String(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestLevel_MarshalText(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
l Level
|
||||
want string
|
||||
}{
|
||||
{"trace", TraceLevel, "trace"},
|
||||
{"debug", DebugLevel, "debug"},
|
||||
{"info", InfoLevel, "info"},
|
||||
{"warn", WarnLevel, "warn"},
|
||||
{"error", ErrorLevel, "error"},
|
||||
{"fatal", FatalLevel, "fatal"},
|
||||
{"panic", PanicLevel, "panic"},
|
||||
{"disabled", Disabled, "disabled"},
|
||||
{"nolevel", NoLevel, ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got, err := tt.l.MarshalText(); err != nil {
|
||||
t.Errorf("MarshalText couldn't marshal: %v", tt.l)
|
||||
} else if string(got) != tt.want {
|
||||
t.Errorf("String() = %v, want %v", string(got), tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseLevel(t *testing.T) {
|
||||
type args struct {
|
||||
levelStr string
|
||||
|
@ -943,3 +970,41 @@ func TestParseLevel(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalTextLevel(t *testing.T) {
|
||||
type args struct {
|
||||
levelStr string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want Level
|
||||
wantErr bool
|
||||
}{
|
||||
{"trace", args{"trace"}, TraceLevel, false},
|
||||
{"debug", args{"debug"}, DebugLevel, false},
|
||||
{"info", args{"info"}, InfoLevel, false},
|
||||
{"warn", args{"warn"}, WarnLevel, false},
|
||||
{"error", args{"error"}, ErrorLevel, false},
|
||||
{"fatal", args{"fatal"}, FatalLevel, false},
|
||||
{"panic", args{"panic"}, PanicLevel, false},
|
||||
{"disabled", args{"disabled"}, Disabled, false},
|
||||
{"nolevel", args{""}, NoLevel, false},
|
||||
{"-1", args{"-1"}, TraceLevel, false},
|
||||
{"-2", args{"-2"}, Level(-2), false},
|
||||
{"-3", args{"-3"}, Level(-3), false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var l Level
|
||||
err := l.UnmarshalText([]byte(tt.args.levelStr))
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("UnmarshalText() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if l != tt.want {
|
||||
t.Errorf("UnmarshalText() got = %v, want %v", l, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue