-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.go
More file actions
86 lines (80 loc) · 2.09 KB
/
module.go
File metadata and controls
86 lines (80 loc) · 2.09 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
package risorxpath
import (
"context"
"os"
"github.com/risor-io/risor/object"
"github.com/speedata/goxml"
"github.com/speedata/goxpath"
"github.com/speedata/risorxml"
)
func convertItemToObject(itm goxpath.Item) object.Object {
switch item := itm.(type) {
case string:
return object.NewString(item)
case bool:
return object.NewBool(item)
case *goxml.Element:
return &risorxml.XMLElement{Value: item}
case float64:
return object.NewFloat(item)
case int64:
return object.NewInt(item)
case int:
return object.NewInt(int64(item))
case *goxml.Attribute:
return &risorxml.XMLAttribute{Value: item}
case goxml.Comment:
return &risorxml.XMLComment{Value: item}
case *goxml.Comment:
return &risorxml.XMLComment{Value: *item}
case goxml.ProcInst:
return &risorxml.XMLProcInst{Value: &item}
case *goxml.ProcInst:
return &risorxml.XMLProcInst{Value: item}
default:
// fmt.Printf("Unknown item type: %T\n", item)
return nil
}
}
func parse(ctx context.Context, args ...object.Object) object.Object {
if len(args) != 1 {
return object.ArgsErrorf("args error: xml.parse() takes exactly 1 argument (%d given)", len(args))
}
firstArg := args[0]
switch firstArg.Type() {
case "string":
// a filename
r, err := os.Open(firstArg.Interface().(string))
if err != nil {
return object.NewError(err)
}
d, err := goxpath.NewParser(r)
if err != nil {
return object.NewError(err)
}
return &xpathParser{
value: d,
}
case "file":
var rFile *object.File
var ok bool
if rFile, ok = firstArg.(*object.File); !ok {
return object.TypeErrorf("xml.parse() takes a file as argument, got %s", firstArg.Type())
}
d, err := goxpath.NewParser(rFile)
if err != nil {
return object.NewError(err)
}
return &xpathParser{
value: d,
}
default:
return object.TypeErrorf("xml.parse() takes a string or file as argument, got %s", firstArg.Type())
}
}
// Module returns a new module with the XPath parser functionality.
func Module() *object.Module {
return object.NewBuiltinsModule("xpath", map[string]object.Object{
"parse": object.NewBuiltin("xpath.parse", parse),
})
}