@cryptotaxi247 / kubo / commits / bc49bee62

Implements path.IsJustAKey().

License: MIT Signed-off-by: Stephen Whitmore <noffle@ipfs.io>

Stephen Whitmore committed Jan 16, 2016 at 03:10 UTC bc49bee623e0845d80669b3a47414a8fbfb240a1
2 files changed +26
path/path.go
+6
@@ -44,6 +44,12 @@ func (p Path) String() string {
44 return string(p)
45 }
46
47 +// IsJustAKey returns true if the path is of the form <key> or /ipfs/<key>.
48 +func (p Path) IsJustAKey() bool {
49 + parts := p.Segments()
50 + return (len(parts) == 2 && parts[0] == "ipfs")
51 +}
52 +
53 func FromSegments(prefix string, seg ...string) (Path, error) {
54 return ParsePath(prefix + strings.Join(seg, "/"))
55 }
path/path_test.go
+20
@@ -28,3 +28,23 @@ func TestPathParsing(t *testing.T) {
28 }
29 }
30 }
31 +
32 +func TestIsJustAKey(t *testing.T) {
33 + cases := map[string]bool{
34 + "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n": true,
35 + "/ipfs/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n": true,
36 + "/ipfs/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n/a": false,
37 + "/ipfs/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n/a/b": false,
38 + }
39 +
40 + for p, expected := range cases {
41 + path, err := ParsePath(p)
42 + if err != nil {
43 + t.Fatalf("ParsePath failed to parse \"%s\", but should have succeeded", p)
44 + }
45 + result := path.IsJustAKey()
46 + if result != expected {
47 + t.Fatalf("expected IsJustAKey(%s) to return %v, not %v", p, expected, result)
48 + }
49 + }
50 +}