-
Notifications
You must be signed in to change notification settings - Fork 20
/
datatypes.go
110 lines (92 loc) · 2.43 KB
/
datatypes.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package runtime
import (
"strings"
)
// TypeGetter is the interface to be implemented for extracting a type.
type TypeGetter interface {
// GetType returns the type of the access object.
GetType() string
}
// TypeSetter is the interface to be implemented for extracting a type.
type TypeSetter interface {
// SetType sets the type of abstract element
SetType(typ string)
}
type (
// TypeInfo defines the accessors for type information.
TypeInfo interface {
TypeGetter
}
)
type (
// ObjectType is the data type providing serializable type information.
// It is an implementation of TypeInfo.
ObjectType struct {
// Type describes the type of the object.
Type string `json:"type"`
}
_ObjectType = ObjectType // provide the same type to be used as provide embedded field
)
var _ TypeInfo = (*ObjectType)(nil)
// NewObjectType creates an ObjectType value.
func NewObjectType(typ string) ObjectType {
return ObjectType{typ}
}
// GetType returns the type of the object.
func (t ObjectType) GetType() string {
return t.Type
}
// SetType sets the type of the object.
func (t *ObjectType) SetType(typ string) {
t.Type = typ
}
////////////////////////////////////////////////////////////////////////////////
type (
VersionedObjectType ObjectType
_VersionedObjectType = VersionedObjectType
)
func NewVersionedObjectType(args ...string) VersionedObjectType {
return NewVersionedTypedObject(args...)
}
// GetType returns the type of the object.
func (t VersionedObjectType) GetType() string {
return t.Type
}
// SetType sets the type of the object.
func (t *VersionedObjectType) SetType(typ string) {
t.Type = typ
}
// GetKind returns the kind of the object.
func (v VersionedObjectType) GetKind() string {
return GetKind(v)
}
// SetKind sets the kind of the object.
func (v *VersionedObjectType) SetKind(kind string) {
t := v.GetType()
i := strings.LastIndex(t, VersionSeparator)
if i < 0 {
v.SetType(kind)
} else {
v.SetType(kind + t[i:])
}
}
// GetVersion returns the version of the object.
func (v VersionedObjectType) GetVersion() string {
return GetVersion(v)
}
// SetVersion sets the version of the object.
func (v *VersionedObjectType) SetVersion(version string) {
t := v.GetType()
i := strings.LastIndex(t, VersionSeparator)
if i < 0 {
if version != "" {
v.SetType(v.Type + VersionSeparator + version)
}
} else {
if version != "" {
v.SetType(t[:i] + VersionSeparator + version)
} else {
v.SetType(t[:i])
}
}
}