@@ -415,7 +415,7 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
415
415
}
416
416
417
417
if strings .HasPrefix (app .Annotations [common .WriteBackTargetAnnotation ], common .HelmPrefix ) {
418
- values := make ( map [ interface {}] interface {})
418
+ var values yaml. MapSlice
419
419
err = yaml .Unmarshal (originalData , & values )
420
420
if err != nil {
421
421
return nil , err
@@ -448,7 +448,7 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
448
448
}
449
449
newValues [helmAnnotationParamVersion ] = helmParamVersion .Value
450
450
}
451
- mergeHelmValues (values , newValues )
451
+ mergeHelmValues (& values , newValues )
452
452
453
453
override , err = yaml .Marshal (values )
454
454
} else {
@@ -495,21 +495,38 @@ func mergeHelmOverride(t *helmOverride, o *helmOverride) {
495
495
}
496
496
}
497
497
498
- func mergeHelmValues (values map [interface {}]interface {}, newValues map [string ]string ) {
499
- for fieldPath , newValue := range newValues {
500
- fields := strings .Split (fieldPath , "." )
501
- lastFieldIndex := len (fields ) - 1
502
- node := values
503
- for i , name := range fields {
504
- if i == lastFieldIndex {
505
- node [name ] = newValue
498
+ func mergeHelmValues (values * yaml.MapSlice , newValues map [string ]string ) {
499
+ var update func (values * yaml.MapSlice , path []string , newValue string )
500
+ update = func (values * yaml.MapSlice , path []string , newValue string ) {
501
+ if len (path ) == 0 {
502
+ return
503
+ }
506
504
507
- break
508
- } else if _ , ok := node [name ]; ! ok {
509
- node [name ] = make (map [interface {}]interface {})
505
+ for i := range * values {
506
+ if (* values )[i ].Key == path [0 ] {
507
+ if len (path ) == 1 {
508
+ (* values )[i ].Value = newValue
509
+ } else if v , ok := (* values )[i ].Value .(yaml.MapSlice ); ok {
510
+ update (& v , path [1 :], newValue )
511
+ (* values )[i ].Value = v
512
+ }
513
+ return
510
514
}
511
- node = node [name ].(map [interface {}]interface {})
512
515
}
516
+
517
+ // If the key was not found and we're not at the end of the path, insert a new MapSlice
518
+ if len (path ) > 1 {
519
+ newMapSlice := yaml.MapSlice {}
520
+ update (& newMapSlice , path [1 :], newValue )
521
+ * values = append (* values , yaml.MapItem {Key : path [0 ], Value : newMapSlice })
522
+ } else if len (path ) == 1 {
523
+ // If the key was not found and we're at the end of the path, insert a new key-value pair
524
+ * values = append (* values , yaml.MapItem {Key : path [0 ], Value : newValue })
525
+ }
526
+ }
527
+
528
+ for path , newValue := range newValues {
529
+ update (values , strings .Split (path , "." ), newValue )
513
530
}
514
531
}
515
532
0 commit comments