diff --git a/path/path.go b/path/path.go index 6f14f901638d07a65ef59dfbca29757e22d7d904..dc2d5d1de3b4fcbd64e8f02d6be74b760f78ca73 100644 --- a/path/path.go +++ b/path/path.go @@ -44,6 +44,12 @@ func (p Path) String() string { return string(p) } +// IsJustAKey returns true if the path is of the form <key> or /ipfs/<key>. +func (p Path) IsJustAKey() bool { + parts := p.Segments() + return (len(parts) == 2 && parts[0] == "ipfs") +} + func FromSegments(prefix string, seg ...string) (Path, error) { return ParsePath(prefix + strings.Join(seg, "/")) } diff --git a/path/path_test.go b/path/path_test.go index f800e19e717b3963cf7aafe87ee3b1d600f16ae2..464cd419a9a0df46edede36f7f8acf0bfbb9f22c 100644 --- a/path/path_test.go +++ b/path/path_test.go @@ -28,3 +28,23 @@ func TestPathParsing(t *testing.T) { } } } + +func TestIsJustAKey(t *testing.T) { + cases := map[string]bool{ + "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n": true, + "/ipfs/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n": true, + "/ipfs/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n/a": false, + "/ipfs/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n/a/b": false, + } + + for p, expected := range cases { + path, err := ParsePath(p) + if err != nil { + t.Fatalf("ParsePath failed to parse \"%s\", but should have succeeded", p) + } + result := path.IsJustAKey() + if result != expected { + t.Fatalf("expected IsJustAKey(%s) to return %v, not %v", p, expected, result) + } + } +}