/
bool.go
71 lines (65 loc) · 1.35 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
package value
import (
"fmt"
"reflect"
)
// Bool represents a Kusto boolean type. Bool implements Kusto.
type Bool struct {
// Value holds the value of the type.
Value bool
// Valid indicates if this value was set.
Valid bool
}
func (Bool) isKustoVal() {}
// String implements fmt.Stringer.
func (bo Bool) String() string {
if !bo.Valid {
return ""
}
if bo.Value {
return "true"
}
return "false"
}
// Unmarshal unmarshals i into Bool. i must be a bool or nil.
func (bo *Bool) Unmarshal(i interface{}) error {
if i == nil {
bo.Value = false
bo.Valid = false
return nil
}
v, ok := i.(bool)
if !ok {
return fmt.Errorf("Column with type 'bool' had value that was %T", i)
}
bo.Value = v
bo.Valid = true
return nil
}
// Convert Bool into reflect value.
func (bo Bool) Convert(v reflect.Value) error {
t := v.Type()
switch {
case t.Kind() == reflect.Bool:
if bo.Valid {
v.SetBool(bo.Value)
}
return nil
case t.ConvertibleTo(reflect.TypeOf(new(bool))):
if bo.Valid {
b := new(bool)
if bo.Value {
*b = true
}
v.Set(reflect.ValueOf(b))
}
return nil
case t.ConvertibleTo(reflect.TypeOf(Bool{})):
v.Set(reflect.ValueOf(bo))
return nil
case t.ConvertibleTo(reflect.TypeOf(&Bool{})):
v.Set(reflect.ValueOf(&bo))
return nil
}
return fmt.Errorf("Column was type Kusto.Bool, receiver had base Kind %s ", t.Kind())
}