-
Notifications
You must be signed in to change notification settings - Fork 18
/
context.go
302 lines (242 loc) · 8.4 KB
/
context.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"context"
"reflect"
"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/contexts/datacontext"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/runtime"
)
// OCM_CONFIG_TYPE_SUFFIX is the standard suffix used for configuration
// types provided by this library.
const OCM_CONFIG_TYPE_SUFFIX = ".config" + common.OCM_TYPE_GROUP_SUFFIX
type ConfigSelector interface {
Select(Config) bool
}
type ConfigSelectorFunction func(Config) bool
func (f ConfigSelectorFunction) Select(cfg Config) bool { return f(cfg) }
var AllConfigs = AppliedConfigSelectorFunction(func(*AppliedConfig) bool { return true })
const AllGenerations int64 = 0
const CONTEXT_TYPE = "config" + datacontext.OCM_CONTEXT_SUFFIX
type ContextProvider interface {
ConfigContext() Context
}
type Context interface {
datacontext.Context
ContextProvider
AttributesContext() datacontext.AttributesContext
// Info provides the context for nested configuration evaluation
Info() string
// WithInfo provides the same context with additional nesting info
WithInfo(desc string) Context
ConfigTypes() ConfigTypeScheme
// GetConfigForData deserialize configuration objects for known
// configuration types.
GetConfigForData(data []byte, unmarshaler runtime.Unmarshaler) (Config, error)
// ApplyData applies the config given by a byte stream to the config store
// If the config type is not known, a generic config is stored and returned.
// In this case an unknown error for kind KIND_CONFIGTYPE is returned.
ApplyData(data []byte, unmarshaler runtime.Unmarshaler, desc string) (Config, error)
// ApplyConfig applies the config to the config store
ApplyConfig(spec Config, desc string) error
GetConfigForType(generation int64, typ string) (int64, []Config)
GetConfigForName(generation int64, name string) (int64, []Config)
GetConfig(generation int64, selector ConfigSelector) (int64, []Config)
AddConfigSet(name string, set *ConfigSet)
ApplyConfigSet(name string) error
// Reset all configs applied so far, subsequent calls to ApplyTo will
// ony see configs allpied after the last reset.
Reset() int64
// Generation return the actual config generation.
// this is a strictly increasing number, regardless of the number
// of Reset calls.
Generation() int64
// ApplyTo applies all configurations applied after the last reset with
// a generation larger than the given watermark to the specified target.
// A target may be any object. The applied configuration objects decide
// on their own whether they are applicable for the given target.
// The generation of the last applied object is returned to be used as
// new watermark.
ApplyTo(gen int64, target interface{}) (int64, error)
}
var key = reflect.TypeOf(_context{})
// DefaultContext is the default context initialized by init functions.
var DefaultContext = Builder{}.New(datacontext.MODE_SHARED)
// FromContext returns the Context to use for context.Context.
// This is either an explicit context or the default context.
// The returned context incorporates the given context.
func FromContext(ctx context.Context) Context {
c, _ := datacontext.ForContextByKey(ctx, key, DefaultContext)
return c.(Context)
}
func FromProvider(p ContextProvider) Context {
if p == nil {
return nil
}
return p.ConfigContext()
}
func DefinedForContext(ctx context.Context) (Context, bool) {
c, ok := datacontext.ForContextByKey(ctx, key, DefaultContext)
if c != nil {
return c.(Context), ok
}
return nil, ok
}
////////////////////////////////////////////////////////////////////////////////
type _InternalContext = datacontext.InternalContext
type coreContext struct {
_InternalContext
updater Updater
sharedAttributes datacontext.AttributesContext
knownConfigTypes ConfigTypeScheme
configs *ConfigStore
}
type _context struct {
*coreContext
description string
}
var _ Context = &_context{}
// gcWrapper is used as garbage collectable
// wrapper for a context implementation
// to establish a runtime finalizer.
type gcWrapper struct {
datacontext.GCWrapper
*_context
}
func (w *gcWrapper) SetContext(c *_context) {
w._context = c
}
func newContext(shared datacontext.AttributesContext, reposcheme ConfigTypeScheme, delegates datacontext.Delegates) Context {
c := &_context{
coreContext: &coreContext{
sharedAttributes: shared,
knownConfigTypes: reposcheme,
configs: NewConfigStore(),
},
}
c._InternalContext = datacontext.NewContextBase(c, CONTEXT_TYPE, key, shared.GetAttributes(), delegates)
c.updater = NewUpdater(c, c)
datacontext.AssureUpdater(shared, NewUpdater(c, shared))
return datacontext.FinalizedContext[gcWrapper](c)
}
func (c *_context) ConfigContext() Context {
return c
}
func (c *_context) Update() error {
return c.updater.Update()
}
var _ datacontext.Updater = (*_context)(nil)
func (c *_context) Info() string {
return c.description
}
func (c *_context) WithInfo(desc string) Context {
if c.description != "" {
desc = desc + "--" + c.description
}
return &_context{c.coreContext, desc}
}
func (c *_context) AttributesContext() datacontext.AttributesContext {
c.updater.Update()
return c.sharedAttributes
}
func (c *_context) ConfigTypes() ConfigTypeScheme {
return c.knownConfigTypes
}
func (c *_context) ConfigForData(data []byte, unmarshaler runtime.Unmarshaler) (Config, error) {
return c.knownConfigTypes.Decode(data, unmarshaler)
}
func (c *_context) GetConfigForData(data []byte, unmarshaler runtime.Unmarshaler) (Config, error) {
spec, err := c.knownConfigTypes.Decode(data, unmarshaler)
if err != nil {
return nil, err
}
return spec, nil
}
func (c *_context) ApplyConfig(spec Config, desc string) error {
var unknown error
spec = (&AppliedConfig{config: spec}).eval(c)
if IsGeneric(spec) {
unknown = errors.ErrUnknown(KIND_CONFIGTYPE, spec.GetType())
}
c.configs.Apply(spec, desc)
var err error
for {
// apply directly and also indirectly described configurations
if gen, in := c.updater.State(); err != nil || in || gen >= c.configs.Generation() {
break
}
err = c.Update()
if IsErrNoContext(err) {
err = unknown
}
}
err = errors.Wrapf(err, "%s", desc)
return err
}
func (c *_context) ApplyData(data []byte, unmarshaler runtime.Unmarshaler, desc string) (Config, error) {
spec, err := c.knownConfigTypes.Decode(data, unmarshaler)
if err != nil {
return nil, err
}
return spec, c.ApplyConfig(spec, desc)
}
func (c *_context) selector(gen int64, selector ConfigSelector) AppliedConfigSelector {
if gen <= 0 {
return AppliedConfigSelectorFor(selector)
}
if selector == nil {
return AppliedGenerationSelector(gen)
}
return AppliedAndSelector(AppliedGenerationSelector(gen), AppliedConfigSelectorFor(selector))
}
func (c *_context) Generation() int64 {
return c.configs.Generation()
}
func (c *_context) Reset() int64 {
return c.configs.Reset()
}
func (c *_context) ApplyTo(gen int64, target interface{}) (int64, error) {
cur := c.configs.Generation()
if cur <= gen {
return gen, nil
}
cur, cfgs := c.configs.GetConfigForSelector(c, AppliedGenerationSelector(gen))
list := errors.ErrListf("config apply errors")
for _, cfg := range cfgs {
err := errors.Wrapf(cfg.config.ApplyTo(c.WithInfo(cfg.description), target), "%s", cfg.description)
if !IsErrNoContext(err) {
list.Add(err)
}
}
return cur, list.Result()
}
func (c *_context) AddConfigSet(name string, set *ConfigSet) {
c.configs.AddSet(name, set)
}
func (c *_context) ApplyConfigSet(name string) error {
set := c.configs.GetSet(name)
if set == nil {
return errors.ErrUnknown(KIND_CONFIGSET, name)
}
desc := "config set " + name
list := errors.ErrListf("applying %s", desc)
for _, cfg := range set.Configurations {
list.Add(c.ApplyConfig(cfg, desc))
}
return list.Result()
}
func (c *_context) GetConfig(gen int64, selector ConfigSelector) (int64, []Config) {
gen, cfgs := c.configs.GetConfigForSelector(c, c.selector(gen, selector))
return gen, cfgs.Configs()
}
func (c *_context) GetConfigForName(gen int64, name string) (int64, []Config) {
gen, cfgs := c.configs.GetConfigForName(c, name, c.selector(gen, nil))
return gen, cfgs.Configs()
}
func (c *_context) GetConfigForType(gen int64, typ string) (int64, []Config) {
gen, cfgs := c.configs.GetConfigForType(c, typ, c.selector(gen, nil))
return gen, cfgs.Configs()
}