-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexplorer.go
216 lines (198 loc) · 4.49 KB
/
explorer.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package structexplorer
import (
"fmt"
"log/slog"
"reflect"
"sync"
)
type objectAccess struct {
isRoot bool // set to true if is was one of the values at start
object any
path []string
label string
typeName string
hideZeros bool
sliceRange interval
}
func (o objectAccess) Value() any {
return valueAtAccessPath(o.object, o.path)
}
func (o objectAccess) isEmpty() bool {
return o.typeName == ""
}
type explorer struct {
mutex *sync.Mutex // to protect concurrent access to the map
accessMap map[int]map[int]objectAccess // row -> column -> objectAccess
options *Options // some properties can be modified by user
}
func (e *explorer) nextFreeColumn(row int) int {
max := 0
cols, ok := e.accessMap[row]
if !ok {
return 0
}
for col := range cols {
if col > max {
max = col
}
}
return max
}
func (e *explorer) nextFreeRow(column int) int {
for row, cols := range e.accessMap {
_, ok := cols[column]
if ok {
// cell taken
} else {
return row
}
}
return 0
}
func (e *explorer) rootKeys() (list []string) {
for _, row := range e.accessMap {
for _, access := range row {
list = append(list, access.label)
}
}
return
}
func (e *explorer) rootAccessWithLabel(label string) (oa objectAccess, row int, col int, ok bool) {
for row, rows := range e.accessMap {
for col, each := range rows {
if each.isRoot && each.label == label {
return each, row, col, true
}
}
}
return objectAccess{}, 0, 0, false
}
func newExplorerOnAll(labelValuePairs ...any) *explorer {
s := &explorer{
accessMap: map[int]map[int]objectAccess{},
options: new(Options),
mutex: new(sync.Mutex),
}
row := 0
for i := 0; i < len(labelValuePairs); i += 2 {
label, ok := labelValuePairs[i].(string)
if !ok {
slog.Info("label must be string", "label", labelValuePairs[i])
continue
}
value := labelValuePairs[i+1]
if !canExplore(value) {
slog.Info("value can not be explored", "value", value)
continue
}
s.putObjectStartingAt(row, 0, objectAccess{
isRoot: true,
object: value,
path: []string{""},
label: label,
hideZeros: true,
typeName: fmt.Sprintf("%T", value),
}, Row(row))
row++
}
return s
}
func (e *explorer) objectAt(row, col int) objectAccess {
r, ok := e.accessMap[row]
if !ok {
return objectAccess{}
}
return r[col]
}
func (e *explorer) canRemoveObjectAt(row, col int) bool {
r, ok := e.accessMap[row]
if !ok {
return false
}
return !r[col].isRoot
}
func (e *explorer) removeObjectAt(row, col int) {
r, ok := e.accessMap[row]
if !ok {
return
}
delete(r, col)
}
func (e *explorer) updateObjectAt(row, col int, updater func(access objectAccess) objectAccess) {
old := e.objectAt(row, col)
e.removeObjectAt(row, col)
e.putObjectStartingAt(row, col, updater(old), Row(row))
}
func (e *explorer) putObjectStartingAt(row, col int, access objectAccess, option ExploreOption) {
r, ok := e.accessMap[row]
if !ok {
r = map[int]objectAccess{}
e.accessMap[row] = r
}
oa, ok := r[col]
if !ok || oa.isEmpty() {
r[col] = access
return
}
// cell is taken, use option to find a new location
newRow, newCol := option.placement(e)
e.putObjectStartingAt(newRow, newCol, access, option)
}
func (e *explorer) buildIndexData(b *indexDataBuilder) indexData {
for row, each := range e.accessMap {
for col, access := range each {
info := b.build(row, col, access)
if info.entriesCount == 0 && info.hasZeros {
// toggle zero to have entries
e.updateObjectAt(row, col, func(access objectAccess) objectAccess {
access.hideZeros = false
return access
})
// rebuild
b.build(row, col, e.objectAt(row, col))
}
}
}
return b.data
}
func (e *explorer) removeNonRootObjects() {
newMap := map[int]map[int]objectAccess{}
for row, each := range e.accessMap {
for col, access := range each {
if access.isRoot {
rowMap, ok := newMap[row]
if !ok {
rowMap = map[int]objectAccess{}
newMap[row] = rowMap
}
rowMap[col] = access
}
}
}
// swap
e.accessMap = newMap
}
func canExplore(v any) bool {
rt := reflect.TypeOf(v)
rv := reflect.ValueOf(v)
if rt.Kind() == reflect.Interface || rt.Kind() == reflect.Pointer {
if rv.IsZero() {
return false
}
rt = rt.Elem()
rv = rv.Elem()
}
if rt.Kind() == reflect.Struct {
return true
}
if rt.Kind() == reflect.Slice {
return rv.Len() > 0
}
if rt.Kind() == reflect.Map {
return rv.Len() > 0
}
if rt.Kind() == reflect.Array {
return true
}
return false
}