Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
b42eafe69f | |||
0f33dbcfaf | |||
43b5b349e6 |
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
name: Bug report
|
name: Bug report
|
||||||
about: For reporting bugs in micro
|
about: For reporting bugs in go-micro
|
||||||
title: "[BUG]"
|
title: "[BUG]"
|
||||||
labels: ''
|
labels: ''
|
||||||
assignees: ''
|
assignees: ''
|
||||||
@@ -16,3 +16,9 @@ assignees: ''
|
|||||||
**How to reproduce the bug:**
|
**How to reproduce the bug:**
|
||||||
|
|
||||||
If possible, please include a minimal code snippet here.
|
If possible, please include a minimal code snippet here.
|
||||||
|
|
||||||
|
**Environment:**
|
||||||
|
Go Version: please paste `go version` output here
|
||||||
|
```
|
||||||
|
please paste `go env` output here
|
||||||
|
```
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
name: Feature request / Enhancement
|
name: Feature request / Enhancement
|
||||||
about: If you have a need not served by micro
|
about: If you have a need not served by go-micro
|
||||||
title: "[FEATURE]"
|
title: "[FEATURE]"
|
||||||
labels: ''
|
labels: ''
|
||||||
assignees: ''
|
assignees: ''
|
||||||
|
@@ -1,8 +1,14 @@
|
|||||||
---
|
---
|
||||||
name: Question
|
name: Question
|
||||||
about: Ask a question about micro
|
about: Ask a question about go-micro
|
||||||
title: ''
|
title: ''
|
||||||
labels: ''
|
labels: ''
|
||||||
assignees: ''
|
assignees: ''
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
Before asking, please check if your question has already been answered:
|
||||||
|
|
||||||
|
1. Check the documentation - https://micro.mu/docs/
|
||||||
|
2. Check the examples and plugins - https://github.com/micro/examples & https://github.com/micro/go-plugins
|
||||||
|
3. Search existing issues
|
||||||
|
@@ -1,28 +0,0 @@
|
|||||||
name: "autoapprove"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
workflow_run:
|
|
||||||
workflows: ["prbuild"]
|
|
||||||
types:
|
|
||||||
- completed
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
autoapprove:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: approve
|
|
||||||
run: [ "curl -o tea https://dl.gitea.com/tea/main/tea-main-linux-amd64",
|
|
||||||
"chmod +x ./tea",
|
|
||||||
"./tea login add --name unistack --token ${{ secrets.GITHUB_TOKEN }} --url https://git.unistack.org",
|
|
||||||
"./tea pr --repo ${{ github.event.repository.name }}"
|
|
||||||
]
|
|
||||||
if: github.actor == 'vtolstov'
|
|
||||||
id: approve
|
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
@@ -4,7 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"go.unistack.org/micro/v4/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFrame(t *testing.T) {
|
func TestFrame(t *testing.T) {
|
||||||
|
18
go.mod
18
go.mod
@@ -1,19 +1,19 @@
|
|||||||
module go.unistack.org/micro-codec-xml/v4
|
module go.unistack.org/micro-codec-xml/v3
|
||||||
|
|
||||||
go 1.22.0
|
go 1.21
|
||||||
|
|
||||||
|
toolchain go1.23.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
go.unistack.org/micro-proto/v4 v4.1.0
|
github.com/stretchr/testify v1.10.0
|
||||||
go.unistack.org/micro/v4 v4.1.3
|
go.unistack.org/micro-proto/v3 v3.4.1
|
||||||
|
go.unistack.org/micro/v3 v3.10.88
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ash3in/uuidv8 v1.2.0 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/google/go-cmp v0.6.0 // indirect
|
github.com/google/go-cmp v0.6.0 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/kr/pretty v0.3.1 // indirect
|
google.golang.org/protobuf v1.35.2 // indirect
|
||||||
github.com/matoous/go-nanoid v1.5.1 // indirect
|
|
||||||
google.golang.org/protobuf v1.36.3 // indirect
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
38
go.sum
38
go.sum
@@ -1,30 +1,18 @@
|
|||||||
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/ash3in/uuidv8 v1.2.0 h1:2oogGdtCPwaVtyvPPGin4TfZLtOGE5F+W++E880G6SI=
|
|
||||||
github.com/ash3in/uuidv8 v1.2.0/go.mod h1:BnU0wJBxnzdEKmVg4xckBkD+VZuecTFTUP3M0dWgyY4=
|
|
||||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
|
||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
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/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
||||||
github.com/matoous/go-nanoid v1.5.1 h1:aCjdvTyO9LLnTIi0fgdXhOPPvOHjpXN6Ik9DaNjIct4=
|
go.unistack.org/micro/v3 v3.10.88 h1:MxlzP+77Y6Kphb3lzHxROL4XfE/WdCQMQpnPv4D9Z8U=
|
||||||
github.com/matoous/go-nanoid v1.5.1/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U=
|
go.unistack.org/micro/v3 v3.10.88/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
|
||||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
|
||||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
|
||||||
go.unistack.org/micro-proto/v4 v4.1.0 h1:qPwL2n/oqh9RE3RTTDgt28XK3QzV597VugQPaw9lKUk=
|
|
||||||
go.unistack.org/micro-proto/v4 v4.1.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
|
|
||||||
go.unistack.org/micro/v4 v4.1.3 h1:9QHiLHBTfPtfKyoY3HsLPNVSK0wlAAoMV+9saLz2iR4=
|
|
||||||
go.unistack.org/micro/v4 v4.1.3/go.mod h1:lr3oYED8Ay1vjK68QqRw30QOtdk/ffpZqMFDasOUhKw=
|
|
||||||
google.golang.org/protobuf v1.36.3 h1:82DV7MYdb8anAVi3qge1wSnMDrnKK7ebr+I0hHRN1BU=
|
|
||||||
google.golang.org/protobuf v1.36.3/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
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])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
83
xml.go
83
xml.go
@@ -1,12 +1,14 @@
|
|||||||
// Package xml provides a xml codec
|
// Package xml provides a xml codec
|
||||||
package xml
|
package xml // import "go.unistack.org/micro-codec-xml/v3"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"io"
|
||||||
|
|
||||||
pb "go.unistack.org/micro-proto/v4/codec"
|
pb "go.unistack.org/micro-proto/v3/codec"
|
||||||
"go.unistack.org/micro/v4/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
rutil "go.unistack.org/micro/v4/util/reflect"
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ codec.Codec = &xmlCodec{}
|
var _ codec.Codec = &xmlCodec{}
|
||||||
@@ -19,6 +21,18 @@ const (
|
|||||||
flattenTag = "flatten"
|
flattenTag = "flatten"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type unmarshalStrictKey struct{}
|
||||||
|
|
||||||
|
func UnmarshalStrict(b bool) codec.Option {
|
||||||
|
return codec.SetOption(unmarshalStrictKey{}, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
type unmarshalXML11Key struct{}
|
||||||
|
|
||||||
|
func UnmarshalXML11(b bool) codec.Option {
|
||||||
|
return codec.SetOption(unmarshalXML11Key{}, b)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *xmlCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
func (c *xmlCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@@ -80,7 +94,66 @@ func (c *xmlCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) erro
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return xml.Unmarshal(b, v)
|
var unmarshalStrict bool
|
||||||
|
var unmarshalXML11 bool
|
||||||
|
if options.Context != nil {
|
||||||
|
if v, ok := options.Context.Value(unmarshalStrictKey{}).(bool); ok {
|
||||||
|
unmarshalStrict = v
|
||||||
|
}
|
||||||
|
if v, ok := options.Context.Value(unmarshalXML11Key{}).(bool); ok {
|
||||||
|
unmarshalXML11 = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var r io.Reader
|
||||||
|
|
||||||
|
if unmarshalXML11 {
|
||||||
|
r = newReader(b)
|
||||||
|
} else {
|
||||||
|
r = bytes.NewReader(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
d := xml.NewDecoder(r)
|
||||||
|
|
||||||
|
if unmarshalStrict {
|
||||||
|
d.Strict = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return d.Decode(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
type reader struct {
|
||||||
|
s []byte
|
||||||
|
i int64 // current reading index
|
||||||
|
prevRune int // index of previous rune; or < 0
|
||||||
|
skip bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func newReader(b []byte) io.Reader {
|
||||||
|
return &reader{b, 0, -1, false}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
srcP = []byte(`<?xml version="1.1"`)
|
||||||
|
srcL = len(srcP)
|
||||||
|
dstP = []byte(`<?xml version="1.0"`)
|
||||||
|
)
|
||||||
|
|
||||||
|
func (r *reader) Read(b []byte) (n int, err error) {
|
||||||
|
if r.i >= int64(len(r.s)) {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
r.prevRune = -1
|
||||||
|
n = copy(b, r.s[r.i:])
|
||||||
|
if !r.skip {
|
||||||
|
if idx := bytes.Index(b, srcP); idx >= 0 {
|
||||||
|
copy(b[idx:idx+srcL], dstP)
|
||||||
|
r.skip = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r.i += int64(n)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *xmlCodec) String() string {
|
func (c *xmlCodec) String() string {
|
||||||
|
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