Skip to content

Commit

Permalink
refactor(logic): replace the parser by a router
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Mar 14, 2023
1 parent 02cc0d1 commit 87e0034
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 60 deletions.
10 changes: 7 additions & 3 deletions x/logic/interpreter/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ import (
// It will hold a list of handler that can resolve file URI and return the corresponding binary file.
type FileSystem struct {
ctx goctx.Context
parser Parser
router Router
}

// New return a new FileSystem 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 {
router := NewRouter()
for _, handler := range handlers {
router.RegisterHandler(handler)
}
return FileSystem{
ctx: ctx,
parser: Parser{handlers},
router: router,
}
}

Expand All @@ -40,7 +44,7 @@ func (f FileSystem) Open(name string) (fs.File, error) {
// ReadFile read the entire file at the uri provided.
// Parse all handler and return the first supported handler file response.
func (f FileSystem) ReadFile(name string) ([]byte, error) {
return f.parser.Parse(f.ctx, name)
return f.router.Open(f.ctx, name)
}

type Object []byte
Expand Down
31 changes: 0 additions & 31 deletions x/logic/interpreter/fs/parser.go

This file was deleted.

40 changes: 40 additions & 0 deletions x/logic/interpreter/fs/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fs

import (
"context"
"fmt"
"net/url"
)

type URIHandler interface {
Scheme() string
Open(ctx context.Context, uri *url.URL) ([]byte, error)
}

type Router struct {
handlers map[string]URIHandler
}

func NewRouter() Router {
return Router{
handlers: make(map[string]URIHandler),
}
}
func (r *Router) Open(ctx context.Context, name string) ([]byte, error) {
uri, err := url.Parse(name)
if err != nil {
return nil, err
}

handler, ok := r.handlers[uri.Scheme]

if !ok {
return nil, fmt.Errorf("could not find handler for load %s file", name)
}

return handler.Open(ctx, uri)
}

func (r *Router) RegisterHandler(handler URIHandler) {
r.handlers[handler.Scheme()] = handler
}
4 changes: 2 additions & 2 deletions x/logic/interpreter/fs/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func NewWasmFS(keeper types.WasmKeeper) WasmFS {
return WasmFS{wasmKeeper: keeper}
}

func (w WasmFS) CanOpen(ctx context.Context, uri *url.URL) bool {
return uri.Scheme == scheme
func (w WasmFS) Scheme() string {
return scheme
}

func (w WasmFS) Open(ctx context.Context, uri *url.URL) ([]byte, error) {
Expand Down
35 changes: 11 additions & 24 deletions x/logic/interpreter/fs/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func TestWasmHandler(t *testing.T) {
contractAddress string
query []byte
data []byte
canOpen bool
uri string
wantResult []byte
wantError error
Expand All @@ -34,15 +33,13 @@ func TestWasmHandler(t *testing.T) {
contractAddress: "okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht",
query: []byte("{\"object_data\":{\"id\": \"4cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05\"}}"),
data: []byte("\"Y2FsYyhYKSA6LSAgWCBpcyAxMDAgKyAyMDAu\""),
canOpen: true,
uri: `cosmwasm:cw-storage:okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht?query=%7B%22object_data%22%3A%7B%22id%22%3A%20%224cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05%22%7D%7D`,
wantResult: []byte("calc(X) :- X is 100 + 200."),
},
{
contractAddress: "okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht",
query: []byte("{\"object_data\":{\"id\": \"4cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05\"}}"),
data: []byte("Y2FsYyhYKSA6LSAgWCBpcyAxMDAgKyAyMDAu"),
canOpen: true,
uri: `cosmwasm:cw-storage:okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht?query=%7B%22object_data%22%3A%7B%22id%22%3A%20%224cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05%22%7D%7D`,
wantResult: []byte("\"\""),
wantError: fmt.Errorf("failed unmarshal json wasm response to string: invalid character 'Y' looking for beginning of value"),
Expand All @@ -51,22 +48,20 @@ func TestWasmHandler(t *testing.T) {
contractAddress: "okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht",
query: []byte("{\"object_data\":{\"id\": \"4cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05\"}}"),
data: []byte("\"Y2FsYyhYKSA6LSAgWCBpcyAxMDAgKyAyMDAu\""),
canOpen: true,
uri: `cosmwasm:okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht?query=%7B%22object_data%22%3A%7B%22id%22%3A%20%224cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05%22%7D%7D`,
wantResult: []byte("calc(X) :- X is 100 + 200."),
},
{
contractAddress: "okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht",
query: []byte("{\"object_data\":{\"id\": \"4cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05\"}}"),
data: []byte("\"Y2FsYyhYKSA6LSAgWCBpcyAxMDAgKyAyMDAu\""),
canOpen: false,
uri: `okp4:okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht?query=%7B%22object_data%22%3A%7B%22id%22%3A%20%224cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05%22%7D%7D`,
wantError: fmt.Errorf("invalid scheme"),
},
{
contractAddress: "okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht",
query: []byte("{\"object_data\":{\"id\": \"4cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05\"}}"),
data: []byte("\"hey\""),
canOpen: true,
uri: `cosmwasm:cw-storage:okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht?query=%7B%22object_data%22%3A%7B%22id%22%3A%20%224cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05%22%7D%7D`,
wantResult: []byte("\"\""),
wantError: fmt.Errorf("failed decode wasm base64 respone: illegal base64 data at input byte 0"),
Expand All @@ -75,7 +70,6 @@ func TestWasmHandler(t *testing.T) {
contractAddress: "okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht",
query: []byte("{\"object_data\":{\"id\": \"4cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05\"}}"),
data: []byte("\"hey\""),
canOpen: true,
uri: `cosmwasm:cw-storage?query=%7B%22object_data%22%3A%7B%22id%22%3A%20%224cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05%22%7D%7D`,
wantResult: []byte("\"\""),
wantError: fmt.Errorf("failed convert path 'cw-storage' to contract address: decoding bech32 failed: invalid separator index -1"),
Expand All @@ -84,7 +78,6 @@ func TestWasmHandler(t *testing.T) {
contractAddress: "okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht",
query: []byte("{\"object_data\":{\"id\": \"4cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05\"}}"),
data: []byte("\"hey\""),
canOpen: true,
uri: `cosmwasm:cw-storage:okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht?wasm=%7B%22object_data%22%3A%7B%22id%22%3A%20%224cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05%22%7D%7D`,
wantResult: []byte("\"\""),
wantError: fmt.Errorf("uri should contains `query` params"),
Expand All @@ -93,7 +86,6 @@ func TestWasmHandler(t *testing.T) {
contractAddress: "okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht",
query: []byte("{\"object_data\":{\"id\": \"4cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05\"}}"),
data: []byte("\"hey\""),
canOpen: true,
uri: `cosmwasm:?query=%7B%22object_data%22%3A%7B%22id%22%3A%20%224cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05%22%7D%7D`,
wantResult: []byte("\"\""),
wantError: fmt.Errorf("emtpy path given, should be 'cosmwasm:{contractName}:{contractAddr}?query={query}'"),
Expand Down Expand Up @@ -122,23 +114,18 @@ func TestWasmHandler(t *testing.T) {

So(err, ShouldBeNil)

result := handler.CanOpen(ctx, uri)
Convey("Then handler response should be as expected", func() {
data, err := handler.Open(ctx, uri)

So(result, ShouldEqual, tc.canOpen)
if tc.wantError != nil {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, tc.wantError.Error())
} else {
So(err, ShouldBeNil)
So(data, ShouldResemble, tc.wantResult)
}
})

if result {
Convey("Then handler response should be as expected", func() {
data, err := handler.Open(ctx, uri)

if tc.wantError != nil {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, tc.wantError.Error())
} else {
So(err, ShouldBeNil)
So(data, ShouldResemble, tc.wantResult)
}
})
}
})
})
})
Expand Down

0 comments on commit 87e0034

Please sign in to comment.