From 516731f92552b0c7e59457010996c6a741e9fce7 Mon Sep 17 00:00:00 2001 From: andrey Date: Mon, 1 Jan 2024 16:47:03 +0300 Subject: [PATCH] implement BinaryMarshaler and BinaryUnmarshaler --- level/level.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/level/level.go b/level/level.go index efa4ace..883aa7d 100644 --- a/level/level.go +++ b/level/level.go @@ -1,6 +1,7 @@ package level import ( + "encoding" "encoding/json" "strings" ) @@ -8,8 +9,12 @@ import ( //go:generate stringer -type=Level -linecomment var ( - _ json.Marshaler = Level(0) - _ json.Unmarshaler = (*Level)(nil) + _ json.Marshaler = Level(0) + _ json.Unmarshaler = (*Level)(nil) + _ encoding.TextMarshaler = Level(0) + _ encoding.TextUnmarshaler = (*Level)(nil) + _ encoding.BinaryMarshaler = Level(0) + _ encoding.BinaryUnmarshaler = (*Level)(nil) ) // Level log. @@ -57,6 +62,17 @@ func (l *Level) UnmarshalText(in []byte) error { return nil } +func (l Level) MarshalBinary() ([]byte, error) { + return []byte(l.String()), nil +} + +func (l *Level) UnmarshalBinary(in []byte) error { + lvl := Parse(string(in)) + *l = lvl + + return nil +} + func Parse(lvl string) Level { switch strings.ToLower(lvl) { case "debug", "Debug", "DEBUG":