feat(invocation): use dedicated type for invocation.Token.Arguments

This commit is contained in:
Steve Moyer
2024-11-07 13:50:20 -05:00
parent 11bc085c60
commit 1fa2b5e6fc
6 changed files with 16 additions and 45 deletions

View File

@@ -30,7 +30,9 @@ func ExampleNew() {
} }
inv, err := invocation.New(iss, sub, cmd, prf, 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("env", "development"),
invocation.WithMeta("tags", meta["tags"]), invocation.WithMeta("tags", meta["tags"]),
invocation.WithExpirationIn(time.Minute), invocation.WithExpirationIn(time.Minute),

View File

@@ -14,9 +14,9 @@ import (
"time" "time"
"github.com/ipfs/go-cid" "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/did"
"github.com/ucan-wg/go-ucan/pkg/args"
"github.com/ucan-wg/go-ucan/pkg/command" "github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/pkg/meta" "github.com/ucan-wg/go-ucan/pkg/meta"
"github.com/ucan-wg/go-ucan/token/internal/parse" "github.com/ucan-wg/go-ucan/token/internal/parse"
@@ -34,7 +34,7 @@ type Token struct {
// The Command // The Command
command command.Command command command.Command
// The Command's Arguments // The Command's Arguments
arguments map[string]datamodel.Node arguments *args.Args
// Delegations that prove the chain of authority // Delegations that prove the chain of authority
proof []cid.Cid proof []cid.Cid
@@ -71,6 +71,7 @@ func New(iss, sub did.DID, cmd command.Command, prf []cid.Cid, opts ...Option) (
issuer: iss, issuer: iss,
subject: sub, subject: sub,
command: cmd, command: cmd,
arguments: args.New(),
proof: prf, proof: prf,
meta: meta.NewMeta(), meta: meta.NewMeta(),
nonce: nil, nonce: nil,
@@ -119,7 +120,7 @@ func (t *Token) Command() command.Command {
// Arguments returns the arguments to be used when the command is // Arguments returns the arguments to be used when the command is
// invoked. // invoked.
func (t *Token) Arguments() map[string]datamodel.Node { func (t *Token) Arguments() *args.Args {
return t.arguments return t.arguments
} }
@@ -204,7 +205,7 @@ func tokenFromModel(m tokenPayloadModel) (*Token, error) {
} }
tkn.nonce = m.Nonce tkn.nonce = m.Nonce
tkn.arguments = m.Args.Values tkn.arguments = m.Args
tkn.proof = m.Prf tkn.proof = m.Prf
tkn.meta = m.Meta tkn.meta = m.Meta

View File

@@ -192,11 +192,6 @@ func FromIPLD(node datamodel.Node) (*Token, error) {
return tkn, err return tkn, err
} }
type stringAny struct {
Keys []string
Values map[string]datamodel.Node
}
func (t *Token) toIPLD(privKey crypto.PrivKey) (datamodel.Node, error) { func (t *Token) toIPLD(privKey crypto.PrivKey) (datamodel.Node, error) {
var aud *string var aud *string
@@ -217,26 +212,12 @@ func (t *Token) toIPLD(privKey crypto.PrivKey) (datamodel.Node, error) {
iat = &i 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{ model := &tokenPayloadModel{
Iss: t.issuer.String(), Iss: t.issuer.String(),
Aud: aud, Aud: aud,
Sub: t.subject.String(), Sub: t.subject.String(),
Cmd: t.command.String(), Cmd: t.command.String(),
Args: args, Args: t.arguments,
Prf: t.proof, Prf: t.proof,
Meta: t.meta, Meta: t.meta,
Nonce: t.nonce, Nonce: t.nonce,

View File

@@ -17,7 +17,9 @@ func TestSealUnsealRoundtrip(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
tkn1, err := invocation.New(iss, sub, cmd, prf, 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("env", "development"),
invocation.WithMeta("tags", meta["tags"]), invocation.WithMeta("tags", meta["tags"]),
invocation.WithExpirationIn(time.Minute), invocation.WithExpirationIn(time.Minute),

View File

@@ -4,7 +4,6 @@ import (
"time" "time"
"github.com/ipfs/go-cid" "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/did"
) )
@@ -14,24 +13,9 @@ import (
type Option func(*Token) error type Option func(*Token) error
// WithArgument adds a key/value pair to the Token's Arguments field. // 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 { return func(t *Token) error {
t.arguments[key] = val return t.arguments.Add(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
} }
} }

View File

@@ -10,6 +10,7 @@ import (
"github.com/ipld/go-ipld-prime/node/bindnode" "github.com/ipld/go-ipld-prime/node/bindnode"
"github.com/ipld/go-ipld-prime/schema" "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/pkg/meta"
"github.com/ucan-wg/go-ucan/token/internal/envelope" "github.com/ucan-wg/go-ucan/token/internal/envelope"
) )
@@ -56,7 +57,7 @@ type tokenPayloadModel struct {
// The Command // The Command
Cmd string Cmd string
// The Command's Arguments // The Command's Arguments
Args stringAny Args *args.Args
// Delegations that prove the chain of authority // Delegations that prove the chain of authority
Prf []cid.Cid Prf []cid.Cid