mirror of
https://github.com/Icinga/icingadb.git
synced 2026-06-03 14:00:11 -04:00
Merge pull request #612 from Icinga/bool-binary-unixmilli-marshaljson-return-valid-json-not-empty-string
{Bool,Binary,UnixMilli}#MarshalJSON(): return valid JSON, not empty string
This commit is contained in:
commit
ef09059549
6 changed files with 92 additions and 3 deletions
|
|
@ -65,7 +65,7 @@ func (binary *Binary) UnmarshalText(text []byte) error {
|
|||
// Supports JSON null.
|
||||
func (binary Binary) MarshalJSON() ([]byte, error) {
|
||||
if !binary.Valid() {
|
||||
return nil, nil
|
||||
return []byte("null"), nil
|
||||
}
|
||||
|
||||
return internal.MarshalJSON(binary.String())
|
||||
|
|
|
|||
29
pkg/types/binary_test.go
Normal file
29
pkg/types/binary_test.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func TestBinary_MarshalJSON(t *testing.T) {
|
||||
subtests := []struct {
|
||||
name string
|
||||
input Binary
|
||||
output string
|
||||
}{
|
||||
{"nil", nil, `null`},
|
||||
{"empty", make(Binary, 0, 1), `null`},
|
||||
{"space", Binary(" "), `"20"`},
|
||||
}
|
||||
|
||||
for _, st := range subtests {
|
||||
t.Run(st.name, func(t *testing.T) {
|
||||
actual, err := st.input.MarshalJSON()
|
||||
|
||||
require.NoError(t, err)
|
||||
require.True(t, utf8.Valid(actual))
|
||||
require.Equal(t, st.output, string(actual))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -26,7 +26,7 @@ type Bool struct {
|
|||
// MarshalJSON implements the json.Marshaler interface.
|
||||
func (b Bool) MarshalJSON() ([]byte, error) {
|
||||
if !b.Valid {
|
||||
return nil, nil
|
||||
return []byte("null"), nil
|
||||
}
|
||||
|
||||
return internal.MarshalJSON(b.Bool)
|
||||
|
|
|
|||
30
pkg/types/bool_test.go
Normal file
30
pkg/types/bool_test.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func TestBool_MarshalJSON(t *testing.T) {
|
||||
subtests := []struct {
|
||||
input Bool
|
||||
output string
|
||||
}{
|
||||
{Bool{Bool: false, Valid: false}, `null`},
|
||||
{Bool{Bool: false, Valid: true}, `false`},
|
||||
{Bool{Bool: true, Valid: false}, `null`},
|
||||
{Bool{Bool: true, Valid: true}, `true`},
|
||||
}
|
||||
|
||||
for _, st := range subtests {
|
||||
t.Run(fmt.Sprintf("Bool-%#v_Valid-%#v", st.input.Bool, st.input.Valid), func(t *testing.T) {
|
||||
actual, err := st.input.MarshalJSON()
|
||||
|
||||
require.NoError(t, err)
|
||||
require.True(t, utf8.Valid(actual))
|
||||
require.Equal(t, st.output, string(actual))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -24,7 +24,7 @@ func (t UnixMilli) Time() time.Time {
|
|||
// Marshals to milliseconds. Supports JSON null.
|
||||
func (t UnixMilli) MarshalJSON() ([]byte, error) {
|
||||
if time.Time(t).IsZero() {
|
||||
return nil, nil
|
||||
return []byte("null"), nil
|
||||
}
|
||||
|
||||
return []byte(strconv.FormatInt(time.Time(t).UnixMilli(), 10)), nil
|
||||
|
|
|
|||
30
pkg/types/unix_milli_test.go
Normal file
30
pkg/types/unix_milli_test.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func TestUnixMilli_MarshalJSON(t *testing.T) {
|
||||
subtests := []struct {
|
||||
name string
|
||||
input UnixMilli
|
||||
output string
|
||||
}{
|
||||
{"zero", UnixMilli{}, `null`},
|
||||
{"epoch", UnixMilli(time.Unix(0, 0)), `0`},
|
||||
{"nonzero", UnixMilli(time.Unix(1234567890, 62500000)), `1234567890062`},
|
||||
}
|
||||
|
||||
for _, st := range subtests {
|
||||
t.Run(st.name, func(t *testing.T) {
|
||||
actual, err := st.input.MarshalJSON()
|
||||
|
||||
require.NoError(t, err)
|
||||
require.True(t, utf8.Valid(actual))
|
||||
require.Equal(t, st.output, string(actual))
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue