-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbool.go
More file actions
41 lines (33 loc) · 808 Bytes
/
Copy pathbool.go
File metadata and controls
41 lines (33 loc) · 808 Bytes
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
package proxyclient
import (
"encoding/json"
"fmt"
"strings"
)
type Bool struct {
v bool
}
func (i *Bool) MarshalJSON() ([]byte, error) {
return json.Marshal(i.v)
}
func (i *Bool) UnmarshalJSON(data []byte) error {
// First try to unmarshal as an integer directly
var v bool
if err := json.Unmarshal(data, &v); err == nil {
*i = Bool{v: v}
return nil
}
// If that fails, try to unmarshal as a string
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("value must be a boolean or a string representation of a boolean: %w", err)
}
s = strings.ToLower(s)
v = s == "true" || s == "1" || s == "yes" || s == "on" || s == "y" || s == "t"
*i = Bool{v: v}
return nil
}
// Add a getter method to retrieve the value
func (i Bool) Value() bool {
return i.v
}