/
xml.go
177 lines (164 loc) · 4.91 KB
/
xml.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package lib
import (
"bytes"
"io"
"strings"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/elastic/mito/lib/xml"
)
// XML returns a cel.EnvOption to configure extended functions for XML
// decoding. The parameter specifies the CEL type adapter to use and a
// map of names to XSD document descriptions.
// A nil adapter is valid and will give an option using the default type
// adapter, types.DefaultTypeAdapter. A nil XSD mapping is valid and
// will give an option that performs best effort decoding leaving all
// values as strings and elevating elements to lists when more than one
// item is found for the path.
//
// # Decode XML
//
// decode_xml returns the object described by the XML encoding of the receiver
// or parameter, using an optional named XSD:
//
// <bytes>.decode_xml() -> <dyn>
// <string>.decode_xml() -> <dyn>
// decode_xml(<bytes>) -> <dyn>
// decode_xml(<string>) -> <dyn>
// <bytes>.decode_xml(<string>) -> <dyn>
// <string>.decode_xml(<string>) -> <dyn>
// decode_xml(<bytes>, <string>) -> <dyn>
// decode_xml(<string>, <string>) -> <dyn>
//
// Examples:
//
// "<?xml vers... ...>".decode_xml() // return { ... }
// b"<?xml vers... ...>".decode_xml() // return { ... }
// "<?xml vers... ...>".decode_xml("xsd") // return { ... }
// b"<?xml vers... ...>".decode_xml("xsd") // return { ... }
func XML(adapter types.Adapter, xsd map[string]string) (cel.EnvOption, error) {
if adapter == nil {
adapter = types.DefaultTypeAdapter
}
details := make(map[string]map[string]xml.Detail, len(xsd))
var err error
for name, doc := range xsd {
details[name], err = xml.Details([]byte(doc))
if err != nil {
return nil, err
}
}
return cel.Lib(xmlLib{adapter: adapter, xsdDetails: details}), nil
}
type xmlLib struct {
adapter types.Adapter
xsdDetails map[string]map[string]xml.Detail
}
func (l xmlLib) CompileOptions() []cel.EnvOption {
return []cel.EnvOption{
cel.Function("decode_xml",
// Without type information.
cel.MemberOverload(
"string_decode_xml",
[]*cel.Type{cel.StringType},
cel.DynType,
cel.UnaryBinding(l.decodeXML),
),
cel.Overload(
"decode_xml_string",
[]*cel.Type{cel.StringType},
cel.DynType,
cel.UnaryBinding(l.decodeXML),
),
cel.MemberOverload(
"bytes_decode_xml",
[]*cel.Type{cel.BytesType},
cel.DynType,
cel.UnaryBinding(l.decodeXML),
),
cel.Overload(
"decode_xml_bytes",
[]*cel.Type{cel.BytesType},
cel.DynType,
cel.UnaryBinding(l.decodeXML),
),
// With type information.
cel.MemberOverload(
"string_decode_xml_string",
[]*cel.Type{cel.StringType, cel.StringType},
cel.DynType,
cel.BinaryBinding(l.decodeXMLWithXSD),
),
cel.Overload(
"decode_xml_string_string",
[]*cel.Type{cel.StringType, cel.StringType},
cel.DynType,
cel.BinaryBinding(l.decodeXMLWithXSD),
),
cel.MemberOverload(
"bytes_decode_xml_string",
[]*cel.Type{cel.BytesType, cel.StringType},
cel.DynType,
cel.BinaryBinding(l.decodeXMLWithXSD),
),
cel.Overload(
"decode_xml_bytes_string",
[]*cel.Type{cel.BytesType, cel.StringType},
cel.DynType,
cel.BinaryBinding(l.decodeXMLWithXSD),
),
),
}
}
func (xmlLib) ProgramOptions() []cel.ProgramOption { return nil }
func (l xmlLib) decodeXML(arg ref.Val) ref.Val {
return l.decodeXMLWithXSD(arg, types.String(""))
}
func (l xmlLib) decodeXMLWithXSD(arg0, arg1 ref.Val) ref.Val {
xsd, ok := arg1.(types.String)
if !ok {
return types.ValOrErr(xsd, "no such overload for decode_xml: %s", arg1.Type())
}
details, ok := l.xsdDetails[string(xsd)]
if !ok && xsd != "" {
return types.NewErr("no xsd %s", xsd)
}
var r io.Reader
switch msg := arg0.(type) {
case types.Bytes:
r = bytes.NewReader(msg)
case types.String:
r = strings.NewReader(string(msg))
default:
return types.NoSuchOverloadErr()
}
cdata, v, err := xml.Unmarshal(r, details)
if err != nil {
return types.NewErr("failed to unmarshal XML document: %v", err)
}
m := make(map[string]any)
if cdata != "" {
m["#text"] = cdata
}
if v != nil {
m["doc"] = v
}
return l.adapter.NativeToValue(m)
}