-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder_path.go
65 lines (54 loc) · 1.08 KB
/
builder_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
package mrlib
import (
"strings"
)
const (
placeholderPath = "{{path}}"
)
type (
BuilderPath interface {
FullPath(path string) string
}
builderPath struct {
basePath string
postfix string
}
)
// NewBuilderPath - sample /dir/{{path}}/postfix -> /dir/real-value/postfix
func NewBuilderPath(basePath string) BuilderPath {
return NewBuilderPathWithPlaceholder(basePath, placeholderPath)
}
func NewBuilderPathWithPlaceholder(basePath, placeholder string) BuilderPath {
basePath = strings.TrimRight(basePath, "/")
if i := strings.Index(basePath, placeholder); i > 0 {
return &builderPath{
basePath: strings.TrimRight(basePath[0:i], "/"),
postfix: strings.TrimLeft(basePath[i+len(placeholder):], "/"),
}
}
return &builderPath{
basePath: basePath,
}
}
func (p *builderPath) FullPath(path string) string {
if path == "" {
return ""
}
if p.postfix == "" {
return strings.Join(
[]string{
p.basePath,
strings.TrimLeft(path, "/"),
},
"/",
)
}
return strings.Join(
[]string{
p.basePath,
strings.Trim(path, "/"),
p.postfix,
},
"/",
)
}