forked from oVirt/go-ovirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
254 lines (231 loc) · 5.54 KB
/
reader.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// Copyright (c) 2017 Joey <majunjiev@gmail.com>.
//
// Licensed 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 ovirtsdk
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"io"
"strconv"
"time"
)
// XMLTagNotMatchError indicates the error of XML tag
// not matched when unmarshaling XML
type XMLTagNotMatchError struct {
ActualTag string
ExpectedTag string
}
func (err XMLTagNotMatchError) Error() string {
return fmt.Sprintf("Tag not matched: expect <%v> but got <%v>", err.ExpectedTag, err.ActualTag)
}
// CanForward indicates if Decoder has been finished
func CanForward(tok xml.Token) (bool, error) {
switch tok.(type) {
case xml.StartElement:
return true, nil
case xml.EndElement:
return false, nil
default:
return true, nil
}
}
// XMLReader unmarshalizes the xml to struct
type XMLReader struct {
*xml.Decoder
}
// NewXMLReader creates a XMLReader instance
func NewXMLReader(b []byte) *XMLReader {
return &XMLReader{
Decoder: xml.NewDecoder(bytes.NewReader(b)),
}
}
// FindStartElement finds the right next StartElement
func (reader *XMLReader) FindStartElement() (*xml.StartElement, error) {
// Find start element if we need it.
for {
tok, err := reader.Next()
if err != nil {
fmt.Printf("err is %v\n", err)
break
}
tok = xml.CopyToken(tok)
if tok, ok := tok.(xml.StartElement); ok {
return &tok, nil
}
}
return nil, errors.New("Failed to find StartElement")
}
// Next calls xml.Decoder.Token() to get the next xml.Token
func (reader *XMLReader) Next() (xml.Token, error) {
return reader.Token()
}
// ReadString reads the xml.CharData as a string after xml.StartElement
func (reader *XMLReader) ReadString(start *xml.StartElement) (string, error) {
if start == nil {
st, err := reader.FindStartElement()
if err != nil {
return "", err
}
start = st
}
var buf []byte
depth := 1
for depth > 0 {
t, err := reader.Next()
if err != nil {
if err == io.EOF {
break
}
return "", err
}
switch t := t.(type) {
case xml.CharData:
if depth == 1 {
buf = append(buf, t...)
}
case xml.StartElement:
depth++
case xml.EndElement:
depth--
}
}
return string(buf), nil
}
// ReadStrings reads the xml.CharData of all subelements with a slice of string returned
func (reader *XMLReader) ReadStrings(start *xml.StartElement) ([]string, error) {
var strings []string
if start == nil {
st, err := reader.FindStartElement()
if err != nil {
return nil, err
}
start = st
}
depth := 1
for depth > 0 {
t, err := reader.Next()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
switch t := t.(type) {
case xml.StartElement:
str, err := reader.ReadString(&t)
if err != nil {
return nil, err
}
strings = append(strings, str)
case xml.EndElement:
depth--
}
}
return strings, nil
}
// ReadBool reads the xml.CharData as bool
func (reader *XMLReader) ReadBool(start *xml.StartElement) (bool, error) {
str, err := reader.ReadString(start)
if err != nil {
return false, err
}
return strconv.ParseBool(str)
}
func (reader *XMLReader) ReadBools(start *xml.StartElement) ([]bool, error) {
strs, err := reader.ReadStrings(start)
if err != nil {
return nil, err
}
var bools []bool
for _, sv := range strs {
bv, err := strconv.ParseBool(sv)
if err != nil {
return nil, err
}
bools = append(bools, bv)
}
return bools, nil
}
// ReadInt64 reads the xml.CharData as int64
func (reader *XMLReader) ReadInt64(start *xml.StartElement) (int64, error) {
str, err := reader.ReadString(start)
if err != nil {
return 0, err
}
return strconv.ParseInt(str, 10, 64)
}
func (reader *XMLReader) ReadInt64s(start *xml.StartElement) ([]int64, error) {
strs, err := reader.ReadStrings(start)
if err != nil {
return nil, err
}
var int64s []int64
for _, sv := range strs {
iv, err := strconv.ParseInt(sv, 10, 64)
if err != nil {
return nil, err
}
int64s = append(int64s, iv)
}
return int64s, nil
}
func (reader *XMLReader) ReadFloat64(start *xml.StartElement) (float64, error) {
str, err := reader.ReadString(start)
if err != nil {
return 0.0, err
}
return strconv.ParseFloat(str, 64)
}
func (reader *XMLReader) ReadFloat64s(start *xml.StartElement) ([]float64, error) {
strs, err := reader.ReadStrings(start)
if err != nil {
return nil, err
}
var float64s []float64
for _, sv := range strs {
fv, err := strconv.ParseFloat(sv, 64)
if err != nil {
return nil, err
}
float64s = append(float64s, fv)
}
return float64s, nil
}
// ReadTime reads the xml.CharData as time.Time
func (reader *XMLReader) ReadTime(start *xml.StartElement) (time.Time, error) {
str, err := reader.ReadString(start)
if err != nil {
var t time.Time
return t, err
}
return time.Parse(time.RFC3339Nano, str)
}
func (reader *XMLReader) ReadTimes(start *xml.StartElement) ([]time.Time, error) {
strs, err := reader.ReadStrings(start)
if err != nil {
return nil, err
}
var times []time.Time
for _, sv := range strs {
tv, err := time.Parse(time.RFC3339Nano, sv)
if err != nil {
return nil, err
}
times = append(times, tv)
}
return times, nil
}