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
|
package tree
import (
"fmt"
)
//Ctx represents the current context position, size, node, and the current filtered result
type Ctx struct {
NodeSet
Pos int
Size int
}
//Fn is a XPath function, written in Go
type Fn func(c Ctx, args ...Result) (Result, error)
//LastArgOpt sets whether the last argument in a function is optional, variadic, or neither
type LastArgOpt int
//LastArgOpt options
const (
None LastArgOpt = iota
Optional
Variadic
)
//Wrap interfaces XPath function calls with Go
type Wrap struct {
Fn Fn
//NArgs represents the number of arguments to the XPath function. -1 represents a single optional argument
NArgs int
LastArgOpt LastArgOpt
}
//Call checks the arguments and calls Fn if they are valid
func (w Wrap) Call(c Ctx, args ...Result) (Result, error) {
switch w.LastArgOpt {
case Optional:
if len(args) == w.NArgs || len(args) == w.NArgs-1 {
return w.Fn(c, args...)
}
case Variadic:
if len(args) >= w.NArgs-1 {
return w.Fn(c, args...)
}
default:
if len(args) == w.NArgs {
return w.Fn(c, args...)
}
}
return nil, fmt.Errorf("Invalid number of arguments")
}
|