-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseValue.go
151 lines (130 loc) · 3.57 KB
/
parseValue.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
package denalijsonparse
import (
"errors"
"math/big"
twos "github.com/Dharitri-org/drtg-bigint/twos-complement"
mj "github.com/Dharitri-org/drtg-vm-util/test-util/denali/json/model"
oj "github.com/Dharitri-org/drtg-vm-util/test-util/orderedjson"
)
type bigIntParseFormat int
const (
bigIntSignedBytes bigIntParseFormat = iota
bigIntUnsignedBytes
)
func (p *Parser) processCheckBigInt(obj oj.OJsonObject, format bigIntParseFormat) (mj.JSONCheckBigInt, error) {
if IsStar(obj) {
// "*" means any value, skip checking it
return mj.JSONCheckBigInt{
Value: nil,
IsStar: true,
Original: "*"}, nil
}
jbi, err := p.processBigInt(obj, format)
if err != nil {
return mj.JSONCheckBigInt{}, err
}
return mj.JSONCheckBigInt{
Value: jbi.Value,
IsStar: false,
Original: jbi.Original,
}, nil
}
func (p *Parser) processBigInt(obj oj.OJsonObject, format bigIntParseFormat) (mj.JSONBigInt, error) {
strVal, err := p.parseString(obj)
if err != nil {
return mj.JSONBigInt{}, err
}
bi, err := p.parseBigInt(strVal, format)
return mj.JSONBigInt{
Value: bi,
Original: strVal,
}, err
}
func (p *Parser) parseBigInt(strRaw string, format bigIntParseFormat) (*big.Int, error) {
bytes, err := p.ValueInterpreter.InterpretString(strRaw)
if err != nil {
return nil, err
}
switch format {
case bigIntSignedBytes:
return twos.FromBytes(bytes), nil
case bigIntUnsignedBytes:
return big.NewInt(0).SetBytes(bytes), nil
default:
return nil, errors.New("unknown format requested")
}
}
func (p *Parser) processCheckUint64(obj oj.OJsonObject) (mj.JSONCheckUint64, error) {
if IsStar(obj) {
// "*" means any value, skip checking it
return mj.JSONCheckUint64{
Value: 0,
IsStar: true,
Original: "*"}, nil
}
ju, err := p.processUint64(obj)
if err != nil {
return mj.JSONCheckUint64{}, err
}
return mj.JSONCheckUint64{
Value: ju.Value,
IsStar: false,
Original: ju.Original}, nil
}
func (p *Parser) processUint64(obj oj.OJsonObject) (mj.JSONUint64, error) {
bi, err := p.processBigInt(obj, bigIntUnsignedBytes)
if err != nil {
return mj.JSONUint64{}, err
}
if bi.Value == nil || !bi.Value.IsUint64() {
return mj.JSONUint64{}, errors.New("value is not uint64")
}
return mj.JSONUint64{
Value: bi.Value.Uint64(),
Original: bi.Original}, nil
}
func (p *Parser) parseCheckBytes(obj oj.OJsonObject) (mj.JSONCheckBytes, error) {
if IsStar(obj) {
// "*" means any value, skip checking it
return mj.JSONCheckBytesExplicitStar(), nil
}
jb, err := p.processSubTreeAsByteArray(obj)
if err != nil {
return mj.JSONCheckBytes{}, err
}
return mj.JSONCheckBytes{
Value: jb.Value,
IsStar: false,
Original: jb.Original,
}, nil
}
func (p *Parser) processStringAsByteArray(obj oj.OJsonObject) (mj.JSONBytesFromString, error) {
strVal, err := p.parseString(obj)
if err != nil {
return mj.JSONBytesFromString{}, err
}
result, err := p.ValueInterpreter.InterpretString(strVal)
return mj.NewJSONBytesFromString(result, strVal), err
}
func (p *Parser) processSubTreeAsByteArray(obj oj.OJsonObject) (mj.JSONBytesFromTree, error) {
value, err := p.ValueInterpreter.InterpretSubTree(obj)
return mj.JSONBytesFromTree{
Value: value,
Original: obj,
}, err
}
func (p *Parser) parseString(obj oj.OJsonObject) (string, error) {
str, isStr := obj.(*oj.OJsonString)
if !isStr {
return "", errors.New("not a string value")
}
return str.Value, nil
}
// IsStar returns whether check object is othe form "*".
func IsStar(obj oj.OJsonObject) bool {
str, isStr := obj.(*oj.OJsonString)
if !isStr {
return false
}
return str.Value == "*"
}