mirror of
https://github.com/rclone/rclone.git
synced 2024-11-24 11:12:23 +08:00
fs: fix tristate conversion to JSON
This commit is contained in:
parent
bad8a01850
commit
5f4d7154c0
|
@ -70,3 +70,11 @@ func (t *Tristate) UnmarshalJSON(in []byte) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON encodes it as a bool or nil for unset
|
||||
func (t *Tristate) MarshalJSON() ([]byte, error) {
|
||||
if !t.Valid {
|
||||
return json.Marshal(nil)
|
||||
}
|
||||
return json.Marshal(t.Value)
|
||||
}
|
||||
|
|
|
@ -85,3 +85,18 @@ func TestTristateUnmarshalJSON(t *testing.T) {
|
|||
assert.Equal(t, test.want, got, test.in)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTristateMarshalJSON(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
in Tristate
|
||||
want string
|
||||
}{
|
||||
{Tristate{}, `null`},
|
||||
{Tristate{Valid: true, Value: true}, `true`},
|
||||
{Tristate{Valid: true, Value: false}, `false`},
|
||||
} {
|
||||
got, err := json.Marshal(&test.in)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, test.want, string(got), fmt.Sprintf("%#v", test.in))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user