forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
37 lines (31 loc) · 754 Bytes
/
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
package suggest
import (
"errors"
"strings"
)
var (
separator = "/"
ErrInvalidRef = errors.New("reference to field must take the form :group/:version/:resource/:field/:name")
)
type RefComponents struct {
Group string
Name string
FieldPath string
}
// String prints the identifier
func (r RefComponents) String() string {
return strings.Join([]string{r.Group, r.Name, r.FieldPath}, separator)
}
// ParseRef takes an identifier and returns it's components.
func ParseRef(ref string) (r RefComponents, err error) {
paths := strings.SplitN(ref, separator, 4)
if len(paths) < 3 {
return r, ErrInvalidRef
}
if len(paths) > 3 {
r.FieldPath = paths[3]
}
r.Group = strings.Join(paths[:2], separator)
r.Name = paths[2]
return
}