From 312efd90ff4e6005f05bd43f41a12e883ba21a5a Mon Sep 17 00:00:00 2001 From: andrey Date: Mon, 1 Jan 2024 16:36:06 +0300 Subject: [PATCH 1/2] update level - add TextUnmarshaler - add TextMarshaler - update json.Marshaler - update json.Unmarshaler - update string name Emergency and Critical - add test json Unmarshaler and Marshaler --- level/level.go | 40 ++++++++++++++--------------- level/level_string.go | 4 +-- level/level_test.go | 59 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 22 deletions(-) create mode 100644 level/level_test.go diff --git a/level/level.go b/level/level.go index 975fa2f..efa4ace 100644 --- a/level/level.go +++ b/level/level.go @@ -2,7 +2,6 @@ package level import ( "encoding/json" - "fmt" "strings" ) @@ -18,9 +17,9 @@ type Level uint32 // available log levels. const ( - Emergency Level = iota // emergency + Emergency Level = iota // emerg Alert // alert - Critical // critical + Critical // crit Error // error Warning // warning Notice // notice @@ -28,15 +27,6 @@ const ( Debug // debug ) -func (l Level) MarshalJSON() ([]byte, error) { - b, err := json.Marshal(l.String()) - if err != nil { - return nil, fmt.Errorf("marshal err: %w", err) - } - - return b, nil -} - func (l Level) Is(level Level) bool { return level == l } @@ -45,13 +35,23 @@ func (l Level) Enabled(level Level) bool { return l <= level } +func (l Level) MarshalJSON() ([]byte, error) { + return []byte("\"" + l.String() + "\""), nil +} + func (l *Level) UnmarshalJSON(in []byte) error { - var v string - if err := json.Unmarshal(in, &v); err != nil { - return fmt.Errorf("unmarshal err: %w", err) - } + lvl := Parse(string(in[1 : len(in)-1])) + *l = lvl + + return nil +} + +func (l Level) MarshalText() ([]byte, error) { + return []byte(l.String()), nil +} - lvl := Parse(v) +func (l *Level) UnmarshalText(in []byte) error { + lvl := Parse(string(in)) *l = lvl return nil @@ -65,11 +65,11 @@ func Parse(lvl string) Level { return Info case "notice", "Notice", "NOTICE": return Notice - case "warning", "Warning", "WARNING": + case "warning", "Warning", "WARNING", "warm", "Warm", "WARN": return Warning - case "error", "Error", "ERROR": + case "error", "Error", "ERROR", "err", "Err", "ERR": return Error - case "critical", "Critical", "CRITICAL": + case "critical", "Critical", "CRITICAL", "crit", "Crit", "CRIT": return Critical case "alert", "Alert", "ALERT": return Alert diff --git a/level/level_string.go b/level/level_string.go index e384efa..128a46f 100644 --- a/level/level_string.go +++ b/level/level_string.go @@ -18,9 +18,9 @@ func _() { _ = x[Debug-7] } -const _Level_name = "emergencyalertcriticalerrorwarningnoticeinfodebug" +const _Level_name = "emergalertcriterrorwarningnoticeinfodebug" -var _Level_index = [...]uint8{0, 9, 14, 22, 27, 34, 40, 44, 49} +var _Level_index = [...]uint8{0, 5, 10, 14, 19, 26, 32, 36, 41} func (i Level) String() string { if i >= Level(len(_Level_index)-1) { diff --git a/level/level_test.go b/level/level_test.go new file mode 100644 index 0000000..d6b7b23 --- /dev/null +++ b/level/level_test.go @@ -0,0 +1,59 @@ +package level_test + +import ( + "testing" + + "gitoa.ru/go-4devs/log/level" +) + +func TestMarshalJSON(t *testing.T) { + levels := map[level.Level]string{ + level.Emergency: `"emerg"`, + level.Alert: `"alert"`, + level.Critical: `"crit"`, + level.Error: `"error"`, + level.Warning: `"warning"`, + level.Notice: `"notice"`, + level.Info: `"info"`, + level.Debug: `"debug"`, + } + + for level, expect := range levels { + actual, err := level.MarshalJSON() + if err != nil { + t.Errorf("%s got err: %s", level, err) + continue + } + if string(actual) != expect { + t.Errorf("%s got: %s expect: %s", level, actual, expect) + } + } +} + +func TestUnmarshalJSON(t *testing.T) { + levels := map[level.Level][]string{ + level.Emergency: {`"emerg"`, `"Emerg"`}, + level.Alert: {`"alert"`, `"ALERT"`}, + level.Critical: {`"crit"`, `"critical"`}, + level.Error: {`"error"`, `"ERR"`}, + level.Warning: {`"warning"`, `"Warning"`}, + level.Notice: {`"notice"`}, + level.Info: {`"info"`}, + level.Debug: {`"debug"`, `"DEBUG"`}, + } + + for expect, actuals := range levels { + for _, actual := range actuals { + var level level.Level + if err := level.UnmarshalJSON([]byte(actual)); err != nil { + t.Errorf("%s got err: %s", level, err) + continue + } + + if !level.Is(expect) { + t.Errorf("%s got: %s expect: %s", actual, level, expect) + } + + } + } +} -- 2.30.2 From 516731f92552b0c7e59457010996c6a741e9fce7 Mon Sep 17 00:00:00 2001 From: andrey Date: Mon, 1 Jan 2024 16:47:03 +0300 Subject: [PATCH 2/2] 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": -- 2.30.2