Skip to content

Commit 972a1fe

Browse files
authored
[type:feature] Add xmlx.go and xmlx_test.go (#107)
Add xmlx.go and xmlx_test.go
1 parent fb7cd3f commit 972a1fe

File tree

2 files changed

+206
-0
lines changed

2 files changed

+206
-0
lines changed

gox/encodingx/xmlx/xmlx.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package xmlx
19+
20+
import (
21+
"encoding/xml"
22+
)
23+
24+
type any = interface{}
25+
26+
// ToXmlBytes convert any to xml bytes
27+
// wrap xml.Marshal
28+
func ToXmlBytes(v any) ([]byte, error) {
29+
return xml.Marshal(v)
30+
}
31+
32+
// ToXmlString convert any to xml string
33+
// wrap xml.Marshal
34+
func ToXmlString(v any) (string, error) {
35+
marshal, err := xml.Marshal(v)
36+
return string(marshal), err
37+
}
38+
39+
// XmlBytesToAny convert xml bytes to any
40+
// wrap xml.Unmarshal
41+
func XmlBytesToAny(bytes []byte, v any) error {
42+
return xml.Unmarshal(bytes, v)
43+
}
44+
45+
// XmlStringToAny convert xml bytes to any
46+
// wrap xml.Unmarshal
47+
func XmlStringToAny(str string, v any) error {
48+
return xml.Unmarshal([]byte(str), v)
49+
}

gox/encodingx/xmlx/xmlx_test.go

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package xmlx
19+
20+
import (
21+
"reflect"
22+
"testing"
23+
)
24+
25+
type user struct {
26+
Name string `xml:"name"`
27+
Age int `xml:"age"`
28+
}
29+
30+
var u = user{
31+
Name: "renzhuyan",
32+
Age: 123,
33+
}
34+
35+
func TestXmlBytesToAny(t *testing.T) {
36+
type args struct {
37+
bytes []byte
38+
v any
39+
}
40+
tests := []struct {
41+
name string
42+
args args
43+
wantErr bool
44+
}{
45+
{
46+
name: "xml bytes to any",
47+
args: args{
48+
bytes: []byte("<user><name>renzhuyan</name><age>123</age></user>"),
49+
v: &user{},
50+
},
51+
wantErr: false,
52+
},
53+
}
54+
for _, tt := range tests {
55+
t.Run(tt.name, func(t *testing.T) {
56+
if err := XmlBytesToAny(tt.args.bytes, tt.args.v); (err != nil) != tt.wantErr {
57+
t.Errorf("XmlBytesToAny() error = %v, wantErr %v", err, tt.wantErr)
58+
}
59+
if !reflect.DeepEqual(tt.args.v, &u) {
60+
t.Errorf("xmlStringToAny() got = %v, want %v", tt.args.v, &u)
61+
}
62+
})
63+
}
64+
}
65+
66+
func TestXmlStringToAny(t *testing.T) {
67+
type args struct {
68+
str string
69+
v any
70+
}
71+
tests := []struct {
72+
name string
73+
args args
74+
wantErr bool
75+
}{
76+
{
77+
name: "xml string to any",
78+
args: args{
79+
str: "<user><name>renzhuyan</name><age>123</age></user>",
80+
v: &user{},
81+
},
82+
wantErr: false,
83+
},
84+
}
85+
for _, tt := range tests {
86+
t.Run(tt.name, func(t *testing.T) {
87+
if err := XmlStringToAny(tt.args.str, tt.args.v); (err != nil) != tt.wantErr {
88+
t.Errorf("xmlStringToAny() error = %v, wantErr %v", err, tt.wantErr)
89+
}
90+
if !reflect.DeepEqual(tt.args.v, &u) {
91+
t.Errorf("xmlStringToAny() got = %v, want %v", tt.args.v, &u)
92+
}
93+
})
94+
}
95+
}
96+
97+
func TestToXmlBytes(t *testing.T) {
98+
type args struct {
99+
v any
100+
}
101+
tests := []struct {
102+
name string
103+
args args
104+
want []byte
105+
wantErr bool
106+
}{
107+
{
108+
name: "any to xml bytes",
109+
args: args{v: u},
110+
want: []byte("<user><name>renzhuyan</name><age>123</age></user>"),
111+
wantErr: false,
112+
},
113+
}
114+
for _, tt := range tests {
115+
t.Run(tt.name, func(t *testing.T) {
116+
got, err := ToXmlBytes(tt.args.v)
117+
if (err != nil) != tt.wantErr {
118+
t.Errorf("ToJXmlBytes() error = %v, wantErr %v", err, tt.wantErr)
119+
return
120+
}
121+
if !reflect.DeepEqual(got, tt.want) {
122+
t.Errorf("ToXmlBytes() got = %v, want %v", got, tt.want)
123+
}
124+
})
125+
}
126+
}
127+
128+
func TestToXmlString(t *testing.T) {
129+
type args struct {
130+
v any
131+
}
132+
tests := []struct {
133+
name string
134+
args args
135+
want string
136+
wantErr bool
137+
}{
138+
{
139+
name: "any to xml string",
140+
args: args{v: u},
141+
want: "<user><name>renzhuyan</name><age>123</age></user>",
142+
wantErr: false,
143+
},
144+
}
145+
for _, tt := range tests {
146+
t.Run(tt.name, func(t *testing.T) {
147+
got, err := ToXmlString(tt.args.v)
148+
if (err != nil) != tt.wantErr {
149+
t.Errorf("ToXmlString() error = %v, wantErr %v", err, tt.wantErr)
150+
return
151+
}
152+
if got != tt.want {
153+
t.Errorf("ToXmlString() got = %v, want %v", got, tt.want)
154+
}
155+
})
156+
}
157+
}

0 commit comments

Comments
 (0)