-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codec): Added msgpack codec (#214)
Signed-off-by: Flc゛ <[email protected]>
- Loading branch information
Showing
6 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters