diff --git a/encoders/interface.go b/encoders/interface.go new file mode 100644 index 0000000..c9439ef --- /dev/null +++ b/encoders/interface.go @@ -0,0 +1,7 @@ +package encoders + +type Encoder interface { + Encode(val any) ([]byte, error) + Decode(data []byte, val any) error + Name() string +} diff --git a/encoders/json.go b/encoders/json.go new file mode 100644 index 0000000..043b64a --- /dev/null +++ b/encoders/json.go @@ -0,0 +1,15 @@ +package encoders + +import ( + "encoding/json" +) + +type JsonEncoder struct{} + +func NewJsonEncoder() Encoder { + return &JsonEncoder{} +} + +func (e JsonEncoder) Encode(v any) ([]byte, error) { return json.Marshal(v) } +func (e JsonEncoder) Decode(data []byte, v any) error { return json.Unmarshal(data, v) } +func (e JsonEncoder) Name() string { return "json" } diff --git a/encoders/rlp.go b/encoders/rlp.go new file mode 100644 index 0000000..b0e4755 --- /dev/null +++ b/encoders/rlp.go @@ -0,0 +1,15 @@ +package encoders + +import ( + "github.com/ethereum/go-ethereum/rlp" +) + +type RLPEncoder struct{} + +func NewRLPEncoder() Encoder { + return &RLPEncoder{} +} + +func (e RLPEncoder) Encode(v any) ([]byte, error) { return rlp.EncodeToBytes(v) } +func (e RLPEncoder) Decode(data []byte, v any) error { return rlp.DecodeBytes(data, v) } +func (e RLPEncoder) Name() string { return "rlp" } diff --git a/encoders/yaml.go b/encoders/yaml.go new file mode 100644 index 0000000..21068ae --- /dev/null +++ b/encoders/yaml.go @@ -0,0 +1,15 @@ +package encoders + +import ( + "gopkg.in/yaml.v3" +) + +type YamlEncoder struct{} + +func NewYamlEncoder() Encoder { + return &YamlEncoder{} +} + +func (e YamlEncoder) Encode(v any) ([]byte, error) { return yaml.Marshal(v) } +func (e YamlEncoder) Decode(data []byte, v any) error { return yaml.Unmarshal(data, v) } +func (e YamlEncoder) Name() string { return "yaml" } diff --git a/go.mod b/go.mod index eadaa55..c7c5585 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.25.2 require ( github.com/dgraph-io/badger/v4 v4.8.0 + github.com/ethereum/go-ethereum v1.16.3 github.com/stretchr/testify v1.10.0 ) @@ -33,7 +34,7 @@ require ( github.com/rogpeppe/go-internal v1.13.1 // indirect golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect golang.org/x/text v0.26.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect + gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -44,6 +45,7 @@ require ( github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/google/flatbuffers v25.2.10+incompatible // indirect + github.com/holiman/uint256 v1.3.2 // indirect github.com/klauspost/compress v1.18.0 // indirect go.opentelemetry.io/auto/sdk v1.1.0 // indirect go.opentelemetry.io/otel v1.37.0 // indirect diff --git a/go.sum b/go.sum index 52ed49e..bbbf94d 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,8 @@ github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da h1:aIftn67I1fkbMa5 github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/ethereum/go-ethereum v1.16.3 h1:nDoBSrmsrPbrDIVLTkDQCy1U9KdHN+F2PzvMbDoS42Q= +github.com/ethereum/go-ethereum v1.16.3/go.mod h1:Lrsc6bt9Gm9RyvhfFK53vboCia8kpF9nv+2Ukntnl+8= github.com/getsentry/sentry-go v0.27.0 h1:Pv98CIbtB3LkMWmXi4Joa5OOcwbmnX88sF5qbK3r3Ps= github.com/getsentry/sentry-go v0.27.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY= github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA= @@ -52,6 +54,8 @@ github.com/google/flatbuffers v25.2.10+incompatible h1:F3vclr7C3HpB1k9mxCGRMXq6F github.com/google/flatbuffers v25.2.10+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/holiman/uint256 v1.3.2 h1:a9EgMPSC1AAaj1SZL5zIQD3WbwTuHrMGOerLjGmM/TA= +github.com/holiman/uint256 v1.3.2/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= diff --git a/helpers/test_setups.go b/helpers/test_setups.go index 9d34579..542036b 100644 --- a/helpers/test_setups.go +++ b/helpers/test_setups.go @@ -6,6 +6,7 @@ import ( "github.com/rawbytedev/zerokv" "github.com/rawbytedev/zerokv/badgerdb" + "github.com/rawbytedev/zerokv/encoders" "github.com/rawbytedev/zerokv/memdb" "github.com/rawbytedev/zerokv/pebbledb" ) @@ -33,6 +34,19 @@ func SetupDB(t *testing.T, name string) zerokv.Core { return db } +func SetupEncoders(t *testing.T, name string) encoders.Encoder { + switch name { + case "json": + return encoders.NewJsonEncoder() + case "rlp": + return encoders.NewRLPEncoder() + case "yaml": + return encoders.NewYamlEncoder() + } + t.Fatalf("Failed to create %s encoder", name) + return nil +} + // randomBytes generates a slice of random bytes of specified length. func RandomBytes(n int) []byte { b := make([]byte, n) diff --git a/tests/encoders_test.go b/tests/encoders_test.go new file mode 100644 index 0000000..f2a4f93 --- /dev/null +++ b/tests/encoders_test.go @@ -0,0 +1,49 @@ +package tests + +import ( + "fmt" + "testing" + + "github.com/rawbytedev/zerokv/helpers" + "github.com/stretchr/testify/require" +) + +type TestingStruct struct { + Name string + Data []byte +} + +func GenerateRandom() TestingStruct { + return TestingStruct{Name: "ZeroKv", Data: []byte{0x01, 0x69, 0x52, 0x75}} +} + +func TestEncoders(t *testing.T) { + encoder := []string{ + "json", "yaml", "rlp", + } + test_list := []test{ + { + name: "Encode", + fn: testEncoding, + }, + } + for i := range encoder { + for tt := range test_list { + testname := fmt.Sprintf("%s_%s", test_list[tt].name, encoder[i]) + t.Run(testname, func(t *testing.T) { + test_list[tt].fn(t, encoder[i]) + }) + } + } +} + +func testEncoding(t *testing.T, name string) { + field := GenerateRandom() + field2 := &TestingStruct{} + enc := helpers.SetupEncoders(t, name) + data, err := enc.Encode(field) + require.NoError(t, err) + err = enc.Decode(data, field2) + require.NoError(t, err) + require.EqualExportedValues(t, field, *field2) +}