-
Notifications
You must be signed in to change notification settings - Fork 22
/
bool.go
87 lines (76 loc) · 1.45 KB
/
bool.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package scalar
import (
"strconv"
"github.com/apache/arrow/go/v16/arrow"
)
type Bool struct {
Valid bool
Value bool
}
func (s *Bool) IsValid() bool {
return s.Valid
}
func (*Bool) DataType() arrow.DataType {
return arrow.FixedWidthTypes.Boolean
}
func (s *Bool) Equal(rhs Scalar) bool {
if rhs == nil {
return false
}
r, ok := rhs.(*Bool)
if !ok {
return false
}
return s.Value == r.Value && s.Valid == r.Valid
}
func (s *Bool) String() string {
if !s.Valid {
return nullValueStr
}
return strconv.FormatBool(s.Value)
}
func (s *Bool) Get() any {
return s.Value
}
func (s *Bool) Set(val any) error {
if val == nil {
s.Valid = false
return nil
}
if sc, ok := val.(Scalar); ok {
if !sc.IsValid() {
s.Valid = false
return nil
}
return s.Set(sc.Get())
}
switch value := val.(type) {
case bool:
s.Value = value
case string:
bb, err := strconv.ParseBool(value)
if err != nil {
return &ValidationError{Type: arrow.FixedWidthTypes.Boolean, Msg: "failed to ParseBool", Value: value, Err: err}
}
s.Value = bb
case *bool:
if value == nil {
s.Valid = false
return nil
}
return s.Set(*value)
case *string:
if value == nil {
s.Valid = false
return nil
}
return s.Set(*value)
default:
if originalSrc, ok := underlyingBoolType(value); ok {
return s.Set(originalSrc)
}
return &ValidationError{Type: arrow.FixedWidthTypes.Boolean, Msg: noConversion, Value: val}
}
s.Valid = true
return nil
}