2022-06-09 15:47:05 +01:00
|
|
|
package aghalg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-01-24 16:50:19 +00:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/golibs/mathutil"
|
2022-06-09 15:47:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// NullBool is a nullable boolean. Use these in JSON requests and responses
|
|
|
|
// instead of pointers to bool.
|
|
|
|
type NullBool uint8
|
|
|
|
|
|
|
|
// NullBool values
|
|
|
|
const (
|
|
|
|
NBNull NullBool = iota
|
|
|
|
NBTrue
|
|
|
|
NBFalse
|
|
|
|
)
|
|
|
|
|
|
|
|
// String implements the fmt.Stringer interface for NullBool.
|
|
|
|
func (nb NullBool) String() (s string) {
|
|
|
|
switch nb {
|
|
|
|
case NBNull:
|
|
|
|
return "null"
|
|
|
|
case NBTrue:
|
|
|
|
return "true"
|
|
|
|
case NBFalse:
|
|
|
|
return "false"
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("!invalid NullBool %d", uint8(nb))
|
|
|
|
}
|
|
|
|
|
|
|
|
// BoolToNullBool converts a bool into a NullBool.
|
|
|
|
func BoolToNullBool(cond bool) (nb NullBool) {
|
2023-01-24 16:50:19 +00:00
|
|
|
return NBFalse - mathutil.BoolToNumber[NullBool](cond)
|
2022-06-09 15:47:05 +01:00
|
|
|
}
|
|
|
|
|
2022-07-11 15:40:00 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-06-09 15:47:05 +01:00
|
|
|
// type check
|
|
|
|
var _ json.Unmarshaler = (*NullBool)(nil)
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface for *NullBool.
|
|
|
|
func (nb *NullBool) UnmarshalJSON(b []byte) (err error) {
|
|
|
|
if len(b) == 0 || bytes.Equal(b, []byte("null")) {
|
|
|
|
*nb = NBNull
|
|
|
|
} else if bytes.Equal(b, []byte("true")) {
|
|
|
|
*nb = NBTrue
|
|
|
|
} else if bytes.Equal(b, []byte("false")) {
|
|
|
|
*nb = NBFalse
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("unmarshalling json data into aghalg.NullBool: bad value %q", b)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|