mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-22 04:55:33 +03:00
Pull request: aghalg: impl json.Marshaler for NullBool
Updates #4735. Squashed commit of the following: commit 93a0b1dc6b668f7d9fd89d06b8f0f24dcd345356 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Jul 11 17:02:36 2022 +0300 aghalg: impl json.Marshaler for NullBool
This commit is contained in:
parent
a832987f7c
commit
bf024fb985
2 changed files with 54 additions and 0 deletions
|
@ -40,6 +40,14 @@ func BoolToNullBool(cond bool) (nb NullBool) {
|
||||||
return NBFalse
|
return NBFalse
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// type check
|
||||||
|
var _ json.Marshaler = NBNull
|
||||||
|
|
||||||
|
// MarshalJSON implements the json.Marshaler interface for NullBool.
|
||||||
|
func (nb NullBool) MarshalJSON() (b []byte, err error) {
|
||||||
|
return []byte(nb.String()), nil
|
||||||
|
}
|
||||||
|
|
||||||
// type check
|
// type check
|
||||||
var _ json.Unmarshaler = (*NullBool)(nil)
|
var _ json.Unmarshaler = (*NullBool)(nil)
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,52 @@ import (
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestNullBool_MarshalJSON(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
wantErrMsg string
|
||||||
|
want []byte
|
||||||
|
in aghalg.NullBool
|
||||||
|
}{{
|
||||||
|
name: "null",
|
||||||
|
wantErrMsg: "",
|
||||||
|
want: []byte("null"),
|
||||||
|
in: aghalg.NBNull,
|
||||||
|
}, {
|
||||||
|
name: "true",
|
||||||
|
wantErrMsg: "",
|
||||||
|
want: []byte("true"),
|
||||||
|
in: aghalg.NBTrue,
|
||||||
|
}, {
|
||||||
|
name: "false",
|
||||||
|
wantErrMsg: "",
|
||||||
|
want: []byte("false"),
|
||||||
|
in: aghalg.NBFalse,
|
||||||
|
}}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got, err := tc.in.MarshalJSON()
|
||||||
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
||||||
|
|
||||||
|
assert.Equal(t, tc.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("json", func(t *testing.T) {
|
||||||
|
in := &struct {
|
||||||
|
A aghalg.NullBool
|
||||||
|
}{
|
||||||
|
A: aghalg.NBTrue,
|
||||||
|
}
|
||||||
|
|
||||||
|
got, err := json.Marshal(in)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, []byte(`{"A":true}`), got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestNullBool_UnmarshalJSON(t *testing.T) {
|
func TestNullBool_UnmarshalJSON(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
Loading…
Reference in a new issue