-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(serializer): remove contract (#33)
* refactor(serializer): remove contract * refactor(serializer): remove contract
- Loading branch information
Showing
7 changed files
with
100 additions
and
19 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Serializer | ||
|
||
## Usage | ||
|
||
```go | ||
package serializer_test | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/go-packagist/go-kratos-components/serializer/json" | ||
) | ||
|
||
var j = json.Serializer | ||
|
||
func ExampleJSON() { | ||
bytes, err := j.Serialize(map[string]string{ | ||
"key": "value", | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
var dest map[string]string | ||
err = j.Unserialize(bytes, &dest) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
``` |
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,24 @@ | ||
package serializer_test | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/go-packagist/go-kratos-components/serializer/json" | ||
) | ||
|
||
var j = json.Serializer | ||
|
||
func ExampleJSON() { | ||
bytes, err := j.Serialize(map[string]string{ | ||
"key": "value", | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
var dest map[string]string | ||
err = j.Unserialize(bytes, &dest) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package json | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSerializer(t *testing.T) { | ||
var j1, j2 = Serializer, Serializer | ||
|
||
assert.Same(t, j1, j2) | ||
|
||
var data = map[string]interface{}{ | ||
"foo": "bar", | ||
} | ||
|
||
// marshal | ||
bytes1, err := json.Marshal(data) | ||
assert.NoError(t, err) | ||
|
||
bytes2, err := j1.Serialize(data) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, bytes1, bytes2) | ||
|
||
// unmarshal | ||
var dest1, dest2 = make(map[string]interface{}), make(map[string]interface{}) | ||
assert.NoError(t, json.Unmarshal(bytes1, &dest1)) | ||
assert.NoError(t, j1.Unserialize(bytes1, &dest2)) | ||
|
||
assert.Equal(t, dest1, dest2) | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package contract | ||
package serializer | ||
|
||
type Serializable interface { | ||
// Serialize the given data into bytes. | ||
Serialize(data interface{}) ([]byte, error) | ||
|
||
// Unserialize the given bytes into the given data. | ||
Unserialize(src []byte, dest interface{}) error | ||
} |