forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
50 lines (43 loc) · 1.35 KB
/
string.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
package resource
import "strings"
const (
// Delimiter of Resource Reference according to Atlas Reference format
Delimiter = "/"
)
// BuildString builds string id according to Atlas Reference format:
// <application_name>/<resource_type>/<resource_id>
func BuildString(aname, rtype, rid string) string {
var l []string
if v := strings.TrimSpace(aname); v != "" {
l = append(l, v)
}
if v := strings.TrimSpace(rtype); v != "" {
l = append(l, v)
}
if v := strings.TrimSpace(rid); v != "" {
l = append(l, v)
}
return strings.Join(l, Delimiter)
}
// ParseString parses id according to Atlas Reference format:
// <application_name>/<resource_type>/<resource_id>
// All leading and trailing Delimiter are removed.
// The resource_id is parsed first, then resource type and last application name.
// The id "/a/b/c/" will be converted to "a/b/c" and returned as (a, b, c).
// The id "b/c/" will be converted to "b/c" and returned as ("", b, c).
func ParseString(id string) (aname, rtype, rid string) {
v := strings.SplitN(strings.Trim(id, Delimiter), Delimiter, 3)
switch len(v) {
case 1:
rid = v[0]
case 2:
rtype, rid = v[0], v[1]
case 3:
aname, rtype, rid = v[0], v[1], v[2]
}
return
}
func (m Identifier) MarshalText() (text []byte, err error) {
text = []byte(BuildString(m.GetApplicationName(), m.GetResourceType(), m.GetResourceId()))
return
}