提交 bc49bee6 作者: Stephen Whitmore

Implements path.IsJustAKey().

License: MIT
Signed-off-by: 's avatarStephen Whitmore <noffle@ipfs.io>
上级 c242660d
...@@ -44,6 +44,12 @@ func (p Path) String() string { ...@@ -44,6 +44,12 @@ func (p Path) String() string {
return string(p) 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) { func FromSegments(prefix string, seg ...string) (Path, error) {
return ParsePath(prefix + strings.Join(seg, "/")) return ParsePath(prefix + strings.Join(seg, "/"))
} }
......
...@@ -28,3 +28,23 @@ func TestPathParsing(t *testing.T) { ...@@ -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)
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论