Repository: bottlerocket-os/bottlerocket
File: sources/settings-migrations/v1.40.0/kubelet-device-plugins-cdi-settings/src/main.rs (Rust)

Code before:
        if let Some(data) = input.data.get_mut(setting) {
            match data {
                serde_json::Value::Array(arr) => {
                    let list: Vec<&str> = arr
                        .iter()
                        .map(|v| v.as_str())
                        .collect::<Option<Vec<&str>>>()
                        .context(error::ReplaceListContentsSnafu {
                            setting,
                            data: arr.clone(),
                        })?;

                    let new_value = if list.is_empty() || list[0] == "cdi-cri" {
                        "volume-mounts".to_string()
                    } else {
                        list[0].to_string()
                    };

                    *data = serde_json::Value::String(new_value.to_string());
                }

                serde_json::Value::String(setting_str) => {
                    if setting_str == "cdi-cri" {
                        *data = serde_json::Value::String("volume-mounts".to_string());
                    } else {
                        *data = serde_json::Value::String(setting_str.to_string());
                    }
                }

                _ => error::InvalidSettingTypeSnafu {
                    data: setting.to_string(),
                }
                .fail()?,
            }
        } else {
            println!("Found no '{}' to change on downgrade", setting);
        }

        Ok(input)
    }
}

#[cfg(test)]
mod test_replace_list {
    use super::*;
    use crate::{Migration, MigrationData};
    use maplit::hashmap;
    use serde_json::Value;
    use std::collections::HashMap;

    #[test]

Review comment (suggestion):
Given that the setting values are basically enum, can this be:

```suggestion
                   *data = serde_json::Value::String(setting_str.replace("cdi-cri", "volume-mounts").to_string());
```

Code after:
        if let Some(data) = input.data.get_mut(setting) {
            match data {
                serde_json::Value::Array(arr) => {
                    let list: Vec<&str> = arr
                        .iter()
                        .map(|v| v.as_str())
                        .collect::<Option<Vec<&str>>>()
                        .context(error::ReplaceListContentsSnafu {
                            setting,
                            data: arr.clone(),
                        })?;

                    let new_value = match list.first() {
                        None | Some(&"cdi-cri") => "volume-mounts".to_string(),
                        Some(value) => value.to_string(),
                    };

                    *data = serde_json::Value::String(new_value.to_string());
                }

                serde_json::Value::String(setting_str) => {
                    if setting_str == "cdi-cri" {
                        *data = serde_json::Value::String("volume-mounts".to_string());
                    } else {
                        *data = serde_json::Value::String(setting_str.to_string());
                    }
                }

                _ => error::InvalidSettingTypeSnafu {
                    data: setting.to_string(),
                }
                .fail()?,
            }
        } else {
            println!("Found no '{}' to change on downgrade", setting);
        }

        Ok(input)
    }
}

#[cfg(test)]
mod test_replace_list {
    use super::*;
    use crate::{Migration, MigrationData};
    use maplit::hashmap;
    use serde_json::Value;
    use std::collections::HashMap;

    #[test]
    fn forward_test() {