-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/jamtime
- Loading branch information
Showing
7 changed files
with
117 additions
and
1 deletion.
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
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,6 @@ | ||
package codec | ||
|
||
type Codec interface { | ||
Marshal(v interface{}) ([]byte, error) | ||
Unmarshal(data []byte, v interface{}) error | ||
} |
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,16 @@ | ||
package codec | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
// JSONCodec implements the Codec interface for JSON encoding and decoding. | ||
type JSONCodec struct{} | ||
|
||
func (j *JSONCodec) Marshal(v interface{}) ([]byte, error) { | ||
return json.Marshal(v) | ||
} | ||
|
||
func (j *JSONCodec) Unmarshal(data []byte, v interface{}) error { | ||
return json.Unmarshal(data, v) | ||
} |
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,14 @@ | ||
package codec | ||
|
||
import "github.com/ChainSafe/gossamer/pkg/scale" | ||
|
||
// SCALECodec implements the Codec interface for SCALE encoding and decoding. | ||
type SCALECodec struct{} | ||
|
||
func (s *SCALECodec) Marshal(v interface{}) ([]byte, error) { | ||
return scale.Marshal(v) | ||
} | ||
|
||
func (s *SCALECodec) Unmarshal(data []byte, v interface{}) error { | ||
return scale.Unmarshal(data, v) | ||
} |
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,23 @@ | ||
package serialization | ||
|
||
import "github.com/eigerco/strawberry/pkg/serialization/codec" | ||
|
||
// Serializer provides methods to encode and decode using a specified codec. | ||
type Serializer struct { | ||
codec codec.Codec | ||
} | ||
|
||
// NewSerializer initializes a new Serializer with the given codec. | ||
func NewSerializer(c codec.Codec) *Serializer { | ||
return &Serializer{codec: c} | ||
} | ||
|
||
// Encode serializes the given value using the codec. | ||
func (s *Serializer) Encode(v interface{}) ([]byte, error) { | ||
return s.codec.Marshal(v) | ||
} | ||
|
||
// Decode deserializes the given data into the specified value using the codec. | ||
func (s *Serializer) Decode(data []byte, v interface{}) error { | ||
return s.codec.Unmarshal(data, v) | ||
} |
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,52 @@ | ||
package serialization_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/eigerco/strawberry/pkg/serialization" | ||
"github.com/eigerco/strawberry/pkg/serialization/codec" | ||
) | ||
|
||
type PayloadExample struct { | ||
ID int `json:"id"` | ||
Data []byte `json:"data"` | ||
} | ||
|
||
func TestJSONSerializer(t *testing.T) { | ||
jsonCodec := &codec.JSONCodec{} | ||
serializer := serialization.NewSerializer(jsonCodec) | ||
|
||
example := PayloadExample{ID: 1, Data: []byte{1, 2, 3}} | ||
|
||
// Test Encoding | ||
encoded, err := serializer.Encode(&example) | ||
require.NoError(t, err) | ||
require.NotNil(t, encoded) | ||
|
||
// Test Decoding | ||
var decoded PayloadExample | ||
err = serializer.Decode(encoded, &decoded) | ||
require.NoError(t, err) | ||
assert.Equal(t, example, decoded) | ||
} | ||
|
||
func TestSCALESerializer(t *testing.T) { | ||
scaleCodec := &codec.SCALECodec{} | ||
serializer := serialization.NewSerializer(scaleCodec) | ||
|
||
example := PayloadExample{ID: 2, Data: []byte{1, 2, 3}} | ||
|
||
// Test Encoding | ||
encoded, err := serializer.Encode(example) | ||
require.NoError(t, err) | ||
require.NotNil(t, encoded) | ||
|
||
// Test Decoding | ||
var decoded PayloadExample | ||
err = serializer.Decode(encoded, &decoded) | ||
require.NoError(t, err) | ||
assert.Equal(t, example, decoded) | ||
} |