-
Notifications
You must be signed in to change notification settings - Fork 6
/
bool.go
95 lines (84 loc) · 1.68 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
package ast
import (
"github.com/dipdup-net/go-lib/tools/base"
"github.com/dipdup-net/go-lib/tools/consts"
)
//
// BOOL
//
// Bool -
type Bool struct {
Default
}
// NewBool -
func NewBool(depth int) *Bool {
return &Bool{
Default: NewDefault(consts.BOOL, 0, depth),
}
}
// ParseValue -
func (b *Bool) ParseValue(node *base.Node) error {
switch node.Prim {
case consts.False:
b.Value = false
case consts.True:
b.Value = true
default:
return consts.ErrInvalidPrim
}
return nil
}
// ToBaseNode -
func (b *Bool) ToBaseNode(optimized bool) (*base.Node, error) {
val := b.Value.(bool)
if val {
return &base.Node{Prim: consts.True}, nil
}
return &base.Node{Prim: consts.False}, nil
}
// ToJSONSchema -
func (b *Bool) ToJSONSchema() (*JSONSchema, error) {
return wrapObject(&JSONSchema{
Prim: b.Prim,
Type: JSONSchemaTypeBool,
Default: false,
Title: b.GetName(),
}), nil
}
// Compare -
func (b *Bool) Compare(second Comparable) (int, error) {
secondItem, ok := second.(*Bool)
if !ok {
return 0, consts.ErrTypeIsNotComparable
}
switch {
case b.Value == secondItem.Value:
return 0, nil
case b.Value:
return 1, nil
default:
return -1, nil
}
}
// Distinguish -
func (b *Bool) Distinguish(x Distinguishable) (*MiguelNode, error) {
second, ok := x.(*Bool)
if !ok {
return nil, nil
}
return b.Default.Distinguish(&second.Default)
}
// ToParameters -
func (b *Bool) ToParameters() ([]byte, error) {
if v, ok := b.Value.(bool); ok && v {
return []byte(`{"prim":"True"}`), nil
}
return []byte(`{"prim":"False"}`), nil
}
// FindByName -
func (b *Bool) FindByName(name string, isEntrypoint bool) Node {
if b.GetName() == name {
return b
}
return nil
}