forked from progrium/darwinkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.go
74 lines (62 loc) · 1.27 KB
/
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
66
67
68
69
70
71
72
73
74
package resource
import (
"fmt"
"reflect"
"strings"
"github.com/progrium/macdriver/objc"
"github.com/rs/xid"
)
type Handle string
func NewHandle(t string) *Handle {
handle := Handle(fmt.Sprintf("%s:%s", t, Handle(xid.New().String())))
return &handle
}
func HasHandle(v interface{}) bool {
rv := reflect.Indirect(reflect.ValueOf(v))
if rv.Kind() == reflect.Struct && rv.Type().NumField() > 0 && rv.Type().Field(0).Name == "Handle" {
return true
}
return false
}
func GetHandle(v interface{}) *Handle {
if !HasHandle(v) {
return nil
}
rv := reflect.Indirect(reflect.ValueOf(v))
h := rv.Field(0).Interface()
hh, ok := h.(*Handle)
if !ok {
return nil
}
return hh
}
func SetHandle(v interface{}, h string) {
if !HasHandle(v) {
return
}
handle := Handle(h)
reflect.Indirect(reflect.ValueOf(v)).Field(0).Set(reflect.ValueOf(&handle))
}
func (h *Handle) Type() string {
parts := strings.Split(string(*h), ":")
return parts[0]
}
func (h *Handle) ID() string {
parts := strings.Split(string(*h), ":")
if len(parts) > 1 {
return parts[1]
}
return ""
}
func (h *Handle) Handle() string {
if h == nil {
return ""
}
return string(*h)
}
type Applier interface {
Apply(objc.Object) (objc.Object, error)
}
type Discarder interface {
Discard(objc.Object) error
}