Compare commits
1 Commits
flacky-tes
...
match-trac
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b05a136b81 |
@@ -4,7 +4,6 @@ package literal
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
|
||||||
|
|
||||||
"github.com/ipfs/go-cid"
|
"github.com/ipfs/go-cid"
|
||||||
"github.com/ipld/go-ipld-prime"
|
"github.com/ipld/go-ipld-prime"
|
||||||
@@ -34,14 +33,8 @@ func Null() ipld.Node {
|
|||||||
// Map creates an IPLD node from a map[string]any
|
// Map creates an IPLD node from a map[string]any
|
||||||
func Map[T any](m map[string]T) (ipld.Node, error) {
|
func Map[T any](m map[string]T) (ipld.Node, error) {
|
||||||
return qp.BuildMap(basicnode.Prototype.Any, int64(len(m)), func(ma datamodel.MapAssembler) {
|
return qp.BuildMap(basicnode.Prototype.Any, int64(len(m)), func(ma datamodel.MapAssembler) {
|
||||||
// deterministic iteration
|
for k, v := range m {
|
||||||
keys := make([]string, 0, len(m))
|
qp.MapEntry(ma, k, anyAssemble(v))
|
||||||
for key := range m {
|
|
||||||
keys = append(keys, key)
|
|
||||||
}
|
|
||||||
sort.Strings(keys)
|
|
||||||
for _, key := range keys {
|
|
||||||
qp.MapEntry(ma, key, anyAssemble(m[key]))
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -97,14 +90,10 @@ func anyAssemble(val any) qp.Assemble {
|
|||||||
if rt.Key().Kind() != reflect.String {
|
if rt.Key().Kind() != reflect.String {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// deterministic iteration
|
it := rv.MapRange()
|
||||||
keys := rv.MapKeys()
|
|
||||||
sort.Slice(keys, func(i, j int) bool {
|
|
||||||
return keys[i].String() < keys[j].String()
|
|
||||||
})
|
|
||||||
return qp.Map(int64(rv.Len()), func(ma datamodel.MapAssembler) {
|
return qp.Map(int64(rv.Len()), func(ma datamodel.MapAssembler) {
|
||||||
for _, key := range keys {
|
for it.Next() {
|
||||||
qp.MapEntry(ma, key.String(), anyAssemble(rv.MapIndex(key)))
|
qp.MapEntry(ma, it.Key().String(), anyAssemble(it.Value()))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ import (
|
|||||||
"github.com/ipld/go-ipld-prime/must"
|
"github.com/ipld/go-ipld-prime/must"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MatchTrace, if set, will print tracing statements to stdout of the policy matching resolution.
|
||||||
|
var MatchTrace = false
|
||||||
|
|
||||||
// Match determines if the IPLD node satisfies the policy.
|
// Match determines if the IPLD node satisfies the policy.
|
||||||
func (p Policy) Match(node datamodel.Node) bool {
|
func (p Policy) Match(node datamodel.Node) bool {
|
||||||
for _, stmt := range p {
|
for _, stmt := range p {
|
||||||
@@ -59,7 +62,7 @@ const (
|
|||||||
// - matchResultNoData: if the selector didn't match the expected data.
|
// - matchResultNoData: if the selector didn't match the expected data.
|
||||||
// For matchResultTrue and matchResultNoData, the leaf-most (innermost) statement failing to be true is returned,
|
// For matchResultTrue and matchResultNoData, the leaf-most (innermost) statement failing to be true is returned,
|
||||||
// as well as the corresponding root-most encompassing statement.
|
// as well as the corresponding root-most encompassing statement.
|
||||||
func matchStatement(cur Statement, node ipld.Node) (_ matchResult, leafMost Statement) {
|
func matchStatement(cur Statement, node ipld.Node) (output matchResult, leafMost Statement) {
|
||||||
var boolToRes = func(v bool) (matchResult, Statement) {
|
var boolToRes = func(v bool) (matchResult, Statement) {
|
||||||
if v {
|
if v {
|
||||||
return matchResultTrue, nil
|
return matchResultTrue, nil
|
||||||
@@ -67,6 +70,11 @@ func matchStatement(cur Statement, node ipld.Node) (_ matchResult, leafMost Stat
|
|||||||
return matchResultFalse, cur
|
return matchResultFalse, cur
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if MatchTrace {
|
||||||
|
defer func() {
|
||||||
|
fmt.Printf("match %v --> %v\n", cur, matchResToStr(output))
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
switch cur.Kind() {
|
switch cur.Kind() {
|
||||||
case KindEqual:
|
case KindEqual:
|
||||||
@@ -274,3 +282,18 @@ func gt(order int) bool { return order == 1 }
|
|||||||
func gte(order int) bool { return order == 0 || order == 1 }
|
func gte(order int) bool { return order == 0 || order == 1 }
|
||||||
func lt(order int) bool { return order == -1 }
|
func lt(order int) bool { return order == -1 }
|
||||||
func lte(order int) bool { return order == 0 || order == -1 }
|
func lte(order int) bool { return order == 0 || order == -1 }
|
||||||
|
|
||||||
|
func matchResToStr(res matchResult) string {
|
||||||
|
switch res {
|
||||||
|
case matchResultTrue:
|
||||||
|
return "True"
|
||||||
|
case matchResultFalse:
|
||||||
|
return "False"
|
||||||
|
case matchResultNoData:
|
||||||
|
return "NoData"
|
||||||
|
case matchResultOptionalNoData:
|
||||||
|
return "OptionalNoData"
|
||||||
|
default:
|
||||||
|
panic("invalid matchResult")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package selector
|
package selector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -353,6 +354,7 @@ func TestParse(t *testing.T) {
|
|||||||
str := `.foo.["bar"].[138]?.baz[1:]`
|
str := `.foo.["bar"].[138]?.baz[1:]`
|
||||||
sel, err := Parse(str)
|
sel, err := Parse(str)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
printSegments(sel)
|
||||||
require.Equal(t, str, sel.String())
|
require.Equal(t, str, sel.String())
|
||||||
require.Equal(t, 7, len(sel))
|
require.Equal(t, 7, len(sel))
|
||||||
require.False(t, sel[0].Identity())
|
require.False(t, sel[0].Identity())
|
||||||
@@ -402,11 +404,13 @@ func TestParse(t *testing.T) {
|
|||||||
t.Run("non dotted", func(t *testing.T) {
|
t.Run("non dotted", func(t *testing.T) {
|
||||||
_, err := Parse("foo")
|
_, err := Parse("foo")
|
||||||
require.NotNil(t, err)
|
require.NotNil(t, err)
|
||||||
|
fmt.Println(err)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("non quoted", func(t *testing.T) {
|
t.Run("non quoted", func(t *testing.T) {
|
||||||
_, err := Parse(".[foo]")
|
_, err := Parse(".[foo]")
|
||||||
require.NotNil(t, err)
|
require.NotNil(t, err)
|
||||||
|
fmt.Println(err)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("slice with negative start and positive end", func(t *testing.T) {
|
t.Run("slice with negative start and positive end", func(t *testing.T) {
|
||||||
@@ -550,3 +554,9 @@ func TestParse(t *testing.T) {
|
|||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printSegments(s Selector) {
|
||||||
|
for i, seg := range s {
|
||||||
|
fmt.Printf("%d: %s\n", i, seg.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package selector
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -12,6 +13,7 @@ import (
|
|||||||
"github.com/ipld/go-ipld-prime/must"
|
"github.com/ipld/go-ipld-prime/must"
|
||||||
basicnode "github.com/ipld/go-ipld-prime/node/basic"
|
basicnode "github.com/ipld/go-ipld-prime/node/basic"
|
||||||
"github.com/ipld/go-ipld-prime/node/bindnode"
|
"github.com/ipld/go-ipld-prime/node/bindnode"
|
||||||
|
"github.com/ipld/go-ipld-prime/printer"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -85,6 +87,8 @@ func TestSelect(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotEmpty(t, res)
|
require.NotEmpty(t, res)
|
||||||
|
|
||||||
|
fmt.Println(printer.Sprint(res))
|
||||||
|
|
||||||
age := must.Int(must.Node(res.LookupByString("age")))
|
age := must.Int(must.Node(res.LookupByString("age")))
|
||||||
require.Equal(t, int64(alice.Age), age)
|
require.Equal(t, int64(alice.Age), age)
|
||||||
})
|
})
|
||||||
@@ -97,6 +101,8 @@ func TestSelect(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotEmpty(t, res)
|
require.NotEmpty(t, res)
|
||||||
|
|
||||||
|
fmt.Println(printer.Sprint(res))
|
||||||
|
|
||||||
name := must.String(res)
|
name := must.String(res)
|
||||||
require.Equal(t, alice.Name.First, name)
|
require.Equal(t, alice.Name.First, name)
|
||||||
|
|
||||||
@@ -104,6 +110,8 @@ func TestSelect(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotEmpty(t, res)
|
require.NotEmpty(t, res)
|
||||||
|
|
||||||
|
fmt.Println(printer.Sprint(res))
|
||||||
|
|
||||||
name = must.String(res)
|
name = must.String(res)
|
||||||
require.Equal(t, bob.Name.First, name)
|
require.Equal(t, bob.Name.First, name)
|
||||||
})
|
})
|
||||||
@@ -116,6 +124,8 @@ func TestSelect(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotEmpty(t, res)
|
require.NotEmpty(t, res)
|
||||||
|
|
||||||
|
fmt.Println(printer.Sprint(res))
|
||||||
|
|
||||||
name := must.String(res)
|
name := must.String(res)
|
||||||
require.Equal(t, *alice.Name.Middle, name)
|
require.Equal(t, *alice.Name.Middle, name)
|
||||||
|
|
||||||
@@ -132,6 +142,8 @@ func TestSelect(t *testing.T) {
|
|||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Empty(t, res)
|
require.Empty(t, res)
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
require.ErrorAs(t, err, &resolutionerr{}, "error should be a resolution error")
|
require.ErrorAs(t, err, &resolutionerr{}, "error should be a resolution error")
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -152,6 +164,8 @@ func TestSelect(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotEmpty(t, res)
|
require.NotEmpty(t, res)
|
||||||
|
|
||||||
|
fmt.Println(printer.Sprint(res))
|
||||||
|
|
||||||
iname := must.String(must.Node(must.Node(res.LookupByIndex(0)).LookupByString("name")))
|
iname := must.String(must.Node(must.Node(res.LookupByIndex(0)).LookupByString("name")))
|
||||||
require.Equal(t, alice.Interests[0].Name, iname)
|
require.Equal(t, alice.Interests[0].Name, iname)
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
package selector_test
|
package selector_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ipld/go-ipld-prime"
|
"github.com/ipld/go-ipld-prime"
|
||||||
"github.com/ipld/go-ipld-prime/codec/dagjson"
|
"github.com/ipld/go-ipld-prime/codec/dagjson"
|
||||||
|
"github.com/ipld/go-ipld-prime/datamodel"
|
||||||
basicnode "github.com/ipld/go-ipld-prime/node/basic"
|
basicnode "github.com/ipld/go-ipld-prime/node/basic"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/pkg/policy/selector"
|
"github.com/ucan-wg/go-ucan/pkg/policy/selector"
|
||||||
@@ -52,7 +55,7 @@ func TestSupportedForms(t *testing.T) {
|
|||||||
require.NotNil(t, res)
|
require.NotNil(t, res)
|
||||||
|
|
||||||
exp := makeNode(t, tc.Output)
|
exp := makeNode(t, tc.Output)
|
||||||
require.True(t, ipld.DeepEqual(exp, res))
|
equalIPLD(t, exp, res)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,6 +106,23 @@ func TestSupportedForms(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func equalIPLD(t *testing.T, expected datamodel.Node, actual datamodel.Node) bool {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
exp, act := &bytes.Buffer{}, &bytes.Buffer{}
|
||||||
|
if err := dagjson.Encode(expected, exp); err != nil {
|
||||||
|
return assert.Fail(t, "Failed to encode json for expected IPLD node")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := dagjson.Encode(actual, act); err != nil {
|
||||||
|
return assert.Fail(t, "Failed to encode JSON for actual IPLD node")
|
||||||
|
}
|
||||||
|
|
||||||
|
require.JSONEq(t, exp.String(), act.String())
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func makeNode(t *testing.T, dagJsonInput string) ipld.Node {
|
func makeNode(t *testing.T, dagJsonInput string) ipld.Node {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
|
|||||||
@@ -100,6 +100,8 @@ func TestConstructors(t *testing.T) {
|
|||||||
data, err := tkn.ToDagJson(privKey)
|
data, err := tkn.ToDagJson(privKey)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
t.Log(string(data))
|
||||||
|
|
||||||
golden.Assert(t, string(data), "new.dagjson")
|
golden.Assert(t, string(data), "new.dagjson")
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -117,6 +119,8 @@ func TestConstructors(t *testing.T) {
|
|||||||
data, err := tkn.ToDagJson(privKey)
|
data, err := tkn.ToDagJson(privKey)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
t.Log(string(data))
|
||||||
|
|
||||||
golden.Assert(t, string(data), "root.dagjson")
|
golden.Assert(t, string(data), "root.dagjson")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package delegation_test
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ipld/go-ipld-prime"
|
"github.com/ipld/go-ipld-prime"
|
||||||
@@ -35,13 +36,18 @@ func TestSchemaRoundTrip(t *testing.T) {
|
|||||||
cborBytes, id, err := p1.ToSealed(privKey)
|
cborBytes, id, err := p1.ToSealed(privKey)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, newCID, envelope.CIDToBase58BTC(id))
|
assert.Equal(t, newCID, envelope.CIDToBase58BTC(id))
|
||||||
|
fmt.Println("cborBytes length", len(cborBytes))
|
||||||
|
fmt.Println("cbor", string(cborBytes))
|
||||||
|
|
||||||
p2, c2, err := delegation.FromSealed(cborBytes)
|
p2, c2, err := delegation.FromSealed(cborBytes)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, id, c2)
|
assert.Equal(t, id, c2)
|
||||||
|
fmt.Println("read Cbor", p2)
|
||||||
|
|
||||||
readJson, err := p2.ToDagJson(privKey)
|
readJson, err := p2.ToDagJson(privKey)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
fmt.Println("readJson length", len(readJson))
|
||||||
|
fmt.Println("json: ", string(readJson))
|
||||||
|
|
||||||
assert.JSONEq(t, string(delegationJson), string(readJson))
|
assert.JSONEq(t, string(delegationJson), string(readJson))
|
||||||
})
|
})
|
||||||
@@ -59,6 +65,7 @@ func TestSchemaRoundTrip(t *testing.T) {
|
|||||||
|
|
||||||
cborBytes := &bytes.Buffer{}
|
cborBytes := &bytes.Buffer{}
|
||||||
id, err := p1.ToSealedWriter(cborBytes, privKey)
|
id, err := p1.ToSealedWriter(cborBytes, privKey)
|
||||||
|
t.Log(len(id.Bytes()), id.Bytes())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, newCID, envelope.CIDToBase58BTC(id))
|
assert.Equal(t, newCID, envelope.CIDToBase58BTC(id))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user