-
Notifications
You must be signed in to change notification settings - Fork 351
/
resolver.go
86 lines (73 loc) · 1.76 KB
/
resolver.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
package path
import (
"errors"
"fmt"
"regexp"
"strings"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/block"
)
const (
Separator = "/"
rePath = "(?P<path>.*)"
reReference = `(?P<ref>[^/]+)`
)
var (
EncodedPathRe = regexp.MustCompile(fmt.Sprintf("^/?%s/%s", reReference, rePath))
EncodedPathReferenceRe = regexp.MustCompile(fmt.Sprintf("^/?%s$", reReference))
ErrPathMalformed = errors.New("encoded path is malformed")
)
type Ref struct {
Branch string
CommitID graveler.CommitID
}
type ResolvedPath struct {
Ref string
Path string
WithPath bool
}
type ResolvedAbsolutePath struct {
Repo string
Reference string
Path string
}
func ResolveAbsolutePath(encodedPath string) (ResolvedAbsolutePath, error) {
const encodedPartsCount = 3
encodedPath = strings.TrimLeft(encodedPath, "/")
parts := strings.SplitN(encodedPath, "/", encodedPartsCount)
if len(parts) != encodedPartsCount {
return ResolvedAbsolutePath{}, ErrPathMalformed
}
return ResolvedAbsolutePath{
Repo: parts[0],
Reference: parts[1],
Path: parts[2],
}, nil
}
func ResolvePath(encodedPath string) (ResolvedPath, error) {
r := ResolvedPath{}
if len(encodedPath) == 0 {
return r, nil // empty path.
}
// try reference with path or just reference regexp
for _, re := range []*regexp.Regexp{EncodedPathRe, EncodedPathReferenceRe} {
match := re.FindStringSubmatch(encodedPath)
if len(match) == 0 {
continue
}
for i, name := range re.SubexpNames() {
switch name {
case "ref":
r.Ref = match[i]
case "path":
r.Path = match[i]
r.WithPath = true
}
}
return r, nil
}
return r, ErrPathMalformed
}
func WithRef(path, ref string) string {
return block.JoinPathParts([]string{ref, path})
}