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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
pub(crate) fn generate(
    mut writer: impl std::io::Write,
    type_name: &str,
    generics: super::Generics<'_>,
    fields: &[super::Property<'_>],
    map_namespace: &impl crate::MapNamespace,
    resource_metadata: Option<&super::ResourceMetadata<'_>>,
) -> Result<(), crate::Error> {
    use std::fmt::Write;

    let local = crate::map_namespace_local_to_string(map_namespace)?;

    let type_generics_impl: std::borrow::Cow<'_, str> = match generics.type_part {
        Some(part) => format!("<'de, {part}>").into(),
        None => "<'de>".into(),
    };
    let type_generics_type = generics.type_part.map(|part| format!("<{part}>")).unwrap_or_default();
    let type_generics_where = generics.where_part.map(|part| format!(" where {part}")).unwrap_or_default();

    let (visitor_field, visitor_create_field) =
        if type_generics_type.is_empty() {
            (String::new(), "")
        }
        else {
            (format!("(std::marker::PhantomData{type_generics_type})"), "(Default::default())")
        };

    let mut fields_string = String::new();
    let mut str_to_field_match_arms = String::new();
    let mut field_value_defs = String::new();
    let mut field_value_match_arms = String::new();
    let mut field_name_list = String::new();
    let mut field_value_assignment = String::new();

    let mut flattened_field = None;

    if resource_metadata.is_some() {
        writeln!(fields_string, "            Key_api_version,")?;
        writeln!(fields_string, "            Key_kind,")?;

        writeln!(str_to_field_match_arms, r#"                            "apiVersion" => Field::Key_api_version,"#)?;
        writeln!(str_to_field_match_arms, r#"                            "kind" => Field::Key_kind,"#)?;

        writeln!(field_value_match_arms, r#"                        Field::Key_api_version => {{"#)?;
        writeln!(field_value_match_arms, r#"                            let value_api_version: String = {local}serde::de::MapAccess::next_value(&mut map)?;"#)?;
        writeln!(field_value_match_arms, r#"                            if value_api_version != <Self::Value as {local}Resource>::API_VERSION {{"#)?;
        writeln!(field_value_match_arms,
            r#"                                return Err({local}serde::de::Error::invalid_value({local}serde::de::Unexpected::Str(&value_api_version), &<Self::Value as {local}Resource>::API_VERSION));"#)?;
        writeln!(field_value_match_arms, r#"                            }}"#)?;
        writeln!(field_value_match_arms, r#"                        }},"#)?;

        writeln!(field_value_match_arms, r#"                        Field::Key_kind => {{"#)?;
        writeln!(field_value_match_arms, r#"                            let value_kind: String = {local}serde::de::MapAccess::next_value(&mut map)?;"#)?;
        writeln!(field_value_match_arms, r#"                            if value_kind != <Self::Value as {local}Resource>::KIND {{"#)?;
        writeln!(field_value_match_arms,
            r#"                                return Err({local}serde::de::Error::invalid_value({local}serde::de::Unexpected::Str(&value_kind), &<Self::Value as {local}Resource>::KIND));"#)?;
        writeln!(field_value_match_arms, r#"                            }}"#)?;
        writeln!(field_value_match_arms, r#"                        }},"#)?;

        writeln!(field_name_list, r#"                "apiVersion","#)?;
        writeln!(field_name_list, r#"                "kind","#)?;
    }
    for super::Property { name, field_name, field_type_name, required, is_flattened, .. } in fields {
        if *is_flattened {
            if flattened_field.is_some() {
                return Err(format!("{type_name} has two flattened fields").into());
            }

            flattened_field = Some((field_name, field_type_name));

            writeln!(field_value_defs,
                r#"                let mut value_{field_name}: std::collections::BTreeMap<{local}serde_value::Value, {local}serde_value::Value> = Default::default();"#)?;
        }
        else {
            writeln!(fields_string, "            Key_{field_name},")?;

            writeln!(str_to_field_match_arms, r#"                            {name:?} => Field::Key_{field_name},"#)?;

            match required {
                super::PropertyRequired::Required { is_default } => {
                    writeln!(field_value_defs, r#"                let mut value_{field_name}: Option<{field_type_name}> = None;"#)?;

                    // The API server doesn't always validate required fields when a resource is created,
                    // so consequently required fields aren't always present when a resource is fetched.
                    //
                    // An example is in https://github.com/Arnavion/k8s-openapi/issues/110 which says it's possible
                    // to create a DaemonSet with a PodSpec that doesn't have its required fields.
                    //
                    // Since the Deserialize impl only matters for parsing the API server response, there's no point being stricter
                    // about the response than the API server is. So we can just pretend that the missing field is the default value
                    // if the field is Default-able. But if the field isn't Default-able, then there's nothing we can do but fail as usual.
                    if *is_default {
                        writeln!(field_value_match_arms,
                            r#"                        Field::Key_{field_name} => value_{field_name} = {local}serde::de::MapAccess::next_value(&mut map)?,"#)?;

                        writeln!(field_value_assignment, "                    {field_name}: value_{field_name}.unwrap_or_default(),")?;
                    }
                    else {
                        writeln!(field_value_match_arms,
                            r#"                        Field::Key_{field_name} => value_{field_name} = Some({local}serde::de::MapAccess::next_value(&mut map)?),"#)?;

                        writeln!(field_value_assignment,
                            "                    {field_name}: value_{field_name}.ok_or_else(|| {local}serde::de::Error::missing_field({name:?}))?,")?;
                    }
                },

                super::PropertyRequired::Optional => {
                    writeln!(field_value_defs, r#"                let mut value_{field_name}: {field_type_name} = None;"#)?;

                    writeln!(field_value_match_arms,
                        r#"                        Field::Key_{field_name} => value_{field_name} = {local}serde::de::MapAccess::next_value(&mut map)?,"#)?;

                    writeln!(field_value_assignment, "                    {field_name}: value_{field_name},")?;
                },

                super::PropertyRequired::OptionalDefault => {
                    writeln!(field_value_defs, r#"                let mut value_{field_name}: Option<{field_type_name}> = None;"#)?;

                    writeln!(field_value_match_arms,
                        r#"                        Field::Key_{field_name} => value_{field_name} = {local}serde::de::MapAccess::next_value(&mut map)?,"#)?;

                    writeln!(field_value_assignment, "                    {field_name}: value_{field_name}.unwrap_or_default(),")?;
                },
            }

            writeln!(field_name_list, r#"                {name:?},"#)?;
        }
    }

    if let Some((field_name, _)) = flattened_field {
        writeln!(fields_string, "            Other(String),")?;

        writeln!(str_to_field_match_arms, "                            v => Field::Other(v.to_owned()),")?;

        writeln!(field_value_match_arms,
            "                        Field::Other(key) => {{ value_{field_name}.insert({local}serde_value::Value::String(key), {local}serde::de::MapAccess::next_value(&mut map)?); }},")?;

        writeln!(field_value_assignment,
            "                    {field_name}: {{")?;
        writeln!(field_value_assignment,
            "                        let value_{field_name} = {local}serde_value::Value::Map(value_{field_name});")?;
        writeln!(field_value_assignment,
            "                        let value_{field_name} = {local}serde_value::ValueDeserializer::new(value_{field_name});")?;
        writeln!(field_value_assignment,
            "                        let value_{field_name} = {local}serde::Deserialize::deserialize(value_{field_name})?;")?;
        writeln!(field_value_assignment,
            "                        value_{field_name}")?;
        writeln!(field_value_assignment,
            "                    }},")?;
    }
    else {
        writeln!(fields_string, "            Other,")?;

        writeln!(str_to_field_match_arms, "                            _ => Field::Other,")?;

        writeln!(field_value_match_arms,
            "                        Field::Other => {{ let _: {local}serde::de::IgnoredAny = {local}serde::de::MapAccess::next_value(&mut map)?; }},")?;
    }

    let deserialize_type_name =
        if resource_metadata.is_some() {
            format!("<Self as {local}Resource>::KIND")
        }
        else {
            format!("{type_name:?}")
        };

    let visitor_expecting_type_name =
        if resource_metadata.is_some() {
            format!("<Self::Value as {local}Resource>::KIND")
        }
        else {
            format!("{type_name:?}")
        };

    writeln!(
        writer,
        include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/impl_deserialize.rs")),
        local = local,
        type_name = type_name,
        type_generics_impl = type_generics_impl,
        type_generics_type = type_generics_type,
        type_generics_where = type_generics_where,
        fields = fields_string,
        str_to_field_match_arms = str_to_field_match_arms,
        field_value_defs = field_value_defs,
        field_value_match_arms = field_value_match_arms,
        field_value_assignment = field_value_assignment,
        field_name_list = field_name_list,
        visitor_field = visitor_field,
        visitor_create_field = visitor_create_field,
        deserialize_type_name = deserialize_type_name,
        visitor_expecting_type_name = visitor_expecting_type_name,
    )?;

    Ok(())
}