mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-19 21:18:07 +02:00
encoding/xml: add xml encode support (#905)
* encoding/xml: add xml encode support * encoding/xml: avoid nil pointer panic * action: avoid go get golint pollution go.mod
This commit is contained in:
parent
eb732c4de0
commit
7e7bbdbed6
10
.github/workflows/go.yml
vendored
10
.github/workflows/go.yml
vendored
@ -24,11 +24,6 @@ jobs:
|
|||||||
- name: Test
|
- name: Test
|
||||||
run: go test -v ./...
|
run: go test -v ./...
|
||||||
|
|
||||||
- name: Lint
|
|
||||||
run: |
|
|
||||||
go get golang.org/x/lint/golint
|
|
||||||
golint ./...
|
|
||||||
|
|
||||||
- name: Kratos
|
- name: Kratos
|
||||||
run: |
|
run: |
|
||||||
cd cmd/kratos
|
cd cmd/kratos
|
||||||
@ -52,3 +47,8 @@ jobs:
|
|||||||
cd examples
|
cd examples
|
||||||
go build ./...
|
go build ./...
|
||||||
go test ./...
|
go test ./...
|
||||||
|
|
||||||
|
- name: Lint
|
||||||
|
run: |
|
||||||
|
go get golang.org/x/lint/golint
|
||||||
|
golint ./...
|
||||||
|
37
encoding/xml/xml.go
Normal file
37
encoding/xml/xml.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package xml
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/go-kratos/kratos/v2/encoding"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Name is the name registered for the xml codec.
|
||||||
|
const Name = "xml"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
encoding.RegisterCodec(codec{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// codec is a Codec implementation with xml.
|
||||||
|
type codec struct{}
|
||||||
|
|
||||||
|
func (codec) Marshal(v interface{}) ([]byte, error) {
|
||||||
|
return xml.Marshal(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec) Unmarshal(data []byte, v interface{}) error {
|
||||||
|
rv := reflect.ValueOf(v)
|
||||||
|
for rv.Kind() == reflect.Ptr {
|
||||||
|
if rv.IsNil() {
|
||||||
|
rv.Set(reflect.New(rv.Type().Elem()))
|
||||||
|
}
|
||||||
|
rv = rv.Elem()
|
||||||
|
}
|
||||||
|
return xml.Unmarshal(data, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec) Name() string {
|
||||||
|
return Name
|
||||||
|
}
|
@ -6,6 +6,7 @@ import (
|
|||||||
// init encoding
|
// init encoding
|
||||||
_ "github.com/go-kratos/kratos/v2/encoding/json"
|
_ "github.com/go-kratos/kratos/v2/encoding/json"
|
||||||
_ "github.com/go-kratos/kratos/v2/encoding/proto"
|
_ "github.com/go-kratos/kratos/v2/encoding/proto"
|
||||||
|
_ "github.com/go-kratos/kratos/v2/encoding/xml"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Server is transport server.
|
// Server is transport server.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user