@@ -79,10 +79,12 @@ func New(log *logger.Logger, c *config.Config, managed bool) (Controller, error)
79
79
if err != nil {
80
80
return nil , errors .New (err , fmt .Sprintf ("failed to build provider '%s'" , name ), errors .TypeConfig , errors .M ("provider" , name ))
81
81
}
82
+ emptyMapping , _ := transpiler .NewAST (nil )
82
83
contextProviders [name ] = & contextProviderState {
83
84
// Safe for Context to be nil here because it will be filled in
84
85
// by (*controller).Run before the provider is started.
85
86
provider : provider ,
87
+ mapping : emptyMapping ,
86
88
}
87
89
}
88
90
@@ -275,20 +277,17 @@ func (c *controller) Close() {
275
277
func (c * controller ) generateVars (fetchContextProviders mapstr.M ) []* transpiler.Vars {
276
278
// build the vars list of mappings
277
279
vars := make ([]* transpiler.Vars , 1 )
278
- mapping := map [string ]interface {}{}
280
+ mapping , _ := transpiler . NewAST ( map [string ]any {})
279
281
for name , state := range c .contextProviders {
280
- mapping [ name ] = state .Current ()
282
+ _ = mapping . Insert ( state .Current (), name )
281
283
}
282
- // this is ensured not to error, by how the mappings states are verified
283
- mappingAst , _ := transpiler .NewAST (mapping )
284
- vars [0 ] = transpiler .NewVarsFromAst ("" , mappingAst , fetchContextProviders )
284
+ vars [0 ] = transpiler .NewVarsFromAst ("" , mapping , fetchContextProviders )
285
285
286
286
// add to the vars list for each dynamic providers mappings
287
287
for name , state := range c .dynamicProviders {
288
288
for _ , mappings := range state .Mappings () {
289
- local := mappingAst .ShallowClone ()
290
- dynamicAst , _ := transpiler .NewAST (mappings .mapping )
291
- _ = local .Insert (dynamicAst , name )
289
+ local := mapping .ShallowClone ()
290
+ _ = local .Insert (mappings .mapping , name )
292
291
id := fmt .Sprintf ("%s-%s" , name , mappings .id )
293
292
v := transpiler .NewVarsWithProcessorsFromAst (id , local , name , mappings .processors , fetchContextProviders )
294
293
vars = append (vars , v )
@@ -302,7 +301,7 @@ type contextProviderState struct {
302
301
303
302
provider corecomp.ContextProvider
304
303
lock sync.RWMutex
305
- mapping map [ string ] interface {}
304
+ mapping * transpiler. AST
306
305
signal chan bool
307
306
}
308
307
@@ -324,30 +323,25 @@ func (c *contextProviderState) Signal() {
324
323
// Set sets the current mapping.
325
324
func (c * contextProviderState ) Set (mapping map [string ]interface {}) error {
326
325
var err error
327
- mapping , err = cloneMap (mapping )
328
- if err != nil {
329
- return err
330
- }
331
- // ensure creating vars will not error
332
- _ , err = transpiler .NewVars ("" , mapping , nil )
326
+ ast , err := transpiler .NewAST (mapping )
333
327
if err != nil {
334
328
return err
335
329
}
336
330
337
331
c .lock .Lock ()
338
332
defer c .lock .Unlock ()
339
333
340
- if reflect . DeepEqual ( c .mapping , mapping ) {
334
+ if c .mapping != nil && c . mapping . Equal ( ast ) {
341
335
// same mapping; no need to update and signal
342
336
return nil
343
337
}
344
- c .mapping = mapping
338
+ c .mapping = ast
345
339
c .Signal ()
346
340
return nil
347
341
}
348
342
349
343
// Current returns the current mapping.
350
- func (c * contextProviderState ) Current () map [ string ] interface {} {
344
+ func (c * contextProviderState ) Current () * transpiler. AST {
351
345
c .lock .RLock ()
352
346
defer c .lock .RUnlock ()
353
347
return c .mapping
@@ -356,7 +350,7 @@ func (c *contextProviderState) Current() map[string]interface{} {
356
350
type dynamicProviderMapping struct {
357
351
id string
358
352
priority int
359
- mapping map [ string ] interface {}
353
+ mapping * transpiler. AST
360
354
processors transpiler.Processors
361
355
}
362
356
@@ -376,31 +370,25 @@ type dynamicProviderState struct {
376
370
// to ensure that matching of variables occurs on the lower priority mappings first.
377
371
func (c * dynamicProviderState ) AddOrUpdate (id string , priority int , mapping map [string ]interface {}, processors []map [string ]interface {}) error {
378
372
var err error
379
- mapping , err = cloneMap (mapping )
380
- if err != nil {
381
- return err
382
- }
383
373
processors , err = cloneMapArray (processors )
384
374
if err != nil {
385
375
return err
386
376
}
387
- // ensure creating vars will not error
388
- _ , err = transpiler .NewVars ("" , mapping , nil )
377
+ ast , err := transpiler .NewAST (mapping )
389
378
if err != nil {
390
379
return err
391
380
}
392
-
393
381
c .lock .Lock ()
394
382
defer c .lock .Unlock ()
395
383
curr , ok := c .mappings [id ]
396
- if ok && reflect . DeepEqual ( curr .mapping , mapping ) && reflect .DeepEqual (curr .processors , processors ) {
384
+ if ok && curr .mapping . Equal ( ast ) && reflect .DeepEqual (curr .processors , processors ) {
397
385
// same mapping; no need to update and signal
398
386
return nil
399
387
}
400
388
c .mappings [id ] = dynamicProviderMapping {
401
389
id : id ,
402
390
priority : priority ,
403
- mapping : mapping ,
391
+ mapping : ast ,
404
392
processors : processors ,
405
393
}
406
394
@@ -458,22 +446,6 @@ func (c *dynamicProviderState) Mappings() []dynamicProviderMapping {
458
446
return mappings
459
447
}
460
448
461
- func cloneMap (source map [string ]interface {}) (map [string ]interface {}, error ) {
462
- if source == nil {
463
- return nil , nil
464
- }
465
- bytes , err := json .Marshal (source )
466
- if err != nil {
467
- return nil , fmt .Errorf ("failed to clone: %w" , err )
468
- }
469
- var dest map [string ]interface {}
470
- err = json .Unmarshal (bytes , & dest )
471
- if err != nil {
472
- return nil , fmt .Errorf ("failed to clone: %w" , err )
473
- }
474
- return dest , nil
475
- }
476
-
477
449
func cloneMapArray (source []map [string ]interface {}) ([]map [string ]interface {}, error ) {
478
450
if source == nil {
479
451
return nil , nil
0 commit comments