-
Notifications
You must be signed in to change notification settings - Fork 0
/
comma.go
85 lines (64 loc) · 1.39 KB
/
comma.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
package jq
import (
"fmt"
"strings"
)
type (
Comma struct {
Filters []Filter
j int
next bool
}
)
var _ FilterPath = (*Comma)(nil)
func NewComma(fs ...Filter) *Comma {
return &Comma{Filters: fs}
}
func (f *Comma) ApplyToGetPath(b *Buffer, off Off, base NodePath, next bool) (res Off, path NodePath, more bool, err error) {
return f.applyTo(b, off, base, next, true)
}
func (f *Comma) ApplyTo(b *Buffer, off Off, next bool) (res Off, more bool, err error) {
res, _, more, err = f.applyTo(b, off, nil, next, false)
return
}
func (f *Comma) applyTo(b *Buffer, off Off, base NodePath, next, addpath bool) (res Off, path NodePath, more bool, err error) {
if !next {
f.j = 0
f.next = false
}
res = None
path = base
for f.j < len(f.Filters) {
ff := f.Filters[f.j]
if addpath {
res, path, f.next, err = ApplyGetPath(ff, b, off, path, f.next)
} else {
res, f.next, err = ff.ApplyTo(b, off, f.next)
}
if err != nil {
return off, path, false, fse(f, f.j, off, err)
}
if !f.next {
f.j++
}
if res != None {
break
}
}
return res, path, f.j < len(f.Filters), nil
}
func (f Comma) String() string {
if len(f.Filters) == 0 {
return "empty"
}
var b strings.Builder
// _ = b.WriteByte('(')
for i, sub := range f.Filters {
if i != 0 {
_, _ = b.WriteString(", ")
}
_, _ = fmt.Fprintf(&b, "%v", sub)
}
// _ = b.WriteByte(')')
return b.String()
}