coreapi: path review
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Apr 20, 2018 at 13:31 UTC
1d23bbeb0fc971b799dc82b44ed9fc224944848f
2 files changed
+14
-5
core/coreapi/path.go
+10
-4
@@ -2,8 +2,8 @@ package coreapi
2
3
import (
4
context "context"
5
+ fmt "fmt"
6
gopath "path"
6
- strings "strings"
7
8
core "github.com/ipfs/go-ipfs/core"
9
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
@@ -87,9 +87,15 @@ func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSyste
87
return nil, err
88
}
89
90
- resolveOnce := uio.ResolveUnixfsOnce
91
- if strings.HasPrefix(ipath.String(), "/ipld") {
90
+ var resolveOnce resolver.ResolveOnce
91
+
92
+ switch p.Namespace() {
93
+ case "ipfs":
94
+ resolveOnce = uio.ResolveUnixfsOnce
95
+ case "ipld":
96
resolveOnce = resolver.ResolveSingle
97
+ default:
98
+ return nil, fmt.Errorf("unsupported path namespace: %s", p.Namespace())
99
}
100
101
r := &resolver.Resolver{
@@ -131,7 +137,7 @@ func (p *path) String() string {
137
138
func (p *path) Namespace() string {
139
if len(p.path.Segments()) < 1 {
134
- return ""
140
+ panic("path without namespace") //this shouldn't happen under any scenario
141
}
142
return p.path.Segments()[0]
143
}
path/resolver/resolver.go
+4
-1
@@ -34,6 +34,9 @@ func (e ErrNoLink) Error() string {
34
return fmt.Sprintf("no link named %q under %s", e.Name, e.Node.String())
35
}
36
37
+// ResolveOnce resolves path through a single node
38
+type ResolveOnce func(ctx context.Context, ds ipld.NodeGetter, nd ipld.Node, names []string) (*ipld.Link, []string, error)
39
+
40
// Resolver provides path resolution to IPFS
41
// It has a pointer to a DAGService, which is uses to resolve nodes.
42
// TODO: now that this is more modular, try to unify this code with the
@@ -41,7 +44,7 @@ func (e ErrNoLink) Error() string {
44
type Resolver struct {
45
DAG ipld.NodeGetter
46
44
- ResolveOnce func(ctx context.Context, ds ipld.NodeGetter, nd ipld.Node, names []string) (*ipld.Link, []string, error)
47
+ ResolveOnce ResolveOnce
48
}
49
50
// NewBasicResolver constructs a new basic resolver.