Doc: golint-ify path package.
This removes all go-lint warnings in the path package. License: MIT Signed-off-by: Hector Sanjuan <hector@protocol.ai>
Hector Sanjuan committed
Feb 12, 2018 at 22:18 UTC
cc1b5498851cc2e52dc265974c92eb2f5123c8c1
2 files changed
+39
-13
path/path.go
+25
-4
@@ -1,3 +1,4 @@
1
+// Package path contains utilities to work with ipfs paths.
2
package path
3
4
import (
@@ -11,20 +12,29 @@ import (
12
// ErrBadPath is returned when a given path is incorrectly formatted
13
var ErrBadPath = errors.New("invalid 'ipfs ref' path")
14
15
+// A Path represents an ipfs content path:
16
+// * /<cid>/path/to/file
17
+// * /ipfs/<cid>
18
+// * /ipns/<cid>/path/to/folder
19
+// * etc
20
+type Path string
21
+
22
+// ^^^
23
// TODO: debate making this a private struct wrapped in a public interface
24
// would allow us to control creation, and cache segments.
16
-type Path string
25
18
-// FromString safely converts a string type to a Path type
26
+// FromString safely converts a string type to a Path type.
27
func FromString(s string) Path {
28
return Path(s)
29
}
30
23
-// FromCid safely converts a cid.Cid type to a Path type
31
+// FromCid safely converts a cid.Cid type to a Path type.
32
func FromCid(c *cid.Cid) Path {
33
return Path("/ipfs/" + c.String())
34
}
35
36
+// Segments returns the different elements of a path
37
+// (elements are delimited by a /).
38
func (p Path) Segments() []string {
39
cleaned := path.Clean(string(p))
40
segments := strings.Split(cleaned, "/")
@@ -37,6 +47,7 @@ func (p Path) Segments() []string {
47
return segments
48
}
49
50
+// String converts a path to string.
51
func (p Path) String() string {
52
return string(p)
53
}
@@ -65,10 +76,16 @@ func (p Path) PopLastSegment() (Path, string, error) {
76
return newPath, segs[len(segs)-1], nil
77
}
78
79
+// FromSegments returns a path given its different segments.
80
func FromSegments(prefix string, seg ...string) (Path, error) {
81
return ParsePath(prefix + strings.Join(seg, "/"))
82
}
83
84
+// ParsePath returns a well-formed ipfs Path.
85
+// The returned path will always be prefixed with /ipfs/ or /ipns/.
86
+// The prefix will be added if not present in the given string.
87
+// This function will return an error when the given string is
88
+// not a valid ipfs path.
89
func ParsePath(txt string) (Path, error) {
90
parts := strings.Split(txt, "/")
91
if len(parts) == 1 {
@@ -78,7 +95,7 @@ func ParsePath(txt string) (Path, error) {
95
}
96
}
97
81
- // if the path doesnt being with a '/'
98
+ // if the path doesnt begin with a '/'
99
// we expect this to start with a hash, and be an 'ipfs' path
100
if parts[0] != "" {
101
if _, err := ParseCidToPath(parts[0]); err != nil {
@@ -103,6 +120,7 @@ func ParsePath(txt string) (Path, error) {
120
return Path(txt), nil
121
}
122
123
+// ParseCidToPath takes a CID in string form and returns a valid ipfs Path.
124
func ParseCidToPath(txt string) (Path, error) {
125
if txt == "" {
126
return "", ErrNoComponents
@@ -116,15 +134,18 @@ func ParseCidToPath(txt string) (Path, error) {
134
return FromCid(c), nil
135
}
136
137
+// IsValid checks if a path is a valid ipfs Path.
138
func (p *Path) IsValid() error {
139
_, err := ParsePath(p.String())
140
return err
141
}
142
143
+// Join joins strings slices using /
144
func Join(pths []string) string {
145
return strings.Join(pths, "/")
146
}
147
148
+// SplitList splits strings usings /
149
func SplitList(pth string) []string {
150
return strings.Split(pth, "/")
151
}
path/resolver.go
+14
-9
@@ -16,7 +16,8 @@ import (
16
17
var log = logging.Logger("path")
18
19
-// Paths after a protocol must contain at least one component
19
+// ErrNoComponents is used when Paths after a protocol
20
+// do not contain at least one component
21
var ErrNoComponents = errors.New(
22
"path must contain at least one component")
23
@@ -26,6 +27,8 @@ type ErrNoLink struct {
27
Node *cid.Cid
28
}
29
30
+// Error implements the Error interface for ErrNoLink with a useful
31
+// human readable message.
32
func (e ErrNoLink) Error() string {
33
return fmt.Sprintf("no link named %q under %s", e.Name, e.Node.String())
34
}
@@ -74,6 +77,8 @@ func SplitAbsPath(fpath Path) (*cid.Cid, []string, error) {
77
return c, parts[1:], nil
78
}
79
80
+// ResolveToLastNode walks the given path and returns the ipld.Node
81
+// referenced by the last element in it.
82
func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath Path) (ipld.Node, []string, error) {
83
c, p, err := SplitAbsPath(fpath)
84
if err != nil {
@@ -109,13 +114,13 @@ func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath Path) (ipld.Node
114
115
// ResolvePath fetches the node for given path. It returns the last item
116
// returned by ResolvePathComponents.
112
-func (s *Resolver) ResolvePath(ctx context.Context, fpath Path) (ipld.Node, error) {
117
+func (r *Resolver) ResolvePath(ctx context.Context, fpath Path) (ipld.Node, error) {
118
// validate path
119
if err := fpath.IsValid(); err != nil {
120
return nil, err
121
}
122
118
- nodes, err := s.ResolvePathComponents(ctx, fpath)
123
+ nodes, err := r.ResolvePathComponents(ctx, fpath)
124
if err != nil || nodes == nil {
125
return nil, err
126
}
@@ -131,7 +136,7 @@ func ResolveSingle(ctx context.Context, ds ipld.NodeGetter, nd ipld.Node, names
136
// ResolvePathComponents fetches the nodes for each segment of the given path.
137
// It uses the first path component as a hash (key) of the first node, then
138
// resolves all other components walking the links, with ResolveLinks.
134
-func (s *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]ipld.Node, error) {
139
+func (r *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]ipld.Node, error) {
140
evt := log.EventBegin(ctx, "resolvePathComponents", logging.LoggableMap{"fpath": fpath})
141
defer evt.Done()
142
@@ -142,13 +147,13 @@ func (s *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]ipl
147
}
148
149
log.Debug("resolve dag get")
145
- nd, err := s.DAG.Get(ctx, h)
150
+ nd, err := r.DAG.Get(ctx, h)
151
if err != nil {
152
evt.Append(logging.LoggableMap{"error": err.Error()})
153
return nil, err
154
}
155
151
- return s.ResolveLinks(ctx, nd, parts)
156
+ return r.ResolveLinks(ctx, nd, parts)
157
}
158
159
// ResolveLinks iteratively resolves names by walking the link hierarchy.
@@ -158,7 +163,7 @@ func (s *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]ipl
163
//
164
// ResolveLinks(nd, []string{"foo", "bar", "baz"})
165
// would retrieve "baz" in ("bar" in ("foo" in nd.Links).Links).Links
161
-func (s *Resolver) ResolveLinks(ctx context.Context, ndd ipld.Node, names []string) ([]ipld.Node, error) {
166
+func (r *Resolver) ResolveLinks(ctx context.Context, ndd ipld.Node, names []string) ([]ipld.Node, error) {
167
168
evt := log.EventBegin(ctx, "resolveLinks", logging.LoggableMap{"names": names})
169
defer evt.Done()
@@ -172,7 +177,7 @@ func (s *Resolver) ResolveLinks(ctx context.Context, ndd ipld.Node, names []stri
177
ctx, cancel = context.WithTimeout(ctx, time.Minute)
178
defer cancel()
179
175
- lnk, rest, err := s.ResolveOnce(ctx, s.DAG, nd, names)
180
+ lnk, rest, err := r.ResolveOnce(ctx, r.DAG, nd, names)
181
if err == dag.ErrLinkNotFound {
182
evt.Append(logging.LoggableMap{"error": err.Error()})
183
return result, ErrNoLink{Name: names[0], Node: nd.Cid()}
@@ -181,7 +186,7 @@ func (s *Resolver) ResolveLinks(ctx context.Context, ndd ipld.Node, names []stri
186
return result, err
187
}
188
184
- nextnode, err := lnk.GetNode(ctx, s.DAG)
189
+ nextnode, err := lnk.GetNode(ctx, r.DAG)
190
if err != nil {
191
evt.Append(logging.LoggableMap{"error": err.Error()})
192
return result, err