-
Notifications
You must be signed in to change notification settings - Fork 66
/
parse.go
183 lines (155 loc) · 4.2 KB
/
parse.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
package repo
import (
"fmt"
"strings"
"github.com/mr-tron/base58/base58"
"github.com/multiformats/go-multihash"
"github.com/qri-io/qri/profile"
reporef "github.com/qri-io/qri/repo/ref"
)
// ParseDatasetRef decodes a dataset reference from a string value
// It’s possible to refer to a dataset in a number of ways.
// The full definition of a dataset reference is as follows:
// dataset_reference = peer_name/dataset_name@peer_id/network/hash
//
// we swap in defaults as follows, all of which are represented as
// empty strings:
// network - defaults to /ipfs/
// hash - tip of version history (latest known commit)
//
// these defaults are currently enforced by convention.
// TODO - make Dataset Ref parsing the responisiblity of the Repo
// interface, replacing empty strings with actual defaults
//
// dataset names & hashes are disambiguated by checking if the input
// parses to a valid multihash after base58 decoding.
// through defaults & base58 checking the following should all parse:
// peer_name/dataset_name
// /network/hash
// peername
// peer_id
// @peer_id
// @peer_id/network/hash
//
// see tests for more exmples
//
// TODO - add validation that prevents peernames from being
// valid base58 multihashes and makes sure hashes are actually valid base58 multihashes
// TODO - figure out how IPFS CID's play into this
func ParseDatasetRef(ref string) (reporef.DatasetRef, error) {
if ref == "" {
return reporef.DatasetRef{}, ErrEmptyRef
}
var (
// nameRefString string
dsr = reporef.DatasetRef{}
err error
)
// if there is an @ symbol, we are dealing with a reporef.DatasetRef
// with an identifier
atIndex := strings.Index(ref, "@")
if atIndex != -1 {
dsr.Peername, dsr.Name = parseAlias(ref[:atIndex])
dsr.ProfileID, dsr.Path, err = parseIdentifiers(ref[atIndex+1:])
} else {
var peername, datasetname, pid bool
toks := strings.Split(ref, "/")
for i, tok := range toks {
if isBase58Multihash(tok) {
// first hash we encounter is a peerID
if !pid {
dsr.ProfileID, _ = profile.IDB58Decode(tok)
pid = true
continue
}
if !isBase58Multihash(toks[i-1]) {
dsr.Path = fmt.Sprintf("/%s/%s", toks[i-1], strings.Join(toks[i:], "/"))
} else {
dsr.Path = fmt.Sprintf("/ipfs/%s", strings.Join(toks[i:], "/"))
}
break
}
if !peername {
dsr.Peername = tok
peername = true
continue
}
if !datasetname {
dsr.Name = tok
datasetname = true
continue
}
dsr.Path = strings.Join(toks[i:], "/")
break
}
}
if dsr.ProfileID == "" && dsr.Peername == "" && dsr.Name == "" && dsr.Path == "" {
err = fmt.Errorf("malformed reporef.DatasetRef string: %s", ref)
return dsr, err
}
// if dsr.ProfileID != "" {
// if !isBase58Multihash(dsr.ProfileID) {
// err = fmt.Errorf("invalid ProfileID: '%s'", dsr.ProfileID)
// return dsr, err
// }
// }
return dsr, err
}
func parseAlias(alias string) (peer, dataset string) {
for i, tok := range strings.Split(alias, "/") {
switch i {
case 0:
peer = tok
case 1:
dataset = tok
}
}
return
}
func parseIdentifiers(ids string) (profileID profile.ID, path string, err error) {
toks := strings.Split(ids, "/")
switch len(toks) {
case 0:
err = fmt.Errorf("malformed reporef.DatasetRef identifier: %s", ids)
case 1:
if toks[0] != "" {
profileID, err = profile.IDB58Decode(toks[0])
// if !isBase58Multihash(toks[0]) {
// err = fmt.Errorf("'%s' is not a base58 multihash", ids)
// }
return
}
case 2:
if pid, e := profile.IDB58Decode(toks[0]); e == nil {
profileID = pid
}
if isBase58Multihash(toks[0]) && isBase58Multihash(toks[1]) {
toks[1] = fmt.Sprintf("/ipfs/%s", toks[1])
}
path = toks[1]
default:
if pid, e := profile.IDB58Decode(toks[0]); e == nil {
profileID = pid
}
path = fmt.Sprintf("/%s/%s", toks[1], toks[2])
return
}
return
}
// TODO - this could be more robust?
func stripProtocol(ref string) string {
if strings.HasPrefix(ref, "/ipfs/") {
return ref[len("/ipfs/"):]
}
return ref
}
func isBase58Multihash(hash string) bool {
data, err := base58.Decode(hash)
if err != nil {
return false
}
if _, err := multihash.Decode(data); err != nil {
return false
}
return true
}