Skip to content

Commit

Permalink
feat(codec): Added msgpack codec (#214)
Browse files Browse the repository at this point in the history
Signed-off-by: Flc゛ <[email protected]>
  • Loading branch information
flc1125 authored Apr 19, 2024
1 parent 2df80e2 commit a61eeef
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions codec/msgpack/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package msgpack

import (
"github.com/vmihailenco/msgpack/v5"

"github.com/go-kratos-ecosystem/components/v2/codec"
)

var Codec codec.Codec = &msgPackCodec{}

type msgPackCodec struct{}

func (j *msgPackCodec) Marshal(data any) ([]byte, error) {
return msgpack.Marshal(data)
}

func (j *msgPackCodec) Unmarshal(src []byte, dest any) error {
return msgpack.Unmarshal(src, dest)
}
25 changes: 25 additions & 0 deletions codec/msgpack/codec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package msgpack

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestJSON(t *testing.T) {
c1, c2 := Codec, Codec

assert.Same(t, c1, c2)

data := map[string]interface{}{
"foo": "bar",
}

// marshal
bytes, err := c1.Marshal(data)
assert.NoError(t, err)

// unmarshal
dest := make(map[string]any)
assert.NoError(t, c1.Unmarshal(bytes, &dest))
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/redis/go-redis/v9 v9.5.1
github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.9.0
github.com/vmihailenco/msgpack/v5 v5.4.1
golang.org/x/term v0.19.0
gorm.io/driver/mysql v1.5.6
gorm.io/gorm v1.25.9
Expand Down Expand Up @@ -53,6 +54,7 @@ require (
github.com/rivo/uniseg v0.4.7 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.22.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc=
golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
Expand Down

0 comments on commit a61eeef

Please sign in to comment.