-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglobals.go
186 lines (159 loc) · 4.04 KB
/
globals.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
package sdk
import (
"bytes"
"encoding/json"
"reflect"
"strings"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
)
// Globals is a subset of globally available options from the command line
// that make sense in the SDK context
type Globals struct {
Ether bool `json:"ether,omitempty"`
Cache bool `json:"cache,omitempty"`
Decache bool `json:"decache,omitempty"`
Verbose bool `json:"verbose,omitempty"`
Chain string `json:"chain,omitempty"`
Output string `json:"output,omitempty"`
Append bool `json:"append,omitempty"`
// Probably can't support
// --file
// Global things ignored in the SDK
// --help
// --wei
// --fmt
// --version
// --noop
// --nocolor
// --no_header
}
func (g Globals) String() string {
bytes, _ := json.Marshal(g)
return string(bytes)
}
type CacheOp uint8
const (
CacheOn CacheOp = iota
CacheOff
Decache
)
func (g *Globals) Caching(op CacheOp) {
switch op {
case CacheOn:
g.Cache = true
g.Decache = false
case CacheOff:
g.Cache = false
g.Decache = false
case Decache:
g.Cache = false
g.Decache = true
}
}
type Cacher interface {
Caching(op CacheOp)
}
func convertObjectToArray(field, strIn string) string {
// We’ll look for this pattern: `"field":`
pattern := []byte("\"" + field + "\":")
in := []byte(strIn)
// Output buffer (pre-allocate at least len(in) to reduce re-allocation).
out := make([]byte, 0, len(in))
// Current scan position
i := 0
for {
// Find the next occurrence of `"field":` starting from i.
idx := bytes.Index(in[i:], pattern)
if idx == -1 {
// No more occurrences; copy everything left to output.
out = append(out, in[i:]...)
break
}
absIdx := i + idx // absolute index of the match in `in`
// 1. Copy everything *before* the match to output.
out = append(out, in[i:absIdx]...)
// 2. Copy the `"field":` pattern itself to output.
out = append(out, pattern...)
// Advance i past `"field":`
i = absIdx + len(pattern)
// 3. Preserve any whitespace right after the colon in the output.
for i < len(in) && isWhitespace(in[i]) {
out = append(out, in[i])
i++
}
// 4. If the next character is not `{`, nothing to bracket. Continue scanning.
if i >= len(in) || in[i] != '{' {
continue
}
// 5. Brace matching. If we find a matching `}`, we bracket the substring.
braceStart := i
braceCount := 0
matchFound := false
for j := i; j < len(in); j++ {
if in[j] == '{' {
braceCount++
} else if in[j] == '}' {
braceCount--
if braceCount == 0 {
// Found the matching closing brace at j
// Write `[ { ... } ]` to output
out = append(out, '[')
out = append(out, in[braceStart:j+1]...)
out = append(out, ']')
// Advance i to the char after `}`
i = j + 1
matchFound = true
break
}
}
}
// 6. If unmatched braces, copy the rest as-is and stop.
if !matchFound {
out = append(out, in[braceStart:]...)
i = len(in)
break
}
}
return string(out)
}
func isWhitespace(c byte) bool {
return c == ' ' || c == '\t' || c == '\n' || c == '\r'
}
func convertEmptyStrToZero(field, strIn string) string {
convertToZero := func(field, str string) (string, bool) {
find := "\"" + field + "\": \"\""
start := strings.Index(str, find)
if start == -1 {
return str, false
}
end := start + len(find)
for i := end; i < len(str); i++ {
if str[i] == ',' || str[i] == '}' {
end = i
break
}
}
beforeB := str[:start+len(find)-2] // Adjust to include '""'
afterB := str[end:] // after ","
return beforeB + "\"0\"" + afterB, strings.Contains(str, find)
}
str := strIn
for {
var more bool
str, more = convertToZero(field, str)
if !more {
break
}
}
return str
}
func debugPrint(str string, t any, err error) {
logger.Error("======================================")
logger.Error(err)
logger.Error(reflect.TypeOf(t))
max := base.Min(2000, len(str))
logger.Error(str[:max])
logger.Error("======================================")
// os.Exit(1)
}