Skip to content

Commit 0ad0aed

Browse files
committed
coreapi: path.Mutable
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent 3a1084e commit 0ad0aed

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

core/coreapi/interface/path.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,25 @@ import (
66

77
// Path is a generic wrapper for paths used in the API. A path can be resolved
88
// to a CID using one of Resolve functions in the API.
9-
// TODO: figure out/explain namespaces
9+
//
10+
// Paths must be prefixed with a valid prefix:
11+
//
12+
// * /ipfs - Immutable unixfs path (files)
13+
// * /ipld - Immutable ipld path (data)
14+
// * /ipns - Mutable names. Usually resolves to one of the immutable paths
15+
//TODO: /local (MFS)
1016
type Path interface {
1117
// String returns the path as a string.
1218
String() string
1319

1420
// Namespace returns the first component of the path
1521
Namespace() string
22+
23+
// Mutable returns false if the data pointed to by this path in guaranteed
24+
// to not change.
25+
//
26+
// Note that resolved mutable path can be immutable.
27+
Mutable() bool
1628
}
1729

1830
// ResolvedPath is a resolved Path

core/coreapi/path.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,26 @@ func (api *CoreAPI) ParsePath(p string) (coreiface.Path, error) {
9797
return &path{path: pp}, nil
9898
}
9999

100-
func (p *path) String() string { return p.path.String() }
100+
func (p *path) String() string {
101+
return p.path.String()
102+
}
103+
101104
func (p *path) Namespace() string {
102105
if len(p.path.Segments()) < 1 {
103106
return ""
104107
}
105108
return p.path.Segments()[0]
106109
}
107110

108-
func (p *resolvedPath) Cid() *cid.Cid { return p.cid }
109-
func (p *resolvedPath) Root() *cid.Cid { return p.root }
111+
func (p *path) Mutable() bool {
112+
//TODO: MFS: check for /local
113+
return p.Namespace() == "ipns"
114+
}
115+
116+
func (p *resolvedPath) Cid() *cid.Cid {
117+
return p.cid
118+
}
119+
120+
func (p *resolvedPath) Root() *cid.Cid {
121+
return p.root
122+
}

core/coreapi/path_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package coreapi_test
2+
3+
import (
4+
"context"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestMutablePath(t *testing.T) {
10+
ctx := context.Background()
11+
_, api, err := makeAPI(ctx)
12+
if err != nil {
13+
t.Fatal(err)
14+
}
15+
16+
// get self /ipns path
17+
keys, err := api.Key().List(ctx)
18+
if err != nil {
19+
t.Fatal(err)
20+
}
21+
22+
if !keys[0].Path().Mutable() {
23+
t.Error("expected self /ipns path to be mutable")
24+
}
25+
26+
blk, err := api.Block().Put(ctx, strings.NewReader(`foo`))
27+
if err != nil {
28+
t.Error(err)
29+
}
30+
31+
if blk.Mutable() {
32+
t.Error("expected /ipld path to be immutable")
33+
}
34+
}

0 commit comments

Comments
 (0)