-
Notifications
You must be signed in to change notification settings - Fork 0
/
entries.go
168 lines (126 loc) · 3.1 KB
/
entries.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
package jq
import (
"nikand.dev/go/cbor"
)
type (
ToEntries struct {
arr []Off
Arrays bool
}
FromEntries struct {
arr []Off
obj []Off
}
)
func NewToEntries() *ToEntries { return &ToEntries{} }
func NewToEntriesArrays() *ToEntries { return &ToEntries{Arrays: true} }
func NewFromEntries() *FromEntries { return &FromEntries{} }
func NewWithEntries(f Filter) *Pipe {
return NewPipe(NewToEntries(), NewMap(f), NewFromEntries())
}
func NewWithEntriesArrays(f Filter) *Pipe {
return NewPipe(NewToEntriesArrays(), NewMap(f), NewFromEntries())
}
func (f *ToEntries) ApplyTo(b *Buffer, off Off, next bool) (res Off, more bool, err error) {
if next {
return None, false, nil
}
br := b.Reader()
bw := b.Writer()
if err = br.check(off, cbor.Map); err != nil {
return None, false, fe(f, off, err)
}
f.arr = br.ArrayMap(off, f.arr[:0])
if len(f.arr) == 0 {
return EmptyArray, false, nil
}
var etag Tag
var keyj, valj int
l := len(f.arr)
if f.Arrays {
etag = cbor.Array
keyj, valj = 0, 1
f.arr = append(f.arr, Null, Null)
} else {
etag = cbor.Map
keyj, valj = 1, 3
key := bw.String("key")
val := bw.String("value")
f.arr = append(f.arr, key, Null, val, Null)
}
for j := 0; j < l; j += 2 {
f.arr[l+keyj] = f.arr[j]
f.arr[l+valj] = f.arr[j+1]
f.arr = append(f.arr, bw.ArrayMap(etag, f.arr[l:l+valj+1]))
}
res = bw.Array(f.arr[l+valj+1:])
return res, false, nil
}
func (f *FromEntries) ApplyTo(b *Buffer, off Off, next bool) (res Off, more bool, err error) {
if next {
return None, false, nil
}
br := b.Reader()
if err = br.check(off, cbor.Array); err != nil {
return None, false, fe(f, off, err)
}
f.arr = br.ArrayMap(off, f.arr[:0])
if len(f.arr) == 0 {
return Null, false, nil
}
l := len(f.arr)
f.obj = f.obj[:0]
for j := 0; j < l; j++ {
if err = br.check2(f.arr[j], cbor.Map, cbor.Array); err != nil {
return None, false, fe(f, f.arr[j], err)
}
f.arr = br.ArrayMap(f.arr[j], f.arr[:l])
if len(f.arr) == 0 {
continue
}
tag := br.Tag(f.arr[j])
var key, val Off = None, Null
if tag == cbor.Map {
keyj := findKey(b, f.arr[l:], "key", "Key", "name", "Name")
if keyj < 0 {
return None, false, fe(f, f.arr[j], NewTypeError(br.Tag(Null), cbor.String, cbor.Bytes))
}
key = f.arr[l+keyj+1]
valj := findKey(b, f.arr[l:], "value", "Value")
if valj >= 0 {
val = f.arr[l+valj+1]
}
} else if len(f.arr[l:]) == 0 {
continue
} else {
key = f.arr[l]
if len(f.arr) > 1 {
val = f.arr[l+1]
}
}
f.obj = append(f.obj, key, val)
}
res = b.Writer().Map(f.obj)
return res, false, nil
}
func findKey(b *Buffer, obj []Off, keys ...string) (jj int) {
br := b.Reader()
jj = -1
for _, key := range keys {
for j := 0; j < len(obj); j += 2 {
if tag := br.Tag(obj[j]); tag != cbor.String && tag != cbor.Bytes {
continue
}
k := br.Bytes(obj[j])
if string(k) == key {
jj = j
}
}
if jj >= 0 {
return jj
}
}
return -1
}
func (f *ToEntries) String() string { return "to_entries" + csel(f.Arrays, "_arrays", "") }
func (f *FromEntries) String() string { return "from_entries" }