@cryptotaxi247 / kubo / commits / 4f6c0666e

coreapi: move path impl to path.go

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Feb 8, 2018 at 02:25 UTC 4f6c0666ea40128025fa1feb355f0036941bd549
2 files changed +109 -102
core/coreapi/coreapi.go
-102
@@ -14,18 +14,9 @@ Interfaces here aren't yet completely stable.
14 package coreapi
15
16 import (
17 - "context"
18 -
17 core "github.com/ipfs/go-ipfs/core"
18 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
19 caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
22 - namesys "github.com/ipfs/go-ipfs/namesys"
23 - ipfspath "github.com/ipfs/go-ipfs/path"
24 - resolver "github.com/ipfs/go-ipfs/path/resolver"
25 - uio "github.com/ipfs/go-ipfs/unixfs/io"
26 -
27 - cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
28 - ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
20 )
21
22 type CoreAPI struct {
@@ -73,96 +64,3 @@ func (api *CoreAPI) Object() coreiface.ObjectAPI {
64 func (api *CoreAPI) Pin() coreiface.PinAPI {
65 return (*PinAPI)(api)
66 }
76 -
77 -// ResolveNode resolves the path `p` using Unixfx resolver, gets and returns the
78 -// resolved Node.
79 -func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (ipld.Node, error) {
80 - return resolveNode(ctx, api.node.DAG, api.node.Namesys, p)
81 -}
82 -
83 -func resolveNode(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSystem, p coreiface.Path) (ipld.Node, error) {
84 - p, err := resolvePath(ctx, ng, nsys, p)
85 - if err != nil {
86 - return nil, err
87 - }
88 -
89 - node, err := ng.Get(ctx, p.Cid())
90 - if err != nil {
91 - return nil, err
92 - }
93 - return node, nil
94 -}
95 -
96 -// ResolvePath resolves the path `p` using Unixfs resolver, returns the
97 -// resolved path.
98 -// TODO: store all of ipfspath.Resolver.ResolvePathComponents() in Path
99 -func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreiface.Path, error) {
100 - return resolvePath(ctx, api.node.DAG, api.node.Namesys, p)
101 -}
102 -
103 -func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSystem, p coreiface.Path) (coreiface.Path, error) {
104 - if p.Resolved() {
105 - return p, nil
106 - }
107 -
108 - r := &resolver.Resolver{
109 - DAG: ng,
110 - ResolveOnce: uio.ResolveUnixfsOnce,
111 - }
112 -
113 - p2 := ipfspath.FromString(p.String())
114 - node, err := core.Resolve(ctx, nsys, r, p2)
115 - if err == core.ErrNoNamesys {
116 - return nil, coreiface.ErrOffline
117 - } else if err != nil {
118 - return nil, err
119 - }
120 -
121 - var root *cid.Cid
122 - if p2.IsJustAKey() {
123 - root = node.Cid()
124 - }
125 -
126 - return ResolvedPath(p.String(), node.Cid(), root), nil
127 -}
128 -
129 -// Implements coreiface.Path
130 -type path struct {
131 - path ipfspath.Path
132 - cid *cid.Cid
133 - root *cid.Cid
134 -}
135 -
136 -// ParsePath parses path `p` using ipfspath parser, returns the parsed path.
137 -func (api *CoreAPI) ParsePath(ctx context.Context, p string, opts ...caopts.ParsePathOption) (coreiface.Path, error) {
138 - options, err := caopts.ParsePathOptions(opts...)
139 - if err != nil {
140 - return nil, err
141 - }
142 -
143 - pp, err := ipfspath.ParsePath(p)
144 - if err != nil {
145 - return nil, err
146 - }
147 -
148 - res := &path{path: pp}
149 - if options.Resolve {
150 - return api.ResolvePath(ctx, res)
151 - }
152 - return res, nil
153 -}
154 -
155 -// ParseCid parses the path from `c`, returns the parsed path.
156 -func (api *CoreAPI) ParseCid(c *cid.Cid) coreiface.Path {
157 - return &path{path: ipfspath.FromCid(c), cid: c, root: c}
158 -}
159 -
160 -// ResolvePath parses path from string `p`, returns parsed path.
161 -func ResolvedPath(p string, c *cid.Cid, r *cid.Cid) coreiface.Path {
162 - return &path{path: ipfspath.FromString(p), cid: c, root: r}
163 -}
164 -
165 -func (p *path) String() string { return p.path.String() }
166 -func (p *path) Cid() *cid.Cid { return p.cid }
167 -func (p *path) Root() *cid.Cid { return p.root }
168 -func (p *path) Resolved() bool { return p.cid != nil }
core/coreapi/path.go new
+109
@@ -0,0 +1,109 @@
1 +package coreapi
2 +
3 +import (
4 + "context"
5 +
6 + core "github.com/ipfs/go-ipfs/core"
7 + coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
8 + caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9 + namesys "github.com/ipfs/go-ipfs/namesys"
10 + ipfspath "github.com/ipfs/go-ipfs/path"
11 + resolver "github.com/ipfs/go-ipfs/path/resolver"
12 + uio "github.com/ipfs/go-ipfs/unixfs/io"
13 +
14 + ipld "gx/ipfs/QmWi2BYBL5gJ3CiAiQchg6rn1A8iBsrWy51EYxvHVjFvLb/go-ipld-format"
15 + cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
16 +)
17 +
18 +// ResolveNode resolves the path `p` using Unixfx resolver, gets and returns the
19 +// resolved Node.
20 +func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (ipld.Node, error) {
21 + return resolveNode(ctx, api.node.DAG, api.node.Namesys, p)
22 +}
23 +
24 +func resolveNode(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSystem, p coreiface.Path) (ipld.Node, error) {
25 + p, err := resolvePath(ctx, ng, nsys, p)
26 + if err != nil {
27 + return nil, err
28 + }
29 +
30 + node, err := ng.Get(ctx, p.Cid())
31 + if err != nil {
32 + return nil, err
33 + }
34 + return node, nil
35 +}
36 +
37 +// ResolvePath resolves the path `p` using Unixfs resolver, returns the
38 +// resolved path.
39 +// TODO: store all of ipfspath.Resolver.ResolvePathComponents() in Path
40 +func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreiface.Path, error) {
41 + return resolvePath(ctx, api.node.DAG, api.node.Namesys, p)
42 +}
43 +
44 +func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSystem, p coreiface.Path) (coreiface.Path, error) {
45 + if p.Resolved() {
46 + return p, nil
47 + }
48 +
49 + r := &resolver.Resolver{
50 + DAG: ng,
51 + ResolveOnce: uio.ResolveUnixfsOnce,
52 + }
53 +
54 + p2 := ipfspath.FromString(p.String())
55 + node, err := core.Resolve(ctx, nsys, r, p2)
56 + if err == core.ErrNoNamesys {
57 + return nil, coreiface.ErrOffline
58 + } else if err != nil {
59 + return nil, err
60 + }
61 +
62 + var root *cid.Cid
63 + if p2.IsJustAKey() {
64 + root = node.Cid()
65 + }
66 +
67 + return ResolvedPath(p.String(), node.Cid(), root), nil
68 +}
69 +
70 +// Implements coreiface.Path
71 +type path struct {
72 + path ipfspath.Path
73 + cid *cid.Cid
74 + root *cid.Cid
75 +}
76 +
77 +// ParsePath parses path `p` using ipfspath parser, returns the parsed path.
78 +func (api *CoreAPI) ParsePath(ctx context.Context, p string, opts ...caopts.ParsePathOption) (coreiface.Path, error) {
79 + options, err := caopts.ParsePathOptions(opts...)
80 + if err != nil {
81 + return nil, err
82 + }
83 +
84 + pp, err := ipfspath.ParsePath(p)
85 + if err != nil {
86 + return nil, err
87 + }
88 +
89 + res := &path{path: pp}
90 + if options.Resolve {
91 + return api.ResolvePath(ctx, res)
92 + }
93 + return res, nil
94 +}
95 +
96 +// ParseCid parses the path from `c`, retruns the parsed path.
97 +func (api *CoreAPI) ParseCid(c *cid.Cid) coreiface.Path {
98 + return &path{path: ipfspath.FromCid(c), cid: c, root: c}
99 +}
100 +
101 +// ResolvePath parses path from string `p`, returns parsed path.
102 +func ResolvedPath(p string, c *cid.Cid, r *cid.Cid) coreiface.Path {
103 + return &path{path: ipfspath.FromString(p), cid: c, root: r}
104 +}
105 +
106 +func (p *path) String() string { return p.path.String() }
107 +func (p *path) Cid() *cid.Cid { return p.cid }
108 +func (p *path) Root() *cid.Cid { return p.root }
109 +func (p *path) Resolved() bool { return p.cid != nil }