feat(args): export fluent builder

This commit is contained in:
Steve Moyer
2024-11-27 12:05:00 -05:00
parent ce1a4b6e32
commit 170e597e71
6 changed files with 176 additions and 55 deletions

View File

@@ -1,8 +1,6 @@
package policytest
import (
"errors"
"github.com/ipld/go-ipld-prime"
"github.com/ucan-wg/go-ucan/pkg/args"
"github.com/ucan-wg/go-ucan/pkg/policy"
@@ -12,12 +10,12 @@ import (
// EmptyPolicy provides a Policy with no statements.
var EmptyPolicy = policy.Policy{}
// ExampleValidationPolicy provides a instantiated Policy containing the
// ExampleValidationPolicy provides a instantiated SpecPolicy containing the
// statements that are included in the second code block of the [Validation]
// section of the delegation specification.
//
// [Validation]: https://github.com/ucan-wg/delegation/tree/v1_ipld#validation
var ExamplePolicy = policy.MustConstruct(
var SpecPolicy = policy.MustConstruct(
policy.Equal(".from", literal.String("alice@example.com")),
policy.Any(".to", policy.Like(".", "*@example.com")),
)
@@ -25,68 +23,39 @@ var ExamplePolicy = policy.MustConstruct(
// TODO: Replace the URL for [Validation] above when the delegation
// specification has been finished/merged.
// ExampleValidArguments provides valid, instantiated Arguments containing
// SpecValidArguments provides valid, instantiated Arguments containing
// the key/value pairs that are included in portion of the the second code
// block of the [Validation] section of the delegation specification.
//
// [Validation]: https://github.com/ucan-wg/delegation/tree/v1_ipld#validation
var ExampleValidArguments = newBuilder(nil).
add("from", "alice@example.com").
add("to", []string{
var SpecValidArguments = args.NewBuilder().
Add("from", "alice@example.com").
Add("to", []string{
"bob@example.com",
"carol@not.example.com",
}).
add("title", "Coffee").
add("body", "Still on for coffee").
mustBuild()
Add("title", "Coffee").
Add("body", "Still on for coffee").
MustBuild()
var exampleValidArgumentsIPLD = mustIPLD(ExampleValidArguments)
var specValidArgumentsIPLD = mustIPLD(SpecValidArguments)
// ExampleInvalidArguments provides invalid, instantiated Arguments containing
// SpecInvalidArguments provides invalid, instantiated Arguments containing
// the key/value pairs that are included in portion of the the second code
// block of the [Validation] section of the delegation specification.
//
// [Validation]: https://github.com/ucan-wg/delegation/tree/v1_ipld#validation
var ExampleInvalidArguments = newBuilder(nil).
add("from", "alice@example.com").
add("to", []string{
var SpecInvalidArguments = args.NewBuilder().
Add("from", "alice@example.com").
Add("to", []string{
"bob@null.com",
"carol@elsewhere.example.com",
}).
add("title", "Coffee").
add("body", "Still on for coffee").
mustBuild()
Add("title", "Coffee").
Add("body", "Still on for coffee").
MustBuild()
var exampleInvalidArgumentsIPLD = mustIPLD(ExampleInvalidArguments)
type builder struct {
args *args.Args
errs error
}
func newBuilder(a *args.Args) *builder {
if a == nil {
a = args.New()
}
return &builder{
args: a,
}
}
func (b *builder) add(key string, val any) *builder {
b.errs = errors.Join(b.errs, b.args.Add(key, val))
return b
}
func (b *builder) mustBuild() *args.Args {
if b.errs != nil {
panic(b.errs)
}
return b.args
}
var specInvalidArgumentsIPLD = mustIPLD(SpecInvalidArguments)
func mustIPLD(args *args.Args) ipld.Node {
node, err := args.ToIPLD()

View File

@@ -17,7 +17,7 @@ func TestInvocationValidationSpecExamples(t *testing.T) {
t.Run("with passing args", func(t *testing.T) {
t.Parallel()
exec, stmt := ExamplePolicy.Match(exampleValidArgumentsIPLD)
exec, stmt := SpecPolicy.Match(specValidArgumentsIPLD)
assert.True(t, exec)
assert.Nil(t, stmt)
})
@@ -25,7 +25,7 @@ func TestInvocationValidationSpecExamples(t *testing.T) {
t.Run("fails on recipients (second statement)", func(t *testing.T) {
t.Parallel()
exec, stmt := ExamplePolicy.Match(exampleInvalidArgumentsIPLD)
exec, stmt := SpecPolicy.Match(specInvalidArgumentsIPLD)
assert.False(t, exec)
assert.NotNil(t, stmt)
})