-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
215 lines (182 loc) · 5.87 KB
/
path.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Package path contains utilities to work with dep2p paths.
package path
import (
"fmt"
gopath "path"
"strings"
"github.com/bpfs/dep2p/util/cid"
)
const (
DeP2PNamespace = "dep2p"
IPNSNamespace = "ipns"
IPLDNamespace = "ipld"
)
// Path is a generic, valid, and well-formed path. A valid path is shaped as follows:
//
// /{namespace}/{root}[/remaining/path]
//
// Where:
//
// 1. Namespace is "dep2p", "ipld", or "ipns".
// 2. If namespace is "dep2p" or "ipld", "root" must be a valid [cid.Cid].
// 3. If namespace is "ipns", "root" may be a [ipns.Name] or a [DNSLink] FQDN.
//
// [DNSLink]: https://dnslink.dev/
type Path interface {
// String returns the path as a string.
String() string
// Namespace returns the first component of the path. For example, the namespace
// of "/dep2p/bafy" is "dep2p".
Namespace() string
// Mutable returns false if the data under this path's namespace is guaranteed to not change.
Mutable() bool
// Segments returns the different elements of a path delimited by a forward
// slash ("/"). The returned array must not contain any empty segments, and
// must have a length of at least two: the first element must be the namespace,
// and the second must be root.
//
// Examples:
// - "/ipld/bafkqaaa" returns ["ipld", "bafkqaaa"]
// - "/dep2p/bafkqaaa/a/b/" returns ["dep2p", "bafkqaaa", "a", "b"]
// - "/ipns/dnslink.net" returns ["ipns", "dnslink.net"]
Segments() []string
}
var _ Path = path{}
type path struct {
str string
namespace string
}
func (p path) String() string {
return p.str
}
func (p path) Namespace() string {
return p.namespace
}
func (p path) Mutable() bool {
return p.Namespace() != DeP2PNamespace && p.Namespace() != IPLDNamespace
}
func (p path) Segments() []string {
return StringToSegments(p.str)
}
// ImmutablePath is a [Path] which is guaranteed to have an immutable [Namespace].
type ImmutablePath struct {
path Path
rootCid cid.Cid
}
var _ Path = ImmutablePath{}
func NewImmutablePath(p Path) (ImmutablePath, error) {
if p.Mutable() {
return ImmutablePath{}, &ErrInvalidPath{err: ErrExpectedImmutable, path: p.String()}
}
segments := p.Segments()
cid, err := cid.Decode(segments[1])
if err != nil {
return ImmutablePath{}, &ErrInvalidPath{err: err, path: p.String()}
}
return ImmutablePath{path: p, rootCid: cid}, nil
}
func (ip ImmutablePath) String() string {
return ip.path.String()
}
func (ip ImmutablePath) Namespace() string {
return ip.path.Namespace()
}
func (ip ImmutablePath) Mutable() bool {
return false
}
func (ip ImmutablePath) Segments() []string {
return ip.path.Segments()
}
func (ip ImmutablePath) RootCid() cid.Cid {
return ip.rootCid
}
// FromCid returns a new "/dep2p" path with the provided CID.
func FromCid(cid cid.Cid) ImmutablePath {
return ImmutablePath{
path: path{
str: fmt.Sprintf("/%s/%s", DeP2PNamespace, cid.String()),
namespace: DeP2PNamespace,
},
rootCid: cid,
}
}
// NewPath takes the given string and returns a well-formed and sanitized [Path].
// The given string is cleaned through [gopath.Clean], but preserving the final
// trailing slash. This function returns an error when the given string is not
// a valid content path.
func NewPath(str string) (Path, error) {
segments := StringToSegments(str)
// Shortest valid path is "/{namespace}/{root}". That yields at least two
// segments: ["{namespace}" "{root}"]. Therefore, here we check if the original
// string begins with "/" (any path must), if we have at least two segments, and if
// the root is non-empty. The namespace is checked further below.
if !strings.HasPrefix(str, "/") || len(segments) < 2 || segments[1] == "" {
return nil, &ErrInvalidPath{err: ErrInsufficientComponents, path: str}
}
cleaned := SegmentsToString(segments...)
if strings.HasSuffix(str, "/") {
// Do not forget to preserve the trailing slash!
cleaned += "/"
}
switch segments[0] {
case DeP2PNamespace, IPLDNamespace:
cid, err := cid.Decode(segments[1])
if err != nil {
return nil, &ErrInvalidPath{err: err, path: str}
}
return ImmutablePath{
path: path{
str: cleaned,
namespace: segments[0],
},
rootCid: cid,
}, nil
case "ipns":
return path{
str: cleaned,
namespace: segments[0],
}, nil
default:
return nil, &ErrInvalidPath{err: fmt.Errorf("%w: %q", ErrUnknownNamespace, segments[0]), path: str}
}
}
// NewPathFromSegments creates a new [Path] from the provided segments. This
// function simply calls [NewPath] internally with the segments concatenated
// using a forward slash "/" as separator. Please see [Path.Segments] for more
// information about how segments must be structured.
func NewPathFromSegments(segments ...string) (Path, error) {
return NewPath(SegmentsToString(segments...))
}
// Join joins a [Path] with certain segments and returns a new [Path].
func Join(p Path, segments ...string) (Path, error) {
s := p.Segments()
s = append(s, segments...)
return NewPathFromSegments(s...)
}
// SegmentsToString converts an array of segments into a string. The returned string
// will always be prefixed with a "/" if there are any segments. For example, if the
// given segments array is ["foo", "bar"], the returned value will be "/foo/bar".
// Given an empty array, an empty string is returned.
func SegmentsToString(segments ...string) string {
str := strings.Join(segments, "/")
if str != "" {
str = "/" + str
}
return str
}
// StringToSegments converts a string into an array of segments. This function follows
// the rules of [Path.Segments]: the path is first cleaned through [gopath.Clean] and
// no empty segments are returned.
func StringToSegments(str string) []string {
str = gopath.Clean(str)
if str == "." {
return nil
}
// Trim slashes from beginning and end, such that we do not return empty segments.
str = strings.TrimSuffix(str, "/")
str = strings.TrimPrefix(str, "/")
if str == "" {
return nil
}
return strings.Split(str, "/")
}