Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions encoders/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package encoders

type Encoder interface {
Encode(val any) ([]byte, error)
Decode(data []byte, val any) error
Name() string
}
15 changes: 15 additions & 0 deletions encoders/json.go
Original file line number Diff line number Diff line change
@@ -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" }
15 changes: 15 additions & 0 deletions encoders/rlp.go
Original file line number Diff line number Diff line change
@@ -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" }
15 changes: 15 additions & 0 deletions encoders/yaml.go
Original file line number Diff line number Diff line change
@@ -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" }
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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 (
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
14 changes: 14 additions & 0 deletions helpers/test_setups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions tests/encoders_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading