-
Notifications
You must be signed in to change notification settings - Fork 0
/
valueInit.go
115 lines (99 loc) · 2.89 KB
/
valueInit.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
package scenmodel
import (
"math/big"
oj "github.com/multiversx/mx-chain-scenario-go/orderedjson"
)
// JSONBytesFromString stores a byte slice
type JSONBytesFromString struct {
Value []byte
Original string
Unspecified bool
}
// NewJSONBytesFromString creates a new JSONBytesFromString instance.
func NewJSONBytesFromString(value []byte, originalStr string) JSONBytesFromString {
return JSONBytesFromString{
Value: value,
Original: originalStr,
Unspecified: false,
}
}
// JSONBytesEmpty creates a new JSONBytesFromString instance with default values.
func JSONBytesEmpty() JSONBytesFromString {
return JSONBytesFromString{
Value: nil,
Original: "",
Unspecified: true,
}
}
// JSONBytesFromTree stores a parsed byte slice, either from a string, or from a list of strings.
// The list of strings representation can be used in storage, arguments or results,
// and it is designed to make it easier to express serialized objects.
// The strings in the list get simply concatenated to produce a value.
type JSONBytesFromTree struct {
Value []byte
Original oj.OJsonObject
Unspecified bool
}
// OriginalEmpty returns true if the object originates from "".
func (jb JSONBytesFromTree) OriginalEmpty() bool {
if str, isStr := jb.Original.(*oj.OJsonString); isStr {
return len(str.Value) == 0
}
return false
}
// JSONBytesFromTreeValues extracts values from a slice of JSONBytesFromTree into a list
func JSONBytesFromTreeValues(jbs []JSONBytesFromTree) [][]byte {
result := make([][]byte, len(jbs))
for i, jb := range jbs {
result[i] = jb.Value
}
return result
}
// JSONBigInt stores the parsed big int value but also the original parsed string
type JSONBigInt struct {
Value *big.Int
Original string
Unspecified bool
}
// JSONBigIntZero provides an unitialized zero value.
func JSONBigIntZero() JSONBigInt {
return JSONBigInt{
Value: big.NewInt(0),
Original: "",
Unspecified: true,
}
}
// JSONUint64 stores the parsed uint64 value but also the original parsed string
type JSONUint64 struct {
Value uint64
Original string
Unspecified bool
}
// OriginalEmpty returns true if the object originates from "".
func (ju *JSONUint64) OriginalEmpty() bool {
return len(ju.Original) == 0
}
// JSONUint64Zero provides an unitialized zero value.
func JSONUint64Zero() JSONUint64 {
return JSONUint64{
Value: 0,
Original: "",
Unspecified: true,
}
}
// JSONValueList represents a list of values, as expressed in JSON
type JSONValueList struct {
Values []JSONBytesFromString
}
// IsUnspecified yields true if the field was originally unspecified.
func (jvl JSONValueList) IsUnspecified() bool {
return len(jvl.Values) == 0
}
// ToValues extracts values from a JSONValueList
func (jvl JSONValueList) ToValues() [][]byte {
result := make([][]byte, len(jvl.Values))
for i, jb := range jvl.Values {
result[i] = jb.Value
}
return result
}