-
Notifications
You must be signed in to change notification settings - Fork 0
/
bool.go
142 lines (110 loc) · 2.49 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package reflector
import (
"errors"
"reflect"
)
type Boolean interface {
Type
BooleanValue() (bool, error)
SetBooleanValue(val bool) error
}
type booleanType struct {
parent Type
reflectType reflect.Type
reflectValue *reflect.Value
}
func (b *booleanType) Name() string {
return b.reflectType.Name()
}
func (b *booleanType) PackageName() string {
return ""
}
func (b *booleanType) PackagePath() string {
return ""
}
func (b *booleanType) CanSet() bool {
if b.reflectValue == nil {
return false
}
return b.reflectValue.CanSet()
}
func (b *booleanType) HasValue() bool {
return b.reflectValue != nil
}
func (b *booleanType) Value() (any, error) {
if b.reflectValue == nil {
return false, errors.New("value reference is nil")
}
return b.reflectValue.Interface(), nil
}
func (b *booleanType) SetValue(val any) error {
if !b.CanSet() {
return errors.New("value cannot be set")
}
switch typedVal := val.(type) {
case bool:
return b.SetBooleanValue(typedVal)
default:
return errors.New("type is not valid")
}
}
func (b *booleanType) Parent() Type {
return b.parent
}
func (b *booleanType) ReflectType() reflect.Type {
return b.reflectType
}
func (b *booleanType) ReflectValue() *reflect.Value {
return b.reflectValue
}
func (b *booleanType) Compare(another Type) bool {
if another == nil {
return false
}
return b.reflectType == another.ReflectType()
}
func (b *booleanType) IsInstantiable() bool {
return true
}
func (b *booleanType) Instantiate() (Value, error) {
return &value{
reflect.New(b.reflectType),
}, nil
}
func (b *booleanType) CanConvert(typ Type) bool {
if typ == nil {
return false
}
if b.reflectValue == nil {
return b.reflectType.ConvertibleTo(typ.ReflectType())
}
return b.reflectValue.CanConvert(typ.ReflectType())
}
func (b *booleanType) Convert(typ Type) (Value, error) {
if typ == nil {
return nil, errors.New("typ should not be nil")
}
if b.reflectValue == nil {
return nil, errors.New("value reference is nil")
}
if !b.CanConvert(typ) {
return nil, errors.New("type is not valid")
}
val := b.reflectValue.Convert(typ.ReflectType())
return &value{
val,
}, nil
}
func (b *booleanType) BooleanValue() (bool, error) {
if b.reflectValue == nil {
return false, errors.New("value reference is nil")
}
return b.reflectValue.Interface().(bool), nil
}
func (b *booleanType) SetBooleanValue(val bool) error {
if !b.CanSet() {
return errors.New("value cannot be set")
}
b.reflectValue.Set(reflect.ValueOf(val))
return nil
}