diff --git a/capability/command/command.go b/pkg/command/command.go similarity index 100% rename from capability/command/command.go rename to pkg/command/command.go diff --git a/capability/command/command_errors.go b/pkg/command/command_errors.go similarity index 100% rename from capability/command/command_errors.go rename to pkg/command/command_errors.go diff --git a/capability/command/command_test.go b/pkg/command/command_test.go similarity index 98% rename from capability/command/command_test.go rename to pkg/command/command_test.go index bf878b7..fc896df 100644 --- a/capability/command/command_test.go +++ b/pkg/command/command_test.go @@ -5,7 +5,7 @@ import ( "github.com/stretchr/testify/require" - "github.com/ucan-wg/go-ucan/capability/command" + "github.com/ucan-wg/go-ucan/pkg/command" ) func TestTop(t *testing.T) { diff --git a/pkg/meta/meta.go b/pkg/meta/meta.go index 176c5fe..9dd8dbb 100644 --- a/pkg/meta/meta.go +++ b/pkg/meta/meta.go @@ -2,12 +2,16 @@ package meta import ( "errors" + "fmt" + "reflect" "github.com/ipld/go-ipld-prime" "github.com/ipld/go-ipld-prime/datamodel" "github.com/ipld/go-ipld-prime/node/basicnode" ) +var ErrUnsupported = errors.New("failure adding unsupported type to meta") + var ErrNotFound = errors.New("key-value not found in meta") // Meta is a container for meta key-value pairs in a UCAN token. @@ -113,8 +117,20 @@ func (m *Meta) Add(key string, val any) error { case datamodel.Node: m.Values[key] = val default: - panic("invalid value type") + return fmt.Errorf("%w: %s", ErrUnsupported, fqtn(val)) } m.Keys = append(m.Keys, key) return nil } + +func fqtn(val any) string { + var name string + + t := reflect.TypeOf(val) + for t.Kind() == reflect.Pointer { + name += "*" + t = t.Elem() + } + + return name + t.PkgPath() + "." + t.Name() +} diff --git a/pkg/meta/meta_test.go b/pkg/meta/meta_test.go new file mode 100644 index 0000000..7b1d994 --- /dev/null +++ b/pkg/meta/meta_test.go @@ -0,0 +1,23 @@ +package meta_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/ucan-wg/go-ucan/pkg/meta" + "gotest.tools/v3/assert" +) + +func TestMeta_Add(t *testing.T) { + t.Parallel() + + type Unsupported struct{} + + t.Run("error if not primative or Node", func(t *testing.T) { + t.Parallel() + + err := (&meta.Meta{}).Add("invalid", &Unsupported{}) + require.ErrorIs(t, err, meta.ErrUnsupported) + assert.ErrorContains(t, err, "*github.com/ucan-wg/go-ucan/pkg/meta_test.Unsupported") + }) +} diff --git a/capability/policy/glob.go b/pkg/policy/glob.go similarity index 100% rename from capability/policy/glob.go rename to pkg/policy/glob.go diff --git a/capability/policy/glob_test.go b/pkg/policy/glob_test.go similarity index 100% rename from capability/policy/glob_test.go rename to pkg/policy/glob_test.go diff --git a/capability/policy/ipld.go b/pkg/policy/ipld.go similarity index 99% rename from capability/policy/ipld.go rename to pkg/policy/ipld.go index 2aea8d8..e3c67d1 100644 --- a/capability/policy/ipld.go +++ b/pkg/policy/ipld.go @@ -9,7 +9,7 @@ import ( "github.com/ipld/go-ipld-prime/must" "github.com/ipld/go-ipld-prime/node/basicnode" - "github.com/ucan-wg/go-ucan/capability/policy/selector" + "github.com/ucan-wg/go-ucan/pkg/policy/selector" ) func FromIPLD(node datamodel.Node) (Policy, error) { diff --git a/capability/policy/ipld_errors.go b/pkg/policy/ipld_errors.go similarity index 100% rename from capability/policy/ipld_errors.go rename to pkg/policy/ipld_errors.go diff --git a/capability/policy/ipld_test.go b/pkg/policy/ipld_test.go similarity index 100% rename from capability/policy/ipld_test.go rename to pkg/policy/ipld_test.go diff --git a/capability/policy/literal/literal.go b/pkg/policy/literal/literal.go similarity index 100% rename from capability/policy/literal/literal.go rename to pkg/policy/literal/literal.go diff --git a/capability/policy/match.go b/pkg/policy/match.go similarity index 98% rename from capability/policy/match.go rename to pkg/policy/match.go index 5313af4..33f5829 100644 --- a/capability/policy/match.go +++ b/pkg/policy/match.go @@ -8,7 +8,7 @@ import ( "github.com/ipld/go-ipld-prime/datamodel" "github.com/ipld/go-ipld-prime/must" - "github.com/ucan-wg/go-ucan/capability/policy/selector" + "github.com/ucan-wg/go-ucan/pkg/policy/selector" ) // Match determines if the IPLD node matches the policy document. diff --git a/capability/policy/match_test.go b/pkg/policy/match_test.go similarity index 99% rename from capability/policy/match_test.go rename to pkg/policy/match_test.go index 4f7dc9b..7daf72e 100644 --- a/capability/policy/match_test.go +++ b/pkg/policy/match_test.go @@ -11,8 +11,8 @@ import ( "github.com/ipld/go-ipld-prime/node/basicnode" "github.com/stretchr/testify/require" - "github.com/ucan-wg/go-ucan/capability/policy/literal" - "github.com/ucan-wg/go-ucan/capability/policy/selector" + "github.com/ucan-wg/go-ucan/pkg/policy/literal" + "github.com/ucan-wg/go-ucan/pkg/policy/selector" ) func TestMatch(t *testing.T) { diff --git a/capability/policy/policy.go b/pkg/policy/policy.go similarity index 98% rename from capability/policy/policy.go rename to pkg/policy/policy.go index e6f385e..7acac17 100644 --- a/capability/policy/policy.go +++ b/pkg/policy/policy.go @@ -5,7 +5,7 @@ package policy import ( "github.com/ipld/go-ipld-prime" - "github.com/ucan-wg/go-ucan/capability/policy/selector" + "github.com/ucan-wg/go-ucan/pkg/policy/selector" ) const ( diff --git a/capability/policy/selector/parsing.go b/pkg/policy/selector/parsing.go similarity index 100% rename from capability/policy/selector/parsing.go rename to pkg/policy/selector/parsing.go diff --git a/capability/policy/selector/selector.go b/pkg/policy/selector/selector.go similarity index 100% rename from capability/policy/selector/selector.go rename to pkg/policy/selector/selector.go diff --git a/capability/policy/selector/selector_test.go b/pkg/policy/selector/selector_test.go similarity index 100% rename from capability/policy/selector/selector_test.go rename to pkg/policy/selector/selector_test.go diff --git a/capability/policy/selector/supported_test.go b/pkg/policy/selector/supported_test.go similarity index 98% rename from capability/policy/selector/supported_test.go rename to pkg/policy/selector/supported_test.go index de8f83c..a7e9917 100644 --- a/capability/policy/selector/supported_test.go +++ b/pkg/policy/selector/supported_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/ucan-wg/go-ucan/capability/policy/selector" + "github.com/ucan-wg/go-ucan/pkg/policy/selector" ) // TestSupported Forms runs tests against the Selector according to the diff --git a/delegation/delegation.go b/tokens/delegation/delegation.go similarity index 98% rename from delegation/delegation.go rename to tokens/delegation/delegation.go index df45491..83a8555 100644 --- a/delegation/delegation.go +++ b/tokens/delegation/delegation.go @@ -18,10 +18,10 @@ import ( "github.com/ipfs/go-cid" "github.com/libp2p/go-libp2p/core/crypto" - "github.com/ucan-wg/go-ucan/capability/command" - "github.com/ucan-wg/go-ucan/capability/policy" "github.com/ucan-wg/go-ucan/did" + "github.com/ucan-wg/go-ucan/pkg/command" "github.com/ucan-wg/go-ucan/pkg/meta" + "github.com/ucan-wg/go-ucan/pkg/policy" ) // Token is an immutable type that holds the fields of a UCAN delegation. diff --git a/delegation/delegation.ipldsch b/tokens/delegation/delegation.ipldsch similarity index 100% rename from delegation/delegation.ipldsch rename to tokens/delegation/delegation.ipldsch diff --git a/delegation/delegation_test.go b/tokens/delegation/delegation_test.go similarity index 95% rename from delegation/delegation_test.go rename to tokens/delegation/delegation_test.go index 20c14ae..0e307bd 100644 --- a/delegation/delegation_test.go +++ b/tokens/delegation/delegation_test.go @@ -8,10 +8,10 @@ import ( "github.com/stretchr/testify/require" "gotest.tools/v3/golden" - "github.com/ucan-wg/go-ucan/capability/command" - "github.com/ucan-wg/go-ucan/capability/policy" - "github.com/ucan-wg/go-ucan/delegation" "github.com/ucan-wg/go-ucan/did" + "github.com/ucan-wg/go-ucan/pkg/command" + "github.com/ucan-wg/go-ucan/pkg/policy" + "github.com/ucan-wg/go-ucan/tokens/delegation" ) const ( diff --git a/delegation/ipld.go b/tokens/delegation/ipld.go similarity index 99% rename from delegation/ipld.go rename to tokens/delegation/ipld.go index 540f4c2..5a76b10 100644 --- a/delegation/ipld.go +++ b/tokens/delegation/ipld.go @@ -12,7 +12,7 @@ import ( "github.com/libp2p/go-libp2p/core/crypto" "github.com/ucan-wg/go-ucan/did" - "github.com/ucan-wg/go-ucan/internal/envelope" + "github.com/ucan-wg/go-ucan/tokens/internal/envelope" ) // ToSealed wraps the delegation token in an envelope, generates the diff --git a/delegation/schema.go b/tokens/delegation/schema.go similarity index 97% rename from delegation/schema.go rename to tokens/delegation/schema.go index c442caf..fe702a6 100644 --- a/delegation/schema.go +++ b/tokens/delegation/schema.go @@ -10,8 +10,8 @@ import ( "github.com/ipld/go-ipld-prime/node/bindnode" "github.com/ipld/go-ipld-prime/schema" - "github.com/ucan-wg/go-ucan/internal/envelope" "github.com/ucan-wg/go-ucan/pkg/meta" + "github.com/ucan-wg/go-ucan/tokens/internal/envelope" ) // [Tag] is the string used as a key within the SigPayload that identifies diff --git a/delegation/schema_test.go b/tokens/delegation/schema_test.go similarity index 97% rename from delegation/schema_test.go rename to tokens/delegation/schema_test.go index 73a68d8..fbfc919 100644 --- a/delegation/schema_test.go +++ b/tokens/delegation/schema_test.go @@ -11,8 +11,8 @@ import ( "github.com/stretchr/testify/require" "gotest.tools/v3/golden" - "github.com/ucan-wg/go-ucan/delegation" - "github.com/ucan-wg/go-ucan/internal/envelope" + "github.com/ucan-wg/go-ucan/tokens/delegation" + "github.com/ucan-wg/go-ucan/tokens/internal/envelope" ) //go:embed delegation.ipldsch diff --git a/delegation/testdata/new.dagjson b/tokens/delegation/testdata/new.dagjson similarity index 100% rename from delegation/testdata/new.dagjson rename to tokens/delegation/testdata/new.dagjson diff --git a/delegation/testdata/root.dagjson b/tokens/delegation/testdata/root.dagjson similarity index 100% rename from delegation/testdata/root.dagjson rename to tokens/delegation/testdata/root.dagjson diff --git a/internal/envelope/cid.go b/tokens/internal/envelope/cid.go similarity index 100% rename from internal/envelope/cid.go rename to tokens/internal/envelope/cid.go diff --git a/internal/envelope/cid_test.go b/tokens/internal/envelope/cid_test.go similarity index 97% rename from internal/envelope/cid_test.go rename to tokens/internal/envelope/cid_test.go index 46f62d0..fd4be1b 100644 --- a/internal/envelope/cid_test.go +++ b/tokens/internal/envelope/cid_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/require" "gotest.tools/v3/golden" - "github.com/ucan-wg/go-ucan/internal/envelope" + "github.com/ucan-wg/go-ucan/tokens/internal/envelope" ) func TestCidFromBytes(t *testing.T) { diff --git a/internal/envelope/example_test.go b/tokens/internal/envelope/example_test.go similarity index 98% rename from internal/envelope/example_test.go rename to tokens/internal/envelope/example_test.go index f8fa05a..4db1a4e 100644 --- a/internal/envelope/example_test.go +++ b/tokens/internal/envelope/example_test.go @@ -16,7 +16,7 @@ import ( "github.com/ipld/go-ipld-prime/schema" "github.com/libp2p/go-libp2p/core/crypto" "github.com/stretchr/testify/require" - "github.com/ucan-wg/go-ucan/internal/envelope" + "github.com/ucan-wg/go-ucan/tokens/internal/envelope" "gotest.tools/v3/golden" ) diff --git a/internal/envelope/ipld.go b/tokens/internal/envelope/ipld.go similarity index 99% rename from internal/envelope/ipld.go rename to tokens/internal/envelope/ipld.go index 553bb30..aef4afc 100644 --- a/internal/envelope/ipld.go +++ b/tokens/internal/envelope/ipld.go @@ -41,7 +41,7 @@ import ( "github.com/libp2p/go-libp2p/core/crypto" "github.com/ucan-wg/go-ucan/did" - "github.com/ucan-wg/go-ucan/internal/varsig" + "github.com/ucan-wg/go-ucan/tokens/internal/varsig" ) const varsigHeaderKey = "h" diff --git a/internal/envelope/ipld_test.go b/tokens/internal/envelope/ipld_test.go similarity index 98% rename from internal/envelope/ipld_test.go rename to tokens/internal/envelope/ipld_test.go index 12d7d73..7c8dcc2 100644 --- a/internal/envelope/ipld_test.go +++ b/tokens/internal/envelope/ipld_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/ucan-wg/go-ucan/internal/envelope" + "github.com/ucan-wg/go-ucan/tokens/internal/envelope" "gotest.tools/v3/golden" ) diff --git a/internal/envelope/testdata/example.dagcbor b/tokens/internal/envelope/testdata/example.dagcbor similarity index 100% rename from internal/envelope/testdata/example.dagcbor rename to tokens/internal/envelope/testdata/example.dagcbor diff --git a/internal/envelope/testdata/example.dagjson b/tokens/internal/envelope/testdata/example.dagjson similarity index 100% rename from internal/envelope/testdata/example.dagjson rename to tokens/internal/envelope/testdata/example.dagjson diff --git a/internal/envelope/testdata/example.ipldsch b/tokens/internal/envelope/testdata/example.ipldsch similarity index 100% rename from internal/envelope/testdata/example.ipldsch rename to tokens/internal/envelope/testdata/example.ipldsch diff --git a/internal/varsig/varsig.go b/tokens/internal/varsig/varsig.go similarity index 100% rename from internal/varsig/varsig.go rename to tokens/internal/varsig/varsig.go diff --git a/internal/varsig/varsig_test.go b/tokens/internal/varsig/varsig_test.go similarity index 94% rename from internal/varsig/varsig_test.go rename to tokens/internal/varsig/varsig_test.go index 56c8bb5..a1ab1cd 100644 --- a/internal/varsig/varsig_test.go +++ b/tokens/internal/varsig/varsig_test.go @@ -8,7 +8,7 @@ import ( "github.com/libp2p/go-libp2p/core/crypto/pb" "github.com/stretchr/testify/assert" - "github.com/ucan-wg/go-ucan/internal/varsig" + "github.com/ucan-wg/go-ucan/tokens/internal/varsig" ) func TestDecode(t *testing.T) {