Skip to content

Commit a5e71a9

Browse files
committed
Severity impl json.Marshaler/json.Unmarshaler
1 parent 739a1cc commit a5e71a9

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

processors/minsev/severity.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package minsev // import "go.opentelemetry.io/contrib/processors/minsev"
55

66
import (
77
"encoding"
8+
"encoding/json"
89
"errors"
910
"fmt"
1011
"strconv"
@@ -23,6 +24,10 @@ type Severity int
2324
var (
2425
// Ensure Severity implements fmt.Stringer.
2526
_ fmt.Stringer = Severity(0)
27+
// Ensure Severity implements json.Marshaler.
28+
_ json.Marshaler = Severity(0)
29+
// Ensure Severity implements json.Unmarshaler.
30+
_ json.Unmarshaler = (*Severity)(nil)
2631
// Ensure Severity implements encoding.TextMarshaler.
2732
_ encoding.TextMarshaler = Severity(0)
2833
// Ensure Severity implements encoding.TextUnmarshaler.
@@ -161,6 +166,26 @@ func (s Severity) String() string {
161166
}
162167
}
163168

169+
// MarshalJSON implements [encoding/json.Marshaler] by quoting the output of
170+
// [Severity.String].
171+
func (s Severity) MarshalJSON() ([]byte, error) {
172+
// AppendQuote is sufficient for JSON-encoding all Severity strings. They
173+
// don't contain any runes that would produce invalid JSON when escaped.
174+
return strconv.AppendQuote(nil, s.String()), nil
175+
}
176+
177+
// UnmarshalJSON implements [encoding/json.Unmarshaler] It accepts any string
178+
// produced by [Severity.MarshalJSON], ignoring case. It also accepts numeric
179+
// offsets that would result in a different string on output. For example,
180+
// "ERROR-8" will unmarshal as SeverityInfo.
181+
func (s *Severity) UnmarshalJSON(data []byte) error {
182+
str, err := strconv.Unquote(string(data))
183+
if err != nil {
184+
return err
185+
}
186+
return s.parse(str)
187+
}
188+
164189
// AppendText implements [encoding.TextAppender] by calling [Severity.String].
165190
func (s Severity) AppendText(b []byte) ([]byte, error) {
166191
return append(b, s.String()...), nil

processors/minsev/severity_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package minsev
55

66
import (
7+
"encoding/json"
78
"sync"
89
"testing"
910

@@ -157,6 +158,48 @@ func TestSeverityString(t *testing.T) {
157158
}
158159
}
159160

161+
func TestSeverityMarshalJSON(t *testing.T) {
162+
for _, test := range validEncodingTests {
163+
t.Run(test.Name, func(t *testing.T) {
164+
got, err := json.Marshal(test.Severity)
165+
require.NoError(t, err)
166+
assert.Equal(t, `"`+test.Text+`"`, string(got))
167+
})
168+
}
169+
}
170+
171+
func TestSeverityUnmarshalJSON(t *testing.T) {
172+
for _, test := range validDecodingTests {
173+
t.Run(test.Name, func(t *testing.T) {
174+
var sev Severity
175+
data := []byte(`"` + test.Text + `"`)
176+
require.NoError(t, sev.UnmarshalJSON(data))
177+
const msg = "UnmarshalJSON(%q) != %d (%[2]s)"
178+
assert.Equalf(t, test.Severity, sev, msg, data, test.Severity)
179+
})
180+
}
181+
}
182+
183+
func TestSeverityUnmarshalJSONError(t *testing.T) {
184+
invalidJSON := []string{
185+
`"UNKNOWN"`,
186+
`"DEBUG3+abc"`,
187+
`"INFO+abc"`,
188+
`"ERROR-xyz"`,
189+
`"not-a-level"`,
190+
`invalid-json`,
191+
`42`, // number instead of string
192+
}
193+
194+
for _, test := range invalidJSON {
195+
t.Run(test, func(t *testing.T) {
196+
var sev Severity
197+
err := sev.UnmarshalJSON([]byte(test))
198+
assert.Error(t, err)
199+
})
200+
}
201+
}
202+
160203
func TestSeverityMarshalText(t *testing.T) {
161204
for _, test := range validEncodingTests {
162205
t.Run(test.Name, func(t *testing.T) {

0 commit comments

Comments
 (0)