Skip to content

Commit

Permalink
coreapi: path.Mutable
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <[email protected]>
  • Loading branch information
magik6k committed Mar 30, 2018
1 parent 3a1084e commit f71e588
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
14 changes: 13 additions & 1 deletion core/coreapi/interface/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 when the content pointed to by this path can
// change.
//
// Note that resolved mutable path can be immutable.
Mutable() bool
}

// ResolvedPath is a resolved Path
Expand Down
19 changes: 16 additions & 3 deletions core/coreapi/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,26 @@ 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 ""
}
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 changes: 34 additions & 0 deletions core/coreapi/path_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit f71e588

Please sign in to comment.