1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-19 01:39:35 +08:00

coreapi: path.Mutable

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera
2018-03-30 22:44:23 +02:00
parent f5f44ab246
commit 7ee6194352
3 changed files with 63 additions and 4 deletions

View File

@ -6,13 +6,25 @@ import (
// Path is a generic wrapper for paths used in the API. A path can be resolved
// to a CID using one of Resolve functions in the API.
// TODO: figure out/explain namespaces
//
// Paths must be prefixed with a valid prefix:
//
// * /ipfs - Immutable unixfs path (files)
// * /ipld - Immutable ipld path (data)
// * /ipns - Mutable names. Usually resolves to one of the immutable paths
//TODO: /local (MFS)
type Path interface {
// String returns the path as a string.
String() string
// Namespace returns the first component of the path
Namespace() string
// Mutable returns false if the data pointed to by this path in guaranteed
// to not change.
//
// Note that resolved mutable path can be immutable.
Mutable() bool
}
// ResolvedPath is a resolved Path

View File

@ -97,7 +97,10 @@ func (api *CoreAPI) ParsePath(p string) (coreiface.Path, error) {
return &path{path: pp}, nil
}
func (p *path) String() string { return p.path.String() }
func (p *path) String() string {
return p.path.String()
}
func (p *path) Namespace() string {
if len(p.path.Segments()) < 1 {
return ""
@ -105,5 +108,15 @@ func (p *path) Namespace() string {
return p.path.Segments()[0]
}
func (p *resolvedPath) Cid() *cid.Cid { return p.cid }
func (p *resolvedPath) Root() *cid.Cid { return p.root }
func (p *path) Mutable() bool {
//TODO: MFS: check for /local
return p.Namespace() == "ipns"
}
func (p *resolvedPath) Cid() *cid.Cid {
return p.cid
}
func (p *resolvedPath) Root() *cid.Cid {
return p.root
}

34
core/coreapi/path_test.go Normal file
View File

@ -0,0 +1,34 @@
package coreapi_test
import (
"context"
"strings"
"testing"
)
func TestMutablePath(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
// get self /ipns path
keys, err := api.Key().List(ctx)
if err != nil {
t.Fatal(err)
}
if !keys[0].Path().Mutable() {
t.Error("expected self /ipns path to be mutable")
}
blk, err := api.Block().Put(ctx, strings.NewReader(`foo`))
if err != nil {
t.Error(err)
}
if blk.Mutable() {
t.Error("expected /ipld path to be immutable")
}
}