coreapi: path.Mutable
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Mar 30, 2018 at 22:44 UTC
7ee6194352ddea86679e66e64a8d71c13dff8b54
3 files changed
+63
-4
core/coreapi/interface/path.go
+13
-1
@@ -6,13 +6,25 @@ import (
6
7
// Path is a generic wrapper for paths used in the API. A path can be resolved
8
// 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)
16
type Path interface {
17
// String returns the path as a string.
18
String() string
19
20
// Namespace returns the first component of the path
21
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
28
}
29
30
// ResolvedPath is a resolved Path
core/coreapi/path.go
+16
-3
@@ -97,7 +97,10 @@ func (api *CoreAPI) ParsePath(p string) (coreiface.Path, error) {
97
return &path{path: pp}, nil
98
}
99
100
-func (p *path) String() string { return p.path.String() }
100
+func (p *path) String() string {
101
+ return p.path.String()
102
+}
103
+
104
func (p *path) Namespace() string {
105
if len(p.path.Segments()) < 1 {
106
return ""
@@ -105,5 +108,15 @@ func (p *path) Namespace() string {
108
return p.path.Segments()[0]
109
}
110
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
new
+34
@@ -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
+}