feat: reorganize packages

This commit is contained in:
Steve Moyer
2024-09-24 11:40:28 -04:00
parent 5202056cc7
commit 6075c19957
36 changed files with 61 additions and 22 deletions

View File

@@ -5,7 +5,7 @@ import (
"github.com/stretchr/testify/require" "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) { func TestTop(t *testing.T) {

View File

@@ -2,12 +2,16 @@ package meta
import ( import (
"errors" "errors"
"fmt"
"reflect"
"github.com/ipld/go-ipld-prime" "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel" "github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/basicnode" "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") var ErrNotFound = errors.New("key-value not found in meta")
// Meta is a container for meta key-value pairs in a UCAN token. // 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: case datamodel.Node:
m.Values[key] = val m.Values[key] = val
default: default:
panic("invalid value type") return fmt.Errorf("%w: %s", ErrUnsupported, fqtn(val))
} }
m.Keys = append(m.Keys, key) m.Keys = append(m.Keys, key)
return nil 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()
}

23
pkg/meta/meta_test.go Normal file
View File

@@ -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")
})
}

View File

@@ -9,7 +9,7 @@ import (
"github.com/ipld/go-ipld-prime/must" "github.com/ipld/go-ipld-prime/must"
"github.com/ipld/go-ipld-prime/node/basicnode" "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) { func FromIPLD(node datamodel.Node) (Policy, error) {

View File

@@ -8,7 +8,7 @@ import (
"github.com/ipld/go-ipld-prime/datamodel" "github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/must" "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. // Match determines if the IPLD node matches the policy document.

View File

@@ -11,8 +11,8 @@ import (
"github.com/ipld/go-ipld-prime/node/basicnode" "github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/ucan-wg/go-ucan/capability/policy/literal" "github.com/ucan-wg/go-ucan/pkg/policy/literal"
"github.com/ucan-wg/go-ucan/capability/policy/selector" "github.com/ucan-wg/go-ucan/pkg/policy/selector"
) )
func TestMatch(t *testing.T) { func TestMatch(t *testing.T) {

View File

@@ -5,7 +5,7 @@ package policy
import ( import (
"github.com/ipld/go-ipld-prime" "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 ( const (

View File

@@ -12,7 +12,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "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 // TestSupported Forms runs tests against the Selector according to the

View File

@@ -18,10 +18,10 @@ import (
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/crypto" "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/did"
"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/pkg/policy"
) )
// Token is an immutable type that holds the fields of a UCAN delegation. // Token is an immutable type that holds the fields of a UCAN delegation.

View File

@@ -8,10 +8,10 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gotest.tools/v3/golden" "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/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 ( const (

View File

@@ -12,7 +12,7 @@ import (
"github.com/libp2p/go-libp2p/core/crypto" "github.com/libp2p/go-libp2p/core/crypto"
"github.com/ucan-wg/go-ucan/did" "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 // ToSealed wraps the delegation token in an envelope, generates the

View File

@@ -10,8 +10,8 @@ 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/internal/envelope"
"github.com/ucan-wg/go-ucan/pkg/meta" "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 // [Tag] is the string used as a key within the SigPayload that identifies

View File

@@ -11,8 +11,8 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gotest.tools/v3/golden" "gotest.tools/v3/golden"
"github.com/ucan-wg/go-ucan/delegation" "github.com/ucan-wg/go-ucan/tokens/delegation"
"github.com/ucan-wg/go-ucan/internal/envelope" "github.com/ucan-wg/go-ucan/tokens/internal/envelope"
) )
//go:embed delegation.ipldsch //go:embed delegation.ipldsch

View File

@@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gotest.tools/v3/golden" "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) { func TestCidFromBytes(t *testing.T) {

View File

@@ -16,7 +16,7 @@ import (
"github.com/ipld/go-ipld-prime/schema" "github.com/ipld/go-ipld-prime/schema"
"github.com/libp2p/go-libp2p/core/crypto" "github.com/libp2p/go-libp2p/core/crypto"
"github.com/stretchr/testify/require" "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" "gotest.tools/v3/golden"
) )

View File

@@ -41,7 +41,7 @@ import (
"github.com/libp2p/go-libp2p/core/crypto" "github.com/libp2p/go-libp2p/core/crypto"
"github.com/ucan-wg/go-ucan/did" "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" const varsigHeaderKey = "h"

View File

@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "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" "gotest.tools/v3/golden"
) )

View File

@@ -8,7 +8,7 @@ import (
"github.com/libp2p/go-libp2p/core/crypto/pb" "github.com/libp2p/go-libp2p/core/crypto/pb"
"github.com/stretchr/testify/assert" "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) { func TestDecode(t *testing.T) {