generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logic): uri_encoded/3 detect component used
- Loading branch information
Showing
2 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package predicate | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ichiban/prolog/engine" | ||
) | ||
|
||
type Component string | ||
|
||
const ( | ||
QueryComponent Component = "query" | ||
FragmentComponent Component = "fragment" | ||
PathComponent Component = "path" | ||
SegmentComponent Component = "segment" | ||
) | ||
|
||
func NewComponent(v string) (Component, error) { | ||
switch v { | ||
case string(QueryComponent): | ||
return QueryComponent, nil | ||
case string(FragmentComponent): | ||
return FragmentComponent, nil | ||
case string(PathComponent): | ||
return PathComponent, nil | ||
case string(SegmentComponent): | ||
return SegmentComponent, nil | ||
default: | ||
return "", fmt.Errorf("invalid component name %s, expected `query`, `fragment`, `path` or `segment`", v) | ||
} | ||
} | ||
|
||
func URIEncoded(vm *engine.VM, component, decoded, encoded engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise { | ||
return engine.Delay(func(ctx context.Context) *engine.Promise { | ||
var comp Component | ||
switch c := env.Resolve(component).(type) { | ||
case engine.Atom: | ||
_, err := NewComponent(c.String()) | ||
if err != nil { | ||
return engine.Error(fmt.Errorf("uri_encoded/3: %w", err)) | ||
} | ||
default: | ||
return engine.Error(fmt.Errorf("uri_encoded/3: invalid component type: %T, should be Atom", component)) | ||
} | ||
|
||
fmt.Printf("%s", comp) | ||
|
||
return engine.Bool(true) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package predicate | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
tmdb "github.com/cometbft/cometbft-db" | ||
"github.com/cometbft/cometbft/libs/log" | ||
tmproto "github.com/cometbft/cometbft/proto/tendermint/types" | ||
"github.com/cosmos/cosmos-sdk/store" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/ichiban/prolog/engine" | ||
"github.com/okp4/okp4d/x/logic/testutil" | ||
"github.com/okp4/okp4d/x/logic/types" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestURIEncoded(t *testing.T) { | ||
Convey("Given a test cases", t, func() { | ||
cases := []struct { | ||
program string | ||
query string | ||
wantResult []types.TermResults | ||
wantError error | ||
wantSuccess bool | ||
}{ | ||
{ | ||
query: `uri_encoded(hey, foo, Decoded).`, | ||
wantSuccess: false, | ||
wantError: fmt.Errorf("uri_encoded/3: invalid component name hey, expected `query`, `fragment`, `path` or `segment`"), | ||
}, | ||
} | ||
for nc, tc := range cases { | ||
Convey(fmt.Sprintf("Given the query #%d: %s", nc, tc.query), func() { | ||
Convey("and a context", func() { | ||
db := tmdb.NewMemDB() | ||
stateStore := store.NewCommitMultiStore(db) | ||
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger()) | ||
|
||
Convey("and a vm", func() { | ||
interpreter := testutil.NewLightInterpreterMust(ctx) | ||
interpreter.Register3(engine.NewAtom("uri_encoded"), URIEncoded) | ||
|
||
err := interpreter.Compile(ctx, tc.program) | ||
So(err, ShouldBeNil) | ||
|
||
Convey("When the predicate is called", func() { | ||
sols, err := interpreter.QueryContext(ctx, tc.query) | ||
|
||
Convey("Then the error should be nil", func() { | ||
So(err, ShouldBeNil) | ||
So(sols, ShouldNotBeNil) | ||
|
||
Convey("and the bindings should be as expected", func() { | ||
var got []types.TermResults | ||
for sols.Next() { | ||
m := types.TermResults{} | ||
err := sols.Scan(m) | ||
So(err, ShouldBeNil) | ||
|
||
got = append(got, m) | ||
} | ||
if tc.wantError != nil { | ||
So(sols.Err(), ShouldNotBeNil) | ||
So(sols.Err().Error(), ShouldEqual, tc.wantError.Error()) | ||
} else { | ||
So(sols.Err(), ShouldBeNil) | ||
|
||
if tc.wantSuccess { | ||
So(len(got), ShouldBeGreaterThan, 0) | ||
So(len(got), ShouldEqual, len(tc.wantResult)) | ||
for iGot, resultGot := range got { | ||
for varGot, termGot := range resultGot { | ||
So(testutil.ReindexUnknownVariables(termGot), ShouldEqual, tc.wantResult[iGot][varGot]) | ||
} | ||
} | ||
} else { | ||
So(len(got), ShouldEqual, 0) | ||
} | ||
} | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
}) | ||
} |