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
use super::*;

/// Extension trait for `corev1::ConfigMap`.
/// Fluent builders and mutable accessors
///
pub trait ConfigMapExt: ResourceBuilder {
    /// Creates new `corev1::ConfigMap object with given `name`
    ///
    fn new(name: impl ToString) -> Self;

    /// Initializes `immutable` field
    ///
    fn immutable(self, yes: bool) -> Self;

    /// Initializes `binary_data` field
    ///
    fn binary_data(self, data: impl IntoIterator<Item = (impl ToString, ByteString)>) -> Self;

    /// Initializes `data` field
    ///
    fn data(self, data: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self;

    /// Mutable access to `data`.
    /// Initializes `data` with empty `BTreeMap` if absent
    ///
    fn data_mut(&mut self) -> &mut BTreeMap<String, String>;

    /// Mutable access to `binary_data`.
    /// Initializes `binary_data` with empty `BTreeMap` if absent
    ///
    fn binary_data_mut(&mut self) -> &mut BTreeMap<String, ByteString>;
}

impl ConfigMapExt for corev1::ConfigMap {
    fn new(name: impl ToString) -> Self {
        let metadata = metadata(name);
        Self {
            metadata,
            // binary_data: todo!(),
            // data: todo!(),
            // immutable: todo!(),
            ..default()
        }
    }

    fn immutable(self, yes: bool) -> Self {
        let immutable = Some(yes);
        Self { immutable, ..self }
    }

    fn binary_data(self, data: impl IntoIterator<Item = (impl ToString, ByteString)>) -> Self {
        let data = data
            .into_iter()
            .map(|(key, value)| (key.to_string(), value))
            .collect();
        Self {
            binary_data: Some(data),
            ..self
        }
    }

    fn data(self, data: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self {
        let data = data
            .into_iter()
            .map(|(key, value)| (key.to_string(), value.to_string()))
            .collect();
        Self {
            data: Some(data),
            ..self
        }
    }

    fn data_mut(&mut self) -> &mut BTreeMap<String, String> {
        self.data.get_or_insert_with(default)
    }

    fn binary_data_mut(&mut self) -> &mut BTreeMap<String, ByteString> {
        self.binary_data.get_or_insert_with(default)
    }
}