-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.go
More file actions
57 lines (49 loc) · 1.63 KB
/
array.go
File metadata and controls
57 lines (49 loc) · 1.63 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
package goxpath
import "fmt"
const nsArray = "http://www.w3.org/2005/xpath-functions/array"
// XPathArray represents an XPath 3.1 array.
type XPathArray struct {
Members []Sequence
}
// Get returns the member at the given 1-based index.
func (a *XPathArray) Get(pos int) (Sequence, error) {
if pos < 1 || pos > len(a.Members) {
return nil, fmt.Errorf("array index %d out of bounds (size %d)", pos, len(a.Members))
}
return a.Members[pos-1], nil
}
// Size returns the number of members in the array.
func (a *XPathArray) Size() int {
return len(a.Members)
}
func fnArrayGet(ctx *Context, args []Sequence) (Sequence, error) {
if len(args[0]) != 1 {
return nil, fmt.Errorf("array:get expects a single array as first argument")
}
arr, ok := args[0][0].(*XPathArray)
if !ok {
return nil, fmt.Errorf("array:get expects an array as first argument, got %T", args[0][0])
}
if len(args[1]) != 1 {
return nil, fmt.Errorf("array:get expects a single integer as second argument")
}
pos, err := NumberValue(args[1])
if err != nil {
return nil, err
}
return arr.Get(int(pos))
}
func fnArraySize(ctx *Context, args []Sequence) (Sequence, error) {
if len(args[0]) != 1 {
return nil, fmt.Errorf("array:size expects a single array as first argument")
}
arr, ok := args[0][0].(*XPathArray)
if !ok {
return nil, fmt.Errorf("array:size expects an array as first argument, got %T", args[0][0])
}
return Sequence{arr.Size()}, nil
}
func init() {
RegisterFunction(&Function{Name: "get", Namespace: nsArray, F: fnArrayGet, MinArg: 2, MaxArg: 2})
RegisterFunction(&Function{Name: "size", Namespace: nsArray, F: fnArraySize, MinArg: 1, MaxArg: 1})
}