coreapi: move path utils to interface
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com> This commit was moved from ipfs/interface-go-ipfs-core@0a0e69cc424e6136e8f0a1fecabda94e7854bc7f This commit was moved from ipfs/boxo@92ebd6eb7be417dec28e3c8fbc0f0bb4db65d268
Łukasz Magiera committed
Jun 12, 2018 at 03:52 UTC
b808f144b5b65e02307e0de0624deba9758d529e
2 files changed
+87
-10
core/coreiface/coreapi.go
-10
@@ -6,7 +6,6 @@ import (
6
"context"
7
8
ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
9
- cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
9
)
10
11
// CoreAPI defines an unified interface to IPFS for Go programs
@@ -38,13 +37,4 @@ type CoreAPI interface {
37
// ResolveNode resolves the path (if not resolved already) using Unixfs
38
// resolver, gets and returns the resolved Node
39
ResolveNode(context.Context, Path) (ipld.Node, error)
41
-
42
- // ParsePath parses string path to a Path
43
- ParsePath(string) (Path, error)
44
-
45
- // IpfsPath creates new /ipfs path from the provided CID
46
- IpfsPath(*cid.Cid) ResolvedPath
47
-
48
- // IpldPath creates new /ipld path from the provided CID
49
- IpldPath(*cid.Cid) ResolvedPath
40
}
core/coreiface/path.go
+87
@@ -1,9 +1,13 @@
1
package iface
2
3
import (
4
+ ipfspath "github.com/ipfs/go-ipfs/path"
5
+
6
cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
7
)
8
9
+//TODO: merge with ipfspath so we don't depend on it
10
+
11
// Path is a generic wrapper for paths used in the API. A path can be resolved
12
// to a CID using one of Resolve functions in the API.
13
//
@@ -87,3 +91,86 @@ type ResolvedPath interface {
91
92
Path
93
}
94
+
95
+// path implements coreiface.Path
96
+type path struct {
97
+ path ipfspath.Path
98
+}
99
+
100
+// resolvedPath implements coreiface.resolvedPath
101
+type resolvedPath struct {
102
+ path
103
+ cid *cid.Cid
104
+ root *cid.Cid
105
+ remainder string
106
+}
107
+
108
+// IpfsPath creates new /ipfs path from the provided CID
109
+func IpfsPath(c *cid.Cid) ResolvedPath {
110
+ return &resolvedPath{
111
+ path: path{ipfspath.Path("/ipfs/" + c.String())},
112
+ cid: c,
113
+ root: c,
114
+ remainder: "",
115
+ }
116
+}
117
+
118
+// IpldPath creates new /ipld path from the provided CID
119
+func IpldPath(c *cid.Cid) ResolvedPath {
120
+ return &resolvedPath{
121
+ path: path{ipfspath.Path("/ipld/" + c.String())},
122
+ cid: c,
123
+ root: c,
124
+ remainder: "",
125
+ }
126
+}
127
+
128
+// ParsePath parses string path to a Path
129
+func ParsePath(p string) (Path, error) {
130
+ pp, err := ipfspath.ParsePath(p)
131
+ if err != nil {
132
+ return nil, err
133
+ }
134
+
135
+ return &path{path: pp}, nil
136
+}
137
+
138
+// NewResolvedPath creates new ResolvedPath. This function performs no checks
139
+// and is intended to be used by resolver implementations. Incorrect inputs may
140
+// cause panics. Handle with care.
141
+func NewResolvedPath(ipath ipfspath.Path, c *cid.Cid, root *cid.Cid, remainder string) ResolvedPath {
142
+ return &resolvedPath{
143
+ path: path{ipath},
144
+ cid: c,
145
+ root: root,
146
+ remainder: remainder,
147
+ }
148
+}
149
+
150
+func (p *path) String() string {
151
+ return p.path.String()
152
+}
153
+
154
+func (p *path) Namespace() string {
155
+ if len(p.path.Segments()) < 1 {
156
+ panic("path without namespace") //this shouldn't happen under any scenario
157
+ }
158
+ return p.path.Segments()[0]
159
+}
160
+
161
+func (p *path) Mutable() bool {
162
+ //TODO: MFS: check for /local
163
+ return p.Namespace() == "ipns"
164
+}
165
+
166
+func (p *resolvedPath) Cid() *cid.Cid {
167
+ return p.cid
168
+}
169
+
170
+func (p *resolvedPath) Root() *cid.Cid {
171
+ return p.root
172
+}
173
+
174
+func (p *resolvedPath) Remainder() string {
175
+ return p.remainder
176
+}