Skip to content

Commit 1d06fea

Browse files
committed
fix: remove must
1 parent 9433f1d commit 1d06fea

4 files changed

Lines changed: 19 additions & 26 deletions

File tree

examples/capability_definition_test.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/alanshaw/ucantone/examples/types"
88
"github.com/alanshaw/ucantone/ipld/datamodel"
99
"github.com/alanshaw/ucantone/principal/ed25519"
10-
"github.com/alanshaw/ucantone/ucan/command"
1110
"github.com/alanshaw/ucantone/ucan/delegation"
1211
"github.com/alanshaw/ucantone/ucan/delegation/policy"
1312
"github.com/alanshaw/ucantone/ucan/invocation"
@@ -19,9 +18,9 @@ func TestCapabilityDefinition(t *testing.T) {
1918
// you get a typed Delegate and Invoke method (see below).
2019
// i.e. the args parameter for those methods is the type you define here.
2120
messageSendCapability, err := capability.New[*types.MessageSendArguments](
22-
must(command.Parse("/message/send")),
23-
capability.WithPolicy(
24-
must(policy.Build(policy.NotEqual(".to", []string{}))),
21+
"/message/send",
22+
capability.WithPolicyBuilder(
23+
policy.NotEqual(".to", []string{}),
2524
),
2625
)
2726
if err != nil {
@@ -76,7 +75,7 @@ func TestCapabilityDefinitionGenericMap(t *testing.T) {
7675
// i.e. no information about what keys can be added and no type information
7776
// for values.
7877
messageSendCapability, err := capability.New[*datamodel.Map](
79-
must(command.Parse("/message/send")),
78+
"/message/send",
8079
capability.WithPolicyBuilder(
8180
policy.NotEqual(".to", []string{}),
8281
),
@@ -127,10 +126,3 @@ func TestCapabilityDefinitionGenericMap(t *testing.T) {
127126
// Now, send the invocation to the service. You'll probably want to put the
128127
// invocation and delegation in a Container and send a HTTP request...
129128
}
130-
131-
func must[T any](v T, err error) T {
132-
if err != nil {
133-
panic(err)
134-
}
135-
return v
136-
}

examples/container_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/alanshaw/ucantone/ipld"
88
"github.com/alanshaw/ucantone/principal/ed25519"
9-
"github.com/alanshaw/ucantone/ucan/command"
109
"github.com/alanshaw/ucantone/ucan/container"
1110
"github.com/alanshaw/ucantone/ucan/delegation"
1211
"github.com/alanshaw/ucantone/ucan/delegation/policy"
@@ -30,7 +29,7 @@ func TestContainer(t *testing.T) {
3029
dlg, err := delegation.Delegate(
3130
mailer,
3231
alice,
33-
must(command.Parse("/message/send")),
32+
"/message/send",
3433
delegation.WithSubject(mailer),
3534
delegation.WithPolicyBuilder(
3635
policy.All(".to", policy.Like(".", "*.example.com")),
@@ -45,7 +44,7 @@ func TestContainer(t *testing.T) {
4544
inv, err := invocation.Invoke(
4645
alice,
4746
mailer,
48-
must(command.Parse("/message/send")),
47+
"/message/send",
4948
ipld.Map{
5049
"to": []string{"bob@example.com"},
5150
"subject": "Hello!",

examples/delegations_test.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"testing"
55

66
"github.com/alanshaw/ucantone/principal/ed25519"
7-
"github.com/alanshaw/ucantone/ucan/command"
87
"github.com/alanshaw/ucantone/ucan/delegation"
98
"github.com/alanshaw/ucantone/ucan/delegation/policy"
109
)
@@ -30,26 +29,23 @@ func TestDelegations(t *testing.T) {
3029
_, err = delegation.Delegate(
3130
mailer,
3231
alice,
33-
must(command.Parse("/message/send")),
32+
"/message/send",
3433
delegation.WithSubject(mailer),
3534
)
3635
if err != nil {
3736
panic(err)
3837
}
3938

40-
// alice delegates bob capability to use the email service, but only allows
41-
// bob to send to example.com email addresses
42-
policy, err := policy.Build(policy.All(".to", policy.Like(".", "*.example.com")))
43-
if err != nil {
44-
panic(err)
45-
}
46-
4739
_, err = delegation.Delegate(
4840
alice,
4941
bob,
50-
must(command.Parse("/message/send")),
42+
"/message/send",
5143
delegation.WithSubject(mailer),
52-
delegation.WithPolicy(policy),
44+
// alice delegates bob capability to use the email service, but only allows
45+
// bob to send to example.com email addresses
46+
delegation.WithPolicyBuilder(
47+
policy.All(".to", policy.Like(".", "*.example.com")),
48+
),
5349
)
5450
if err != nil {
5551
panic(err)

validator/capability/capability.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package capability
22

33
import (
44
"errors"
5+
"fmt"
56
"reflect"
67

78
"github.com/alanshaw/ucantone/ipld"
89
"github.com/alanshaw/ucantone/ipld/codec/dagcbor"
910
"github.com/alanshaw/ucantone/ipld/datamodel"
1011
"github.com/alanshaw/ucantone/ucan"
12+
"github.com/alanshaw/ucantone/ucan/command"
1113
"github.com/alanshaw/ucantone/ucan/delegation"
1214
"github.com/alanshaw/ucantone/ucan/delegation/policy"
1315
"github.com/alanshaw/ucantone/ucan/invocation"
@@ -72,6 +74,10 @@ func New[A Arguments](cmd ucan.Command, options ...Option) (*Capability[A], erro
7274
return nil, err
7375
}
7476
}
77+
cmd, err := command.Parse(string(cmd))
78+
if err != nil {
79+
return nil, fmt.Errorf("parsing command: %w", err)
80+
}
7581
return &Capability[A]{cmd, cfg.pol}, nil
7682
}
7783

0 commit comments

Comments
 (0)