-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace.go
48 lines (38 loc) · 1.14 KB
/
namespace.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
package main
import (
"path"
"strings"
"github.com/diamondburned/cchat/repository"
)
var rootNamespace = pathToNamespace(path.Base(repository.RootPath))
func namespaceType(namespace, typeName string) string {
return joinNamespace(namespace) + "." + typeName
}
func joinNamespace(namespace string) string {
if namespace == rootNamespace {
return rootNamespace
}
return strings.Join([]string{rootNamespace, namespace}, ".")
}
func pathToNamespace(path string) string {
parts := strings.Split(path, "/")
// Reverse domain.
domainParts := strings.Split(parts[0], ".")
// SliceTricks: Reversing.
for i := len(domainParts)/2 - 1; i >= 0; i-- {
opp := len(domainParts) - 1 - i
domainParts[i], domainParts[opp] = domainParts[opp], domainParts[i]
}
parts[0] = strings.Join(domainParts, ".")
return strings.Join(parts, ".")
}
func dirNamespace(namespace string) string {
if strings.Contains(namespace, ".") {
parts := strings.Split(namespace, ".")
namespace = strings.Join(parts[:len(parts)-1], ".")
}
return namespace
}
func trimPrefix(rootPrefix, path string) string {
return strings.Trim(strings.TrimPrefix(path, rootPrefix), "/")
}