This commit is contained in:
4
go.mod
4
go.mod
@@ -5,11 +5,15 @@ go 1.21
|
||||
toolchain go1.23.4
|
||||
|
||||
require (
|
||||
github.com/stretchr/testify v1.10.0
|
||||
go.unistack.org/micro-proto/v3 v3.4.1
|
||||
go.unistack.org/micro/v3 v3.10.88
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/google/go-cmp v0.6.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
google.golang.org/protobuf v1.35.2 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
10
go.sum
10
go.sum
@@ -1,8 +1,18 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
||||
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
||||
go.unistack.org/micro/v3 v3.10.88 h1:MxlzP+77Y6Kphb3lzHxROL4XfE/WdCQMQpnPv4D9Z8U=
|
||||
go.unistack.org/micro/v3 v3.10.88/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
|
||||
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
|
||||
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
40
reader_test.go
Normal file
40
reader_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package xml
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestReader_Read(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input []byte
|
||||
expected []byte
|
||||
}{
|
||||
{
|
||||
name: "with replacement",
|
||||
input: []byte(`<?xml version="1.1" encoding="UTF-8"?><root name="NAME"><item>ITEM</item></root>`),
|
||||
expected: []byte(`<?xml version="1.0" encoding="UTF-8"?><root name="NAME"><item>ITEM</item></root>`),
|
||||
},
|
||||
{
|
||||
name: "without replacement",
|
||||
input: []byte(`<?xml version="1.0" encoding="UTF-8"?><root name="NAME"><item>ITEM</item></root>`),
|
||||
expected: []byte(`<?xml version="1.0" encoding="UTF-8"?><root name="NAME"><item>ITEM</item></root>`),
|
||||
},
|
||||
{
|
||||
name: "check invalid replacement",
|
||||
input: []byte(`<?xml version="1.0" encoding="UTF-8"?><root version="1.1"><item>ITEM</item></root>`),
|
||||
expected: []byte(`<?xml version="1.0" encoding="UTF-8"?><root version="1.1"><item>ITEM</item></root>`),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
buf := make([]byte, len(tt.input))
|
||||
n, err := newReader(tt.input).Read(buf)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tt.expected, buf[:n])
|
||||
})
|
||||
}
|
||||
}
|
6
xml.go
6
xml.go
@@ -134,9 +134,9 @@ func newReader(b []byte) io.Reader {
|
||||
}
|
||||
|
||||
var (
|
||||
srcP = []byte(`version="1.1"`)
|
||||
srcP = []byte(`<?xml version="1.1"`)
|
||||
srcL = len(srcP)
|
||||
dstP = []byte(`version="1.0"`)
|
||||
dstP = []byte(`<?xml version="1.0"`)
|
||||
)
|
||||
|
||||
func (r *reader) Read(b []byte) (n int, err error) {
|
||||
@@ -146,7 +146,7 @@ func (r *reader) Read(b []byte) (n int, err error) {
|
||||
r.prevRune = -1
|
||||
n = copy(b, r.s[r.i:])
|
||||
if !r.skip {
|
||||
if idx := bytes.Index(b, srcP); idx > 0 {
|
||||
if idx := bytes.Index(b, srcP); idx >= 0 {
|
||||
copy(b[idx:idx+srcL], dstP)
|
||||
r.skip = true
|
||||
}
|
||||
|
70
xml_test.go
Normal file
70
xml_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package xml
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.unistack.org/micro/v3/codec"
|
||||
)
|
||||
|
||||
func TestXmlCodec_Unmarshal(t *testing.T) {
|
||||
type result struct {
|
||||
XMLName xml.Name `xml:"root"`
|
||||
Name string `xml:"name,attr"`
|
||||
Item string `xml:"item"`
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input []byte
|
||||
isXML11 bool
|
||||
expected result
|
||||
}{
|
||||
{
|
||||
name: "without version",
|
||||
input: []byte(`<root name="NAME"><item>ITEM</item></root>`),
|
||||
expected: result{
|
||||
XMLName: xml.Name{Local: "root"},
|
||||
Name: "NAME",
|
||||
Item: "ITEM",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "version 1.0",
|
||||
input: []byte(`<?xml version="1.0" encoding="UTF-8"?><root name="NAME"><item>ITEM</item></root>`),
|
||||
expected: result{
|
||||
XMLName: xml.Name{Local: "root"},
|
||||
Name: "NAME",
|
||||
Item: "ITEM",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "version 1.1",
|
||||
input: []byte(`<?xml version="1.1" encoding="UTF-8"?><root name="NAME"><item>ITEM</item></root>`),
|
||||
isXML11: true,
|
||||
expected: result{
|
||||
XMLName: xml.Name{Local: "root"},
|
||||
Name: "NAME",
|
||||
Item: "ITEM",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var (
|
||||
r result
|
||||
opts []codec.Option
|
||||
)
|
||||
|
||||
if tt.isXML11 {
|
||||
opts = append(opts, UnmarshalXML11(true))
|
||||
}
|
||||
|
||||
err := NewCodec(opts...).Unmarshal(tt.input, &r)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tt.expected, r)
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user