Skip to content

Commit

Permalink
fix(logic): add type safety on interface and rename it corrctly
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Mar 15, 2023
1 parent 9bb1c10 commit 010bcc7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
14 changes: 8 additions & 6 deletions x/logic/interpreter/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ import (
"io/fs"
)

// FileSystem is the custom virtual file system used into the blockchain.
// VirtualFS is the custom virtual file system used into the blockchain.
// It will hold a list of handler that can resolve file URI and return the corresponding binary file.
type FileSystem struct {
type VirtualFS struct {
ctx goctx.Context
router Router
}

// New return a new FileSystem object that will handle all virtual file on the interpreter.
var _ fs.FS = (*VirtualFS)(nil)

// New return a new VirtualFS object that will handle all virtual file on the interpreter.
// File can be provided from different sources like CosmWasm cw-storage smart contract.
func New(ctx goctx.Context, handlers []URIHandler) FileSystem {
func New(ctx goctx.Context, handlers []URIHandler) VirtualFS {
router := NewRouter()
for _, handler := range handlers {
router.RegisterHandler(handler)
}
return FileSystem{
return VirtualFS{
ctx: ctx,
router: router,
}
Expand All @@ -34,7 +36,7 @@ func New(ctx goctx.Context, handlers []URIHandler) FileSystem {
// Open should reject attempts to open names that do not satisfy
// ValidPath(name), returning a *PathError with Err set to
// ErrInvalid or ErrNotExist.
func (f FileSystem) Open(name string) (fs.File, error) {
func (f VirtualFS) Open(name string) (fs.File, error) {
data, err := f.router.Open(f.ctx, name)
if err != nil {
return nil, &fs.PathError{
Expand Down
12 changes: 7 additions & 5 deletions x/logic/interpreter/fs/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ const (
scheme = "cosmwasm"
)

type WasmFS struct {
type WasmHandler struct {
wasmKeeper types.WasmKeeper
}

func NewWasmFS(keeper types.WasmKeeper) WasmFS {
return WasmFS{wasmKeeper: keeper}
var _ URIHandler = (*WasmHandler)(nil)

func NewWasmHandler(keeper types.WasmKeeper) WasmHandler {
return WasmHandler{wasmKeeper: keeper}
}

func (w WasmFS) Scheme() string {
func (w WasmHandler) Scheme() string {
return scheme
}

func (w WasmFS) Open(ctx context.Context, uri *url.URL) (fs.File, error) {
func (w WasmHandler) Open(ctx context.Context, uri *url.URL) (fs.File, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

if uri.Scheme != scheme {
Expand Down
2 changes: 1 addition & 1 deletion x/logic/interpreter/fs/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestWasmHandler(t *testing.T) {
Return(tc.data, nil)

Convey("and wasm handler", func() {
handler := NewWasmFS(wasmKeeper)
handler := NewWasmHandler(wasmKeeper)

Convey("When ask handler if it can open uri", func() {
uri, err := url.Parse(tc.uri)
Expand Down
2 changes: 1 addition & 1 deletion x/logic/keeper/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (k Keeper) newInterpreter(ctx goctx.Context) (*prolog.Interpreter, error) {

interpreterParams := params.GetInterpreter()

wasmHandler := fs.NewWasmFS(k.WasmKeeper)
wasmHandler := fs.NewWasmHandler(k.WasmKeeper)

interpreted, err := interpreter.New(
ctx,
Expand Down

0 comments on commit 010bcc7

Please sign in to comment.