func applyMapOperations()

in pkg/bundle/patch/executor.go [555:621]


func applyMapOperations(input map[string]string, op *bundlev1.PatchOperation, values map[string]interface{}) error {
	// Check parameters
	if input == nil {
		return fmt.Errorf("cannot process nil map")
	}
	if op == nil {
		return fmt.Errorf("cannot process nil operation")
	}

	// Process all operations
	// Remove all keys
	if len(op.RemoveKeys) > 0 {
		for _, rx := range op.RemoveKeys {
			re, err := regexp.Compile(rx)
			if err != nil {
				return fmt.Errorf("unable to compile regexp for key deletion '%s': %w", rx, err)
			}

			// Add to remove if match one expression
			for k := range input {
				if re.MatchString(k) {
					if op.Remove == nil {
						op.Remove = make([]string, 0)
					}
					op.Remove = append(op.Remove, k)
				}
			}
		}
	}
	if len(op.Remove) > 0 {
		for _, toRemove := range op.Remove {
			delete(input, toRemove)
		}
	}
	if op.Add != nil {
		inMap, err := precompileMap(op.Add, values)
		if err != nil {
			return fmt.Errorf("unable to compile add map templates: %w", err)
		}
		if err := mergo.Merge(&input, inMap); err != nil {
			return fmt.Errorf("unable to add attributes to object: %w", err)
		}
	}
	if op.Update != nil {
		inMap, err := precompileMap(op.Update, values)
		if err != nil {
			return fmt.Errorf("unable to compile update map templates: %w", err)
		}
		if err := mergo.Merge(&input, inMap, mergo.WithOverride); err != nil {
			return fmt.Errorf("unable to add attributes to object: %w", err)
		}
	}
	if op.ReplaceKeys != nil {
		inMap, err := precompileMap(op.ReplaceKeys, values)
		if err != nil {
			return fmt.Errorf("unable to compile replaceKeys map templates: %w", err)
		}
		for oldKey, newKey := range inMap {
			if v, ok := input[oldKey]; ok {
				input[newKey] = v
				delete(input, oldKey)
			}
		}
	}

	return nil
}