-
Notifications
You must be signed in to change notification settings - Fork 0
/
valueCheck.go
194 lines (171 loc) · 4.84 KB
/
valueCheck.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package scenmodel
import (
"bytes"
"math/big"
oj "github.com/multiversx/mx-chain-scenario-go/orderedjson"
)
// JSONCheckBytes holds a byte slice condition.
// Values are checked for equality.
// "*" allows all values.
type JSONCheckBytes struct {
Value []byte
IsStar bool
Original oj.OJsonObject
Unspecified bool
}
// JSONCheckBytesUnspecified yields JSONCheckBytes that check that value is empty.
func JSONCheckBytesUnspecified() JSONCheckBytes {
return JSONCheckBytes{
Value: []byte{},
IsStar: false,
Original: &oj.OJsonString{Value: ""},
Unspecified: true,
}
}
// JSONCheckBytesStar yields JSONCheckBytes explicit "*" value.
func JSONCheckBytesStar() JSONCheckBytes {
return JSONCheckBytes{
Value: []byte{},
IsStar: true,
Original: &oj.OJsonString{Value: "*"},
Unspecified: false,
}
}
// JSONCheckBytesReconstructed creates a JSONCheckBytes without an original JSON source.
func JSONCheckBytesReconstructed(value []byte, originalString string) JSONCheckBytes {
return JSONCheckBytes{
Value: value,
IsStar: false,
Original: &oj.OJsonString{Value: originalString},
Unspecified: false,
}
}
// OriginalEmpty returns true if original = "".
func (jcbytes JSONCheckBytes) OriginalEmpty() bool {
if str, isStr := jcbytes.Original.(*oj.OJsonString); isStr {
return len(str.Value) == 0
}
return false
}
// IsUnspecified yields true if the field was originally unspecified.
func (jcbytes JSONCheckBytes) IsUnspecified() bool {
return jcbytes.Unspecified
}
// Check returns true if condition expressed in object holds for another value.
// Explicit values are interpreted as equals assertion.
func (jcbytes JSONCheckBytes) Check(other []byte) bool {
if jcbytes.IsStar {
return true
}
return bytes.Equal(jcbytes.Value, other)
}
// JSONCheckBigInt holds a big int condition.
// Values are checked for equality.
// "*" allows all values.
type JSONCheckBigInt struct {
Value *big.Int
IsStar bool
Original string
Unspecified bool
}
// JSONCheckBigIntUnspecified yields JSONCheckBigInt default "*" value.
func JSONCheckBigIntUnspecified() JSONCheckBigInt {
return JSONCheckBigInt{
Value: big.NewInt(0),
IsStar: false,
Original: "",
Unspecified: true,
}
}
// IsUnspecified yields true if the field was originally unspecified.
func (jcbi JSONCheckBigInt) IsUnspecified() bool {
return jcbi.Unspecified
}
// Check returns true if condition expressed in object holds for another value.
// Explicit values are interpreted as equals assertion.
func (jcbi JSONCheckBigInt) Check(other *big.Int) bool {
if jcbi.IsStar {
return true
}
return jcbi.Value.Cmp(other) == 0
}
// JSONCheckUint64 holds a uint64 condition.
// Values are checked for equality.
// "*" allows all values.
type JSONCheckUint64 struct {
Value uint64
IsStar bool
Original string
Unspecified bool
}
// JSONCheckUint64Unspecified yields JSONCheckBigInt default "*" value.
func JSONCheckUint64Unspecified() JSONCheckUint64 {
return JSONCheckUint64{
Value: 0,
IsStar: false,
Original: "",
Unspecified: true,
}
}
// IsUnspecified yields true if the field was originally unspecified.
func (jcu JSONCheckUint64) IsUnspecified() bool {
return jcu.Unspecified
}
// Check returns true if condition expressed in object holds for another value.
// Explicit values are interpreted as equals assertion.
func (jcu JSONCheckUint64) Check(other uint64) bool {
if jcu.IsStar {
return true
}
return jcu.Value == other
}
// CheckBool interprets own value as bool (true = anything > 0, false = 0),
// We are using JSONCheckUint64 for bool too so we don't create another type.
func (jcu JSONCheckUint64) CheckBool(other bool) bool {
if jcu.IsStar {
return true
}
return jcu.Value > 0 == other
}
// JSONCheckValueList represents a list of value checks, as expressed in JSON.
// TODO: add star for all values
type JSONCheckValueList struct {
Values []JSONCheckBytes
IsStar bool
Unspecified bool
}
// JSONCheckValueListUnspecified yields JSONCheckBytesList empty value.
func JSONCheckValueListUnspecified() JSONCheckValueList {
return JSONCheckValueList{
Values: nil,
IsStar: false,
Unspecified: true,
}
}
// JSONCheckValueListStar yields the "*" value
func JSONCheckValueListStar() JSONCheckValueList {
return JSONCheckValueList{
Values: nil,
IsStar: true,
Unspecified: false,
}
}
// IsUnspecified yields true if the field was originally unspecified.
func (jcbl JSONCheckValueList) IsUnspecified() bool {
return jcbl.Unspecified
}
// CheckList compares expected value with a list of values.
func (jcbl JSONCheckValueList) CheckList(other [][]byte) bool {
if jcbl.IsStar {
return true
}
if len(jcbl.Values) != len(other) {
return false
}
for i, expected := range jcbl.Values {
if !expected.Check(other[i]) {
return false
}
}
return true
}