forked from divan/gorilla-xmlrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml2rpc.go
210 lines (184 loc) · 4.86 KB
/
xml2rpc.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
// Copyright 2013 Ivan Danyliuk
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xml
import (
"bytes"
"encoding/base64"
"encoding/xml"
"fmt"
"reflect"
"strconv"
"time"
"unicode"
"unicode/utf8"
"github.com/rogpeppe/go-charset/charset"
_ "github.com/rogpeppe/go-charset/data"
)
// Types used for unmarshalling
type response struct {
Name xml.Name `xml:"methodResponse"`
Params []param `xml:"params>param"`
Fault faultValue `xml:"fault,omitempty"`
}
type param struct {
Value value `xml:"value"`
}
type value struct {
Array []value `xml:"array>data>value"`
Struct []member `xml:"struct>member"`
String string `xml:"string"`
Int string `xml:"int"`
Int4 string `xml:"i4"`
Double string `xml:"double"`
Boolean string `xml:"boolean"`
DateTime string `xml:"dateTime.iso8601"`
Base64 string `xml:"base64"`
Raw string `xml:",innerxml"` // the value can be defualt string
}
type member struct {
Name string `xml:"name"`
Value value `xml:"value"`
}
func xml2RPC(xmlraw string, rpc interface{}) error {
// Unmarshal raw XML into the temporal structure
var ret response
decoder := xml.NewDecoder(bytes.NewReader([]byte(xmlraw)))
decoder.CharsetReader = charset.NewReader
err := decoder.Decode(&ret)
if err != nil {
return FaultDecode
}
if !ret.Fault.IsEmpty() {
return getFaultResponse(ret.Fault)
}
// Structures should have equal number of fields
if reflect.TypeOf(rpc).Elem().NumField() != len(ret.Params) {
return FaultWrongArgumentsNumber
}
// Now, convert temporal structure into the
// passed rpc variable, according to it's structure
for i, param := range ret.Params {
field := reflect.ValueOf(rpc).Elem().Field(i)
err = value2Field(param.Value, &field)
if err != nil {
return err
}
}
return nil
}
// getFaultResponse converts faultValue to Fault.
func getFaultResponse(fault faultValue) Fault {
var (
code int
str string
)
for _, field := range fault.Value.Struct {
if field.Name == "faultCode" {
code, _ = strconv.Atoi(field.Value.Int)
} else if field.Name == "faultString" {
str = field.Value.String
if str == "" {
str = field.Value.Raw
}
}
}
return Fault{Code: code, String: str}
}
func value2Field(value value, field *reflect.Value) error {
if !field.CanSet() {
return FaultApplicationError
}
var (
err error
val interface{}
)
switch {
case value.Int != "":
val, _ = strconv.Atoi(value.Int)
case value.Int4 != "":
val, _ = strconv.Atoi(value.Int4)
case value.Double != "":
val, _ = strconv.ParseFloat(value.Double, 64)
case value.String != "":
val = value.String
case value.Boolean != "":
val = xml2Bool(value.Boolean)
case value.DateTime != "":
val, err = xml2DateTime(value.DateTime)
case value.Base64 != "":
val, err = xml2Base64(value.Base64)
case len(value.Struct) != 0:
if field.Kind() != reflect.Struct {
fault := FaultInvalidParams
fault.String += fmt.Sprintf("structure fields mismatch: %s != %s", field.Kind(), reflect.Struct.String())
return fault
}
s := value.Struct
for i := 0; i < len(s); i++ {
// Uppercase first letter for field name to deal with
// methods in lowercase, which cannot be used
field_name := uppercaseFirst(s[i].Name)
f := field.FieldByName(field_name)
err = value2Field(s[i].Value, &f)
}
case len(value.Array) != 0:
a := value.Array
f := *field
slice := reflect.MakeSlice(reflect.TypeOf(f.Interface()),
len(a), len(a))
for i := 0; i < len(a); i++ {
item := slice.Index(i)
err = value2Field(a[i], &item)
}
f = reflect.AppendSlice(f, slice)
val = f.Interface()
case len(value.Array) == 0:
val = val
default:
// value field is default to string, see http://en.wikipedia.org/wiki/XML-RPC#Data_types
// also can be <nil/>
if value.Raw != "<nil/>" {
val = value.Raw
}
}
if val != nil {
if reflect.TypeOf(val) != reflect.TypeOf(field.Interface()) {
fault := FaultInvalidParams
fault.String += fmt.Sprintf(": fields type mismatch: %s != %s",
reflect.TypeOf(val),
reflect.TypeOf(field.Interface()))
return fault
}
field.Set(reflect.ValueOf(val))
}
return err
}
func xml2Bool(value string) bool {
var b bool
switch value {
case "1", "true", "TRUE", "True":
b = true
case "0", "false", "FALSE", "False":
b = false
}
return b
}
func xml2DateTime(value string) (time.Time, error) {
var (
year, month, day int
hour, minute, second int
)
_, err := fmt.Sscanf(value, "%04d%02d%02dT%02d:%02d:%02d",
&year, &month, &day,
&hour, &minute, &second)
t := time.Date(year, time.Month(month), day, hour, minute, second, 0, time.Local)
return t, err
}
func xml2Base64(value string) ([]byte, error) {
return base64.StdEncoding.DecodeString(value)
}
func uppercaseFirst(in string) (out string) {
r, n := utf8.DecodeRuneInString(in)
return string(unicode.ToUpper(r)) + in[n:]
}