/
resource.go
65 lines (54 loc) · 999 Bytes
/
resource.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 domain
import (
"errors"
"fmt"
"strings"
)
var (
RootResource = Resource{resourceId{"", "root"}, nil}
)
type resourceId struct {
id string
kind string
}
type Resource struct {
id resourceId
Attributes []Attribute
}
func NewResource(id, kind string) (*Resource, error) {
return &Resource{
id: resourceId{
id: id,
kind: kind,
},
}, nil
}
func NewResourceFromName(name string) (*Resource, error) {
split := strings.Split(name, "/")
if len(split) < 2 {
return nil, errors.New("invalid resource name format")
}
kind := split[0]
id := strings.Join(split[1:], "/")
return &Resource{
id: resourceId{
id: id,
kind: kind,
},
}, nil
}
func (r Resource) Id() string {
return r.id.id
}
func (r Resource) SetId(id string) {
r.id.id = id
}
func (r Resource) Kind() string {
return r.id.kind
}
func (r Resource) SetKind(kind string) {
r.id.kind = kind
}
func (r Resource) Name() string {
return fmt.Sprintf("%s/%s", r.id.kind, r.id.id)
}