-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.go
55 lines (41 loc) · 920 Bytes
/
select.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
package jq
import (
"fmt"
"nikand.dev/go/cbor"
)
type (
Select struct {
Cond Filter
}
)
func NewSelect(cond Filter) *Select { return &Select{Cond: cond} }
func (f *Select) ApplyTo(b *Buffer, off Off, next bool) (res Off, more bool, err error) {
if next {
return None, false, nil
}
subf := or[Filter](f.Cond, Dot{})
next = false
for {
res, next, err = subf.ApplyTo(b, off, next)
if err != nil {
return off, false, fe(f, off, err)
}
if IsTrue(b, res) {
return off, false, nil
}
if !next {
break
}
}
return None, false, nil
}
func IsTrue(b *Buffer, off Off) bool {
if off < 0 {
return off != None && off != Null && off != False
}
tag, sub, _, _, _ := b.Decoder.Tag(b.Buf(off))
return tag != cbor.Simple || (sub != cbor.Null && sub != cbor.False && sub != cbor.None)
}
func (f Select) String() string {
return fmt.Sprintf("select(%v)", or[Filter](f.Cond, Dot{}))
}