9 Commits
v4.1.0 ... v4

Author SHA1 Message Date
vtolstov
e4bdf71b70 Apply Code Coverage Badge 2025-05-13 21:04:19 +00:00
9f81375698 add unit-tests (#137)
All checks were successful
coverage / build (push) Successful in 1m36s
test / test (push) Successful in 1m54s
2025-05-13 23:58:09 +03:00
vtolstov
b020114dc5 Apply Code Coverage Badge 2025-05-13 16:54:53 +00:00
e37ddb00df add options
Some checks failed
coverage / build (push) Successful in 1m18s
test / test (push) Failing after 15m38s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-05-13 19:53:58 +03:00
vtolstov
c3e541a87e Apply Code Coverage Badge 2025-05-13 15:24:08 +00:00
cda26c49c8 allow to replace xml version 1.1 to 1.0 on the fly
Some checks failed
coverage / build (push) Successful in 1m1s
test / test (push) Failing after 16m19s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-05-13 18:22:44 +03:00
fa93955439 fixup workflows
All checks were successful
sync / sync (push) Successful in 21s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-05-04 16:47:44 +03:00
vtolstov
251b612df0 Apply Code Coverage Badge
Some checks failed
coverage / build (push) Successful in 3m10s
test / test (push) Failing after 13m8s
sync / sync (push) Has been cancelled
2025-04-27 18:26:13 +00:00
0a7eced607 improve sync
Some checks failed
sync / sync (push) Has started running
coverage / build (push) Has been cancelled
test / test (push) Has been cancelled
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-04-27 21:25:33 +03:00
17 changed files with 323 additions and 27 deletions

53
.github/workflows/job_coverage.yml vendored Normal file
View File

@@ -0,0 +1,53 @@
name: coverage
on:
push:
branches: [ main, v3, v4 ]
paths-ignore:
- '.github/**'
- '.gitea/**'
pull_request:
branches: [ main, v3, v4 ]
jobs:
build:
if: github.server_url != 'https://github.com'
runs-on: ubuntu-latest
steps:
- name: checkout code
uses: actions/checkout@v4
with:
filter: 'blob:none'
- name: setup go
uses: actions/setup-go@v5
with:
cache-dependency-path: "**/*.sum"
go-version: 'stable'
- name: test coverage
run: |
go test -v -cover ./... -covermode=count -coverprofile coverage.out -coverpkg ./...
go tool cover -func coverage.out -o coverage.out
- name: coverage badge
uses: tj-actions/coverage-badge-go@v2
with:
green: 80
filename: coverage.out
- uses: stefanzweifel/git-auto-commit-action@v4
name: autocommit
with:
commit_message: Apply Code Coverage Badge
skip_fetch: false
skip_checkout: false
file_pattern: ./README.md
- name: push
if: steps.auto-commit-action.outputs.changes_detected == 'true'
uses: ad-m/github-push-action@master
with:
github_token: ${{ github.token }}
branch: ${{ github.ref }}

View File

@@ -3,10 +3,10 @@ name: lint
on: on:
pull_request: pull_request:
types: [opened, reopened, synchronize] types: [opened, reopened, synchronize]
branches: branches: [ master, v3, v4 ]
- master paths-ignore:
- v3 - '.github/**'
- v4 - '.gitea/**'
jobs: jobs:
lint: lint:
@@ -24,6 +24,6 @@ jobs:
- name: setup deps - name: setup deps
run: go get -v ./... run: go get -v ./...
- name: run lint - name: run lint
uses: https://github.com/golangci/golangci-lint-action@v6 uses: golangci/golangci-lint-action@v6
with: with:
version: 'latest' version: 'latest'

52
.github/workflows/job_sync.yml vendored Normal file
View File

@@ -0,0 +1,52 @@
name: sync
on:
schedule:
- cron: '*/5 * * * *'
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
sync:
if: github.server_url != 'https://github.com'
runs-on: ubuntu-latest
steps:
- name: init
run: |
git config --global user.email "vtolstov <vtolstov@users.noreply.github.com>"
git config --global user.name "github-actions[bot]"
echo "machine git.unistack.org login vtolstov password ${{ secrets.TOKEN_GITEA }}" >> /root/.netrc
echo "machine github.com login vtolstov password ${{ secrets.TOKEN_GITHUB }}" >> /root/.netrc
- name: sync master
run: |
git clone --filter=blob:none --filter=tree:0 --branch master --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
cd repo
git remote add --no-tags --fetch --track master upstream https://github.com/${GITHUB_REPOSITORY}
git pull --rebase upstream master
git push upstream master --progress
git push origin master --progress
cd ../
rm -rf repo
- name: sync v3
run: |
git clone --filter=blob:none --filter=tree:0 --branch v3 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
cd repo
git remote add --no-tags --fetch --track v3 upstream https://github.com/${GITHUB_REPOSITORY}
git pull --rebase upstream v3
git push upstream v3 --progress
git push origin v3 --progress
cd ../
rm -rf repo
- name: sync v4
run: |
git clone --filter=blob:none --filter=tree:0 --branch v4 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
cd repo
git remote add --no-tags --fetch --track v4 upstream https://github.com/${GITHUB_REPOSITORY}
git pull --rebase upstream v4
git push upstream v4 --progress
git push origin v4 --progress
cd ../
rm -rf repo

View File

@@ -3,15 +3,12 @@ name: test
on: on:
pull_request: pull_request:
types: [opened, reopened, synchronize] types: [opened, reopened, synchronize]
branches: branches: [ master, v3, v4 ]
- master
- v3
- v4
push: push:
branches: branches: [ master, v3, v4 ]
- master paths-ignore:
- v3 - '.github/**'
- v4 - '.gitea/**'
jobs: jobs:
test: test:

View File

@@ -3,15 +3,12 @@ name: test
on: on:
pull_request: pull_request:
types: [opened, reopened, synchronize] types: [opened, reopened, synchronize]
branches: branches: [ master, v3, v4 ]
- master
- v3
- v4
push: push:
branches: branches: [ master, v3, v4 ]
- master paths-ignore:
- v3 - '.github/**'
- v4 - '.gitea/**'
jobs: jobs:
test: test:
@@ -35,19 +32,19 @@ jobs:
go-version: 'stable' go-version: 'stable'
- name: setup go work - name: setup go work
env: env:
GOWORK: /workspace/${{ github.repository_owner }}/go.work GOWORK: ${{ github.workspace }}/go.work
run: | run: |
go work init go work init
go work use . go work use .
go work use micro-tests go work use micro-tests
- name: setup deps - name: setup deps
env: env:
GOWORK: /workspace/${{ github.repository_owner }}/go.work GOWORK: ${{ github.workspace }}/go.work
run: go get -v ./... run: go get -v ./...
- name: run tests - name: run tests
env: env:
INTEGRATION_TESTS: yes INTEGRATION_TESTS: yes
GOWORK: /workspace/${{ github.repository_owner }}/go.work GOWORK: ${{ github.workspace }}/go.work
run: | run: |
cd micro-tests cd micro-tests
go test -mod readonly -v ./... || true go test -mod readonly -v ./... || true

5
.golangci.yml Normal file
View File

@@ -0,0 +1,5 @@
run:
concurrency: 8
timeout: 5m
issues-exit-code: 1
tests: true

View File

@@ -1 +1,2 @@
# micro-codec-xml # micro-codec-xml
![Coverage](https://img.shields.io/badge/Coverage-62.3%25-yellow)

4
go.mod
View File

@@ -2,18 +2,20 @@ module go.unistack.org/micro-codec-xml/v4
go 1.22.0 go 1.22.0
require ( require (
github.com/stretchr/testify v1.10.0
go.unistack.org/micro-proto/v4 v4.1.0 go.unistack.org/micro-proto/v4 v4.1.0
go.unistack.org/micro/v4 v4.1.3 go.unistack.org/micro/v4 v4.1.3
) )
require ( require (
github.com/ash3in/uuidv8 v1.2.0 // indirect github.com/ash3in/uuidv8 v1.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // 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/google/uuid v1.6.0 // indirect
github.com/kr/pretty v0.3.1 // indirect github.com/kr/pretty v0.3.1 // indirect
github.com/matoous/go-nanoid v1.5.1 // indirect github.com/matoous/go-nanoid v1.5.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
google.golang.org/protobuf v1.36.3 // 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
) )

6
go.sum
View File

@@ -3,6 +3,8 @@ github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ
github.com/ash3in/uuidv8 v1.2.0 h1:2oogGdtCPwaVtyvPPGin4TfZLtOGE5F+W++E880G6SI= 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/ash3in/uuidv8 v1.2.0/go.mod h1:BnU0wJBxnzdEKmVg4xckBkD+VZuecTFTUP3M0dWgyY4=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/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 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/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
@@ -14,9 +16,13 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/matoous/go-nanoid v1.5.1 h1:aCjdvTyO9LLnTIi0fgdXhOPPvOHjpXN6Ik9DaNjIct4= github.com/matoous/go-nanoid v1.5.1 h1:aCjdvTyO9LLnTIi0fgdXhOPPvOHjpXN6Ik9DaNjIct4=
github.com/matoous/go-nanoid v1.5.1/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U= github.com/matoous/go-nanoid v1.5.1/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
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/v4 v4.1.0 h1:qPwL2n/oqh9RE3RTTDgt28XK3QzV597VugQPaw9lKUk= 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-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 h1:9QHiLHBTfPtfKyoY3HsLPNVSK0wlAAoMV+9saLz2iR4=

40
reader_test.go Normal file
View 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])
})
}
}

75
xml.go
View File

@@ -2,7 +2,9 @@
package xml package xml
import ( import (
"bytes"
"encoding/xml" "encoding/xml"
"io"
pb "go.unistack.org/micro-proto/v4/codec" pb "go.unistack.org/micro-proto/v4/codec"
"go.unistack.org/micro/v4/codec" "go.unistack.org/micro/v4/codec"
@@ -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
View File

@@ -0,0 +1,70 @@
package xml
import (
"encoding/xml"
"testing"
"github.com/stretchr/testify/require"
"go.unistack.org/micro/v4/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)
})
}
}