Skip to content

Commit

Permalink
wasi store: change proto store generic type
Browse files Browse the repository at this point in the history
  • Loading branch information
colindickson committed Jan 9, 2024
1 parent 09e4705 commit 3f380de
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions wasm/wasi/substreams/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"math/big"
"strings"

"google.golang.org/protobuf/proto"
)

type StoreGet[T any] interface {
Expand Down Expand Up @@ -156,36 +158,29 @@ func (s *baseStore) readFromFile(path string) ([]byte, error) {
return s.fileReadWriter.ReadFile(path)
}

//TODO: check this type. is this what we want? do we want to support VT?
type ProtoMarhsaler interface {
Marshal() ([]byte, error)
Unmarshal([]byte) error
}

//TODO: DO WE EVEN NEED THIS???
type protoStore[M ProtoMarhsaler] struct {
type protoStore[M proto.Message] struct {
bytesStore *baseStore //for some reason, it does not like to be embedded for this struct. perhaps because of the generic type? not sure. no time to investigate at the moment
}

func NewGetProtoStore[M ProtoMarhsaler](idx uint32) StoreGet[M] {
func NewGetProtoStore[M proto.Message](idx uint32) StoreGet[M] {
return &protoStore[M]{
bytesStore: newBaseStore(idx),
}
}

func NewSetProtoStore[M ProtoMarhsaler](idx uint32) StoreSet[M] {
func NewSetProtoStore[M proto.Message](idx uint32) StoreSet[M] {
return &protoStore[M]{
bytesStore: newBaseStore(idx),
}
}

func NewDeleteProtoStore[M ProtoMarhsaler](idx uint32) StoreDelete {
func NewDeleteProtoStore[M proto.Message](idx uint32) StoreDelete {
return &protoStore[M]{
bytesStore: newBaseStore(idx),
}
}

func newM[M ProtoMarhsaler]() M {
func newM[M proto.Message]() M {
var m M // create a zero value of M
return m
}
Expand Down Expand Up @@ -264,15 +259,15 @@ func (s *protoStore[M]) DeletePrefix(ord uint64, prefix string) error {

func (s *protoStore[M]) into(data []byte) (M, error) {
m := newM[M]()
err := m.Unmarshal(data)
err := proto.Unmarshal(data, m)
if err != nil {
return m, fmt.Errorf("unmarshalling proto: %w", err)
}
return m, nil
}

func (s *protoStore[M]) from(item M) ([]byte, error) {
return item.Marshal()
return proto.Marshal(item)
}

type stringStore struct {
Expand Down

0 comments on commit 3f380de

Please sign in to comment.