-
Notifications
You must be signed in to change notification settings - Fork 12
/
boolean.go
45 lines (36 loc) · 944 Bytes
/
boolean.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package adyen
import (
"strconv"
"strings"
)
// StringBool allows us to unmarhsal Adyen Boolean values
// which appear as strings instead of bools.
type StringBool bool
// NewStringBool returns an instance of StringBool representing a given bool
func NewStringBool(b bool) *StringBool {
sb := StringBool(b)
return &sb
}
// UnmarshalJSON unmarshalls to a StringBool from a slice of bytes
func (b *StringBool) UnmarshalJSON(data []byte) (err error) {
str := strings.TrimFunc(strings.ToLower(string(data)), func(c rune) bool {
return c == ' ' || c == '"'
})
parsed, err := strconv.ParseBool(str)
if err != nil {
return
}
*b = StringBool(parsed)
return
}
// MarshalJSON marshalls a StringBool to a slice of bytes
func (b StringBool) MarshalJSON() ([]byte, error) {
boolResult := bool(b)
var boolString string
if boolResult {
boolString = `"true"`
} else {
boolString = `"false"`
}
return []byte(boolString), nil
}