-
Notifications
You must be signed in to change notification settings - Fork 4
/
ref.go
193 lines (171 loc) · 5.68 KB
/
ref.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package schema
import (
"fmt"
"reflect"
)
// Ref holds the definition of a reference to a scope-wide object. The ref must always be inside a scope,
// either directly or indirectly. If several scopes are embedded within each other, the Ref references the object
// in the scope specified. SelfNamespace refers to the current scope.
type Ref interface {
Object
ID() string
Namespace() string
Display() Display
GetObject() Object
ObjectReady() bool
}
// NewRefSchema creates a new reference to an object in a wrapping Scope by ID.
func NewRefSchema(id string, display Display) *RefSchema {
return NewNamespacedRefSchema(id, SelfNamespace, display)
}
// NewNamespacedRefSchema creates a new reference to an object in a wrapping Scope by ID and namespace.
func NewNamespacedRefSchema(id string, namespace string, display Display) *RefSchema {
return &RefSchema{
id,
display,
namespace,
nil,
}
}
type RefSchema struct {
IDValue string `json:"id"`
DisplayValue Display `json:"display"`
ObjectNamespace string `json:"namespace"`
referencedObjectCache Object
}
func (r *RefSchema) Properties() map[string]*PropertySchema {
if r.referencedObjectCache == nil {
panic(BadArgumentError{
Message: fmt.Sprintf(
"ref type not linked to its object with ID %q in Properties; scope with namespace %q was not applied successfully",
r.IDValue, r.ObjectNamespace),
})
}
return r.referencedObjectCache.Properties()
}
func (r *RefSchema) GetDefaults() map[string]any {
if r.referencedObjectCache == nil {
panic(BadArgumentError{
Message: fmt.Sprintf(
"ref type not linked to its object with ID %q in GetDefaults; scope with namespace %q was not applied successfully",
r.IDValue, r.ObjectNamespace),
})
}
return r.referencedObjectCache.GetDefaults()
}
func (r *RefSchema) TypeID() TypeID {
return TypeIDRef
}
func (r *RefSchema) GetObject() Object {
if r.referencedObjectCache == nil {
panic(BadArgumentError{
Message: fmt.Sprintf(
"ref type not linked to its object with ID %q in GetObject; scope with namespace %q was not applied successfully",
r.IDValue, r.ObjectNamespace),
})
}
return r.referencedObjectCache
}
func (r *RefSchema) ReflectedType() reflect.Type {
if r.referencedObjectCache == nil {
panic(BadArgumentError{
Message: fmt.Sprintf(
"ref type not linked to its object with ID %q in ReflectedType; scope with namespace %q was not applied successfully",
r.IDValue, r.ObjectNamespace),
})
}
return r.referencedObjectCache.ReflectedType()
}
func (r *RefSchema) ObjectReady() bool {
return r.referencedObjectCache != nil
}
func (r *RefSchema) ID() string {
return r.IDValue
}
func (r *RefSchema) Namespace() string {
return r.ObjectNamespace
}
func (r *RefSchema) Display() Display {
return r.DisplayValue
}
// ApplyNamespace links the reference to the object if the given namespace
// matches the ref's namespace. Other namespaces are skipped.
func (r *RefSchema) ApplyNamespace(objects map[string]*ObjectSchema, namespace string) {
if namespace != r.ObjectNamespace {
return // This reference does not refer to an object in the supplied namespace.
}
referencedObject, ok := objects[r.IDValue]
if !ok {
availableObjects := ""
for objectID := range objects {
availableObjects += objectID + "\n"
}
panic(BadArgumentError{
Message: fmt.Sprintf("Referenced object '%s' not found in scope with namespace %q; available:\n%s", r.IDValue, namespace, availableObjects),
})
}
r.referencedObjectCache = referencedObject
}
func (r *RefSchema) ValidateReferences() error {
if r.referencedObjectCache != nil {
return nil // Success
}
// The only way, unless there is a bug, for it to get here is if ApplyNamespace was not called with the
// correct namespace, or if the code disregards the error returned by ApplyNamespace. ApplyNamespace should
// always set referencedObjectCache or return an error if the correct namespace is applied.
return BadArgumentError{
Message: fmt.Sprintf(
"Ref object reference missing its link to object with ID %q in namespace %q. Namespace not valid (not applied).",
r.IDValue,
r.ObjectNamespace,
),
}
}
func (r *RefSchema) Unserialize(data any) (any, error) {
if r.referencedObjectCache == nil {
panic(BadArgumentError{
Message: fmt.Sprintf(
"ref type not linked to its object with ID %q in Unserialize; scope with namespace %q was not applied successfully",
r.IDValue, r.ObjectNamespace,
),
})
}
return r.referencedObjectCache.Unserialize(data)
}
func (r *RefSchema) Validate(data any) error {
if r.referencedObjectCache == nil {
panic(BadArgumentError{
Message: fmt.Sprintf(
"ref type not linked to its object with ID %q in Validate; scope with namespace %q was not applied successfully",
r.IDValue, r.ObjectNamespace,
),
})
}
return r.referencedObjectCache.Validate(data)
}
func (r *RefSchema) ValidateCompatibility(typeOrData any) error {
if r.referencedObjectCache == nil {
panic(BadArgumentError{
Message: fmt.Sprintf(
"ref type not linked to its object with ID %q in ValidateCompatibility; scope with namespace %q was not applied successfully",
r.IDValue, r.ObjectNamespace,
),
})
}
schemaType, ok := typeOrData.(*RefSchema)
if ok {
return r.referencedObjectCache.ValidateCompatibility(schemaType.referencedObjectCache)
}
return r.referencedObjectCache.ValidateCompatibility(typeOrData)
}
func (r *RefSchema) Serialize(data any) (any, error) {
if r.referencedObjectCache == nil {
panic(BadArgumentError{
Message: fmt.Sprintf(
"ref type not linked to its object with ID %q in Serialize; scope with namespace %q was not applied successfully",
r.IDValue, r.ObjectNamespace,
),
})
}
return r.referencedObjectCache.Serialize(data)
}