-
Notifications
You must be signed in to change notification settings - Fork 1
/
globals.go
146 lines (127 loc) · 2.97 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
package sdk
import (
"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 {
convertToArray := func(field, str string) (string, bool) {
find := "\"" + field + "\": {"
start := strings.Index(str, find)
if start == -1 {
return str, false
}
braceCount := 0
end := start + len(find)
for i := end; i < len(str); i++ {
if str[i] == '{' {
braceCount++
} else if str[i] == '}' {
if braceCount == 0 {
end = i + 1
break
}
braceCount--
}
}
beforeB := str[:start+len(find)-1] // Adjust to include '{'
afterB := str[end:] // after "}"
objectB := str[start+len(find)-1 : end] // Adjust to start from '{'
return beforeB + "[" + objectB + "]" + afterB, strings.Contains(str, find)
}
str := strIn
for {
var more bool
str, more = convertToArray(field, str)
if !more {
break
}
}
return str
}
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)
}