delegation: make the examples more examply, less testy

This commit is contained in:
Michael Muré
2024-10-14 12:19:33 +02:00
parent 100a510097
commit 5f2877f0ff
6 changed files with 201 additions and 103 deletions

View File

@@ -4,10 +4,12 @@ import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/ipld/go-ipld-prime/printer"
)
var ErrUnsupported = errors.New("failure adding unsupported type to meta")
@@ -139,6 +141,25 @@ func (m *Meta) Equals(other *Meta) bool {
return true
}
func (m *Meta) String() string {
buf := strings.Builder{}
buf.WriteString("{")
var i int
for key, node := range m.Values {
if i > 0 {
buf.WriteString(", ")
}
i++
buf.WriteString(key)
buf.WriteString(":")
buf.WriteString(printer.Sprint(node))
}
buf.WriteString("}")
return buf.String()
}
func fqtn(val any) string {
var name string

View File

@@ -3,7 +3,11 @@ package policy
// https://github.com/ucan-wg/delegation/blob/4094d5878b58f5d35055a3b93fccda0b8329ebae/README.md#policy
import (
"fmt"
"strings"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ucan-wg/go-ucan/pkg/policy/selector"
)
@@ -24,8 +28,20 @@ const (
type Policy []Statement
func (p Policy) String() string {
if len(p) == 0 {
return "[]"
}
childs := make([]string, len(p))
for i, statement := range p {
childs[i] = strings.ReplaceAll(statement.String(), "\n", "\n ")
}
return fmt.Sprintf("[\n %s\n]", strings.Join(childs, ",\n "))
}
type Statement interface {
Kind() string
String() string
}
type equality struct {
@@ -38,6 +54,14 @@ func (e equality) Kind() string {
return e.kind
}
func (e equality) String() string {
child, err := ipld.Encode(e.value, dagjson.Encode)
if err != nil {
return "ERROR: INVALID VALUE"
}
return fmt.Sprintf(`["%s", "%s", %s]`, e.kind, e.selector, strings.ReplaceAll(string(child), "\n", "\n "))
}
func Equal(selector selector.Selector, value ipld.Node) Statement {
return equality{kind: KindEqual, selector: selector, value: value}
}
@@ -66,6 +90,11 @@ func (n negation) Kind() string {
return KindNot
}
func (n negation) String() string {
child := n.statement.String()
return fmt.Sprintf(`["%s", "%s"]`, n.Kind(), strings.ReplaceAll(child, "\n", "\n "))
}
func Not(stmt Statement) Statement {
return negation{statement: stmt}
}
@@ -79,6 +108,14 @@ func (c connective) Kind() string {
return c.kind
}
func (c connective) String() string {
childs := make([]string, len(c.statements))
for i, statement := range c.statements {
childs[i] = strings.ReplaceAll(statement.String(), "\n", "\n ")
}
return fmt.Sprintf("[\"%s\", [\n %s]]\n", c.kind, strings.Join(childs, ",\n "))
}
func And(stmts ...Statement) Statement {
return connective{kind: KindAnd, statements: stmts}
}
@@ -96,6 +133,10 @@ func (n wildcard) Kind() string {
return KindLike
}
func (n wildcard) String() string {
return fmt.Sprintf(`["%s", "%s", "%s"]`, n.Kind(), n.selector, n.pattern)
}
func Like(selector selector.Selector, pattern string) (Statement, error) {
g, err := parseGlob(pattern)
if err != nil {
@@ -105,6 +146,14 @@ func Like(selector selector.Selector, pattern string) (Statement, error) {
return wildcard{selector: selector, pattern: g}, nil
}
func MustLike(selector selector.Selector, pattern string) Statement {
g, err := Like(selector, pattern)
if err != nil {
panic(err)
}
return g
}
type quantifier struct {
kind string
selector selector.Selector
@@ -115,6 +164,11 @@ func (n quantifier) Kind() string {
return n.kind
}
func (n quantifier) String() string {
child := n.statement.String()
return fmt.Sprintf("[\"%s\", \"%s\",\n %s]", n.Kind(), n.selector, strings.ReplaceAll(child, "\n", "\n "))
}
func All(selector selector.Selector, statement Statement) Statement {
return quantifier{kind: KindAll, selector: selector, statement: statement}
}

View File

@@ -27,7 +27,7 @@ func Parse(str string) (Selector, error) {
if len(sel) > 0 && sel[len(sel)-1].Identity() {
return nil, newParseError("selector contains unsupported recursive descent segment: '..'", str, col, tok)
}
sel = append(sel, Identity)
sel = append(sel, segment{".", true, false, false, nil, "", 0})
case "[]":
sel = append(sel, segment{tok, false, opt, true, nil, "", 0})
default:

View File

@@ -23,7 +23,7 @@ func (s Selector) String() string {
return res.String()
}
var Identity = segment{".", true, false, false, nil, "", 0}
var Identity = MustParse(".")
var (
indexRegex = regexp.MustCompile(`^-?\d+$`)