mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-20 10:42:11 +08:00
coreapi: path.Mutable
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
@ -6,13 +6,25 @@ import (
|
|||||||
|
|
||||||
// Path is a generic wrapper for paths used in the API. A path can be resolved
|
// 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.
|
// 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 {
|
type Path interface {
|
||||||
// String returns the path as a string.
|
// String returns the path as a string.
|
||||||
String() string
|
String() string
|
||||||
|
|
||||||
// Namespace returns the first component of the path
|
// Namespace returns the first component of the path
|
||||||
Namespace() string
|
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
|
// ResolvedPath is a resolved Path
|
||||||
|
@ -97,7 +97,10 @@ func (api *CoreAPI) ParsePath(p string) (coreiface.Path, error) {
|
|||||||
return &path{path: pp}, nil
|
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 {
|
func (p *path) Namespace() string {
|
||||||
if len(p.path.Segments()) < 1 {
|
if len(p.path.Segments()) < 1 {
|
||||||
return ""
|
return ""
|
||||||
@ -105,5 +108,15 @@ func (p *path) Namespace() string {
|
|||||||
return p.path.Segments()[0]
|
return p.path.Segments()[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *resolvedPath) Cid() *cid.Cid { return p.cid }
|
func (p *path) Mutable() bool {
|
||||||
func (p *resolvedPath) Root() *cid.Cid { return p.root }
|
//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
34
core/coreapi/path_test.go
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user