-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence.go
More file actions
133 lines (113 loc) · 2.98 KB
/
sequence.go
File metadata and controls
133 lines (113 loc) · 2.98 KB
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
package risorxpath
import (
"context"
"github.com/risor-io/risor/errz"
"github.com/risor-io/risor/object"
"github.com/risor-io/risor/op"
"github.com/speedata/goxpath"
)
// sequence of objects
type sequence struct {
seq goxpath.Sequence
current object.Object
pos int
}
func newSequence(items goxpath.Sequence) *sequence {
return &sequence{seq: items}
}
func (ls *sequence) Type() object.Type {
return "xpath.sequence"
}
func (ls *sequence) Inspect() string {
sv, err := goxpath.StringValue(ls.seq)
if err != nil {
return err.Error()
}
return sv
}
func (ls *sequence) SetAttr(name string, value object.Object) error {
return errz.TypeErrorf("type error: object has no attribute %q", name)
}
func (ls *sequence) GetAttr(name string) (object.Object, bool) {
return nil, false
}
func (ls *sequence) Interface() interface{} {
items := make([]interface{}, 0, len(ls.seq))
for _, item := range ls.seq {
items = append(items, item)
}
return items
}
func (ls *sequence) String() string {
return ls.Inspect()
}
func (ls *sequence) Equals(other object.Object) object.Object {
if other.Type() != "xpath.sequence" {
return object.False
}
otherList := other.(*sequence)
if len(ls.seq) != len(otherList.seq) {
return object.False
}
for i, v := range ls.seq {
otherV := otherList.seq[i]
if v != otherV {
return object.False
}
}
return object.True
}
func (ls *sequence) IsTruthy() bool {
bv, err := goxpath.BooleanValue(ls.seq)
if err != nil {
return false
}
return bv
}
// Len returns the number of items in this container.
func (ls *sequence) Len() *object.Int {
return object.NewInt(int64(len(ls.seq)))
}
func (ls *sequence) Size() int {
return len(ls.seq)
}
func (ls *sequence) RunOperation(opType op.BinaryOpType, right object.Object) object.Object {
switch right := right.(type) {
case *sequence:
return ls.runOperationList(opType, right)
default:
return object.TypeErrorf("type error: unsupported operation for list: %v on type %s",
opType, right.Type())
}
}
func (ls *sequence) runOperationList(opType op.BinaryOpType, right *sequence) object.Object {
switch opType {
case op.Add:
combined := make(goxpath.Sequence, len(ls.seq)+len(right.seq))
copy(combined, ls.seq)
copy(combined[len(ls.seq):], right.seq)
return newSequence(combined)
default:
return object.TypeErrorf("type error: unsupported operation for list: %v on type %s",
opType, right.Type())
}
}
func (ls *sequence) Cost() int {
// It would be possible to recurse into the list and compute the cost of
// each item, but let's avoid that since it would be an expensive op itself.
return len(ls.seq) * 8
}
func (ls *sequence) Next(_ context.Context) (object.Object, bool) {
if ls.pos >= len(ls.seq) {
return nil, false
}
ls.current = convertItemToObject(ls.seq[ls.pos])
ls.pos++
return ls.current, true
}
func (ls *sequence) Entry() (object.IteratorEntry, bool) {
if ls.current == nil {
return nil, false
}
return object.NewEntry(object.NewInt(int64(ls.pos-1)), ls.current), true
}