2 Commits

Author SHA1 Message Date
43b5b349e6 allow to replace xml version 1.1 to 1.0 on the fly
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-05-13 18:23:37 +03:00
efb75b8357 add codec.RawMessage support
All checks were successful
test / test (push) Successful in 3m25s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-12-23 20:57:22 +03:00
3 changed files with 59 additions and 1559 deletions

11
go.mod
View File

@@ -1,10 +1,15 @@
module go.unistack.org/micro-codec-xml/v3
go 1.16
go 1.21
toolchain go1.23.4
require (
github.com/imdario/mergo v0.3.13 // indirect
go.unistack.org/micro-proto/v3 v3.4.1
go.unistack.org/micro/v3 v3.10.88
google.golang.org/protobuf v1.34.2 // indirect
)
require (
github.com/google/go-cmp v0.6.0 // indirect
google.golang.org/protobuf v1.35.2 // indirect
)

1559
go.sum

File diff suppressed because it is too large Load Diff

48
xml.go
View File

@@ -2,7 +2,9 @@
package xml // import "go.unistack.org/micro-codec-xml/v3"
import (
"bytes"
"encoding/xml"
"io"
pb "go.unistack.org/micro-proto/v3/codec"
"go.unistack.org/micro/v3/codec"
@@ -40,6 +42,10 @@ func (c *xmlCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error)
return m.Data, nil
case *pb.Frame:
return m.Data, nil
case codec.RawMessage:
return []byte(m), nil
case *codec.RawMessage:
return []byte(*m), nil
}
return xml.Marshal(v)
@@ -68,9 +74,49 @@ func (c *xmlCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) erro
case *pb.Frame:
m.Data = b
return nil
case *codec.RawMessage:
*m = append((*m)[0:0], b...)
return nil
case codec.RawMessage:
copy(m, b)
return nil
}
return xml.Unmarshal(b, v)
return xml.NewDecoder(newReader(b)).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(`version="1.1"`)
srcL = len(srcP)
dstP = []byte(`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 {