From 1fa2b5e6fc0f0735a2dde7899213c418bb03ee06 Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Thu, 7 Nov 2024 13:50:20 -0500 Subject: [PATCH] feat(invocation): use dedicated type for invocation.Token.Arguments --- token/invocation/examples_test.go | 4 +++- token/invocation/invocation.go | 9 +++++---- token/invocation/ipld.go | 21 +-------------------- token/invocation/ipld_test.go | 4 +++- token/invocation/options.go | 20 ++------------------ token/invocation/schema.go | 3 ++- 6 files changed, 16 insertions(+), 45 deletions(-) diff --git a/token/invocation/examples_test.go b/token/invocation/examples_test.go index f948041..7ecf349 100644 --- a/token/invocation/examples_test.go +++ b/token/invocation/examples_test.go @@ -30,7 +30,9 @@ func ExampleNew() { } inv, err := invocation.New(iss, sub, cmd, prf, - invocation.WithArguments(args), + invocation.WithArgument("uri", args["uri"]), + invocation.WithArgument("headers", args["headers"]), + invocation.WithArgument("payload", args["payload"]), invocation.WithMeta("env", "development"), invocation.WithMeta("tags", meta["tags"]), invocation.WithExpirationIn(time.Minute), diff --git a/token/invocation/invocation.go b/token/invocation/invocation.go index a1fdcb9..f6eb07a 100644 --- a/token/invocation/invocation.go +++ b/token/invocation/invocation.go @@ -14,9 +14,9 @@ import ( "time" "github.com/ipfs/go-cid" - "github.com/ipld/go-ipld-prime/datamodel" "github.com/ucan-wg/go-ucan/did" + "github.com/ucan-wg/go-ucan/pkg/args" "github.com/ucan-wg/go-ucan/pkg/command" "github.com/ucan-wg/go-ucan/pkg/meta" "github.com/ucan-wg/go-ucan/token/internal/parse" @@ -34,7 +34,7 @@ type Token struct { // The Command command command.Command // The Command's Arguments - arguments map[string]datamodel.Node + arguments *args.Args // Delegations that prove the chain of authority proof []cid.Cid @@ -71,6 +71,7 @@ func New(iss, sub did.DID, cmd command.Command, prf []cid.Cid, opts ...Option) ( issuer: iss, subject: sub, command: cmd, + arguments: args.New(), proof: prf, meta: meta.NewMeta(), nonce: nil, @@ -119,7 +120,7 @@ func (t *Token) Command() command.Command { // Arguments returns the arguments to be used when the command is // invoked. -func (t *Token) Arguments() map[string]datamodel.Node { +func (t *Token) Arguments() *args.Args { return t.arguments } @@ -204,7 +205,7 @@ func tokenFromModel(m tokenPayloadModel) (*Token, error) { } tkn.nonce = m.Nonce - tkn.arguments = m.Args.Values + tkn.arguments = m.Args tkn.proof = m.Prf tkn.meta = m.Meta diff --git a/token/invocation/ipld.go b/token/invocation/ipld.go index b477eb7..acaaf98 100644 --- a/token/invocation/ipld.go +++ b/token/invocation/ipld.go @@ -192,11 +192,6 @@ func FromIPLD(node datamodel.Node) (*Token, error) { return tkn, err } -type stringAny struct { - Keys []string - Values map[string]datamodel.Node -} - func (t *Token) toIPLD(privKey crypto.PrivKey) (datamodel.Node, error) { var aud *string @@ -217,26 +212,12 @@ func (t *Token) toIPLD(privKey crypto.PrivKey) (datamodel.Node, error) { iat = &i } - argsKey := make([]string, len(t.arguments)) - - // TODO: make specialized type and builder? - i := 0 - for k := range t.arguments { - argsKey[i] = k - i++ - } - - args := stringAny{ - Keys: argsKey, - Values: t.arguments, - } - model := &tokenPayloadModel{ Iss: t.issuer.String(), Aud: aud, Sub: t.subject.String(), Cmd: t.command.String(), - Args: args, + Args: t.arguments, Prf: t.proof, Meta: t.meta, Nonce: t.nonce, diff --git a/token/invocation/ipld_test.go b/token/invocation/ipld_test.go index ec3c04f..9754c16 100644 --- a/token/invocation/ipld_test.go +++ b/token/invocation/ipld_test.go @@ -17,7 +17,9 @@ func TestSealUnsealRoundtrip(t *testing.T) { require.NoError(t, err) tkn1, err := invocation.New(iss, sub, cmd, prf, - invocation.WithArguments(args), + invocation.WithArgument("uri", args["uri"]), + invocation.WithArgument("headers", args["headers"]), + invocation.WithArgument("payload", args["payload"]), invocation.WithMeta("env", "development"), invocation.WithMeta("tags", meta["tags"]), invocation.WithExpirationIn(time.Minute), diff --git a/token/invocation/options.go b/token/invocation/options.go index 1c557b9..9322cd7 100644 --- a/token/invocation/options.go +++ b/token/invocation/options.go @@ -4,7 +4,6 @@ import ( "time" "github.com/ipfs/go-cid" - "github.com/ipld/go-ipld-prime/datamodel" "github.com/ucan-wg/go-ucan/did" ) @@ -14,24 +13,9 @@ import ( type Option func(*Token) error // WithArgument adds a key/value pair to the Token's Arguments field. -func WithArgument(key string, val datamodel.Node) Option { +func WithArgument(key string, val any) Option { return func(t *Token) error { - t.arguments[key] = val - - return nil - } -} - -// WithArguments sets the Token's Arguments field to the provided map. -// -// Note that this will overwrite any existing Arguments whether provided -// by a previous call to this function or by one or more calls to -// WithArgument. -func WithArguments(args map[string]datamodel.Node) Option { - return func(t *Token) error { - t.arguments = args - - return nil + return t.arguments.Add(key, val) } } diff --git a/token/invocation/schema.go b/token/invocation/schema.go index b2b99f2..d51cf4f 100644 --- a/token/invocation/schema.go +++ b/token/invocation/schema.go @@ -10,6 +10,7 @@ import ( "github.com/ipld/go-ipld-prime/node/bindnode" "github.com/ipld/go-ipld-prime/schema" + "github.com/ucan-wg/go-ucan/pkg/args" "github.com/ucan-wg/go-ucan/pkg/meta" "github.com/ucan-wg/go-ucan/token/internal/envelope" ) @@ -56,7 +57,7 @@ type tokenPayloadModel struct { // The Command Cmd string // The Command's Arguments - Args stringAny + Args *args.Args // Delegations that prove the chain of authority Prf []cid.Cid