-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathfield.rs
More file actions
402 lines (364 loc) · 16.5 KB
/
Copy pathfield.rs
File metadata and controls
402 lines (364 loc) · 16.5 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
use std::collections::BTreeMap;
use darling::{FromField, Result, util::IdentString};
use k8s_version::Version;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{Attribute, Field, Type};
use crate::{
attrs::item::FieldAttributes,
codegen::{
Direction, VersionDefinition,
changes::{BTreeMapExt, ChangesetExt},
item::ItemStatus,
module::ModuleGenerationContext,
},
utils::FieldIdent,
};
pub struct VersionedField {
pub original_attributes: Vec<Attribute>,
pub changes: Option<BTreeMap<Version, ItemStatus>>,
pub ident: FieldIdent,
pub nested: bool,
pub ty: Type,
}
impl VersionedField {
pub fn new(
field: Field,
versions: &[VersionDefinition],
experimental_conversion_tracking: bool,
) -> Result<Self> {
let field_attributes = FieldAttributes::from_field(&field)?;
field_attributes.validate_versions(versions)?;
field_attributes.validate_nested_flag(experimental_conversion_tracking)?;
let ident = field
.ident
.expect("internal error: field must have an ident");
let idents = ident.into();
let changes = field_attributes
.common
.into_changeset(&idents, field.ty.clone());
let nested = field_attributes.nested.is_present();
Ok(Self {
original_attributes: field_attributes.attrs,
ident: idents,
ty: field.ty,
changes,
nested,
})
}
pub fn insert_container_versions(&mut self, versions: &[VersionDefinition]) {
if let Some(changes) = &mut self.changes {
changes.insert_container_versions(versions, &self.ty);
}
}
pub fn generate_for_container(&self, version: &VersionDefinition) -> Option<TokenStream> {
let original_attributes = &self.original_attributes;
match &self.changes {
Some(changes) => {
// Check if the provided container version is present in the map
// of actions. If it is, some action occurred in exactly that
// version and thus code is generated for that field based on
// the type of action.
// If not, the provided version has no action attached to it.
// The code generation then depends on the relation to other
// versions (with actions).
let field_type = &self.ty;
// NOTE (@Techassi): https://rust-lang.github.io/rust-clippy/master/index.html#/expect_fun_call
match changes.get(&version.inner).unwrap_or_else(|| {
panic!(
"internal error: chain must contain container version {}",
version.inner
)
}) {
ItemStatus::Addition { ident, ty, .. } => Some(quote! {
#(#original_attributes)*
pub #ident: #ty,
}),
ItemStatus::Change {
to_ident, to_type, ..
} => Some(quote! {
#(#original_attributes)*
pub #to_ident: #to_type,
}),
ItemStatus::Deprecation {
ident: field_ident,
note,
..
} => {
// FIXME (@Techassi): Emitting the deprecated attribute
// should cary over even when the item status is
// 'NoChange'.
// TODO (@Techassi): Make the generation of deprecated
// items customizable. When a container is used as a K8s
// CRD, the item must continue to exist, even when
// deprecated. For other versioning use-cases, that
// might not be the case.
let deprecated_attr = if let Some(note) = note {
quote! {#[deprecated = #note]}
} else {
quote! {#[deprecated]}
};
Some(quote! {
#(#original_attributes)*
#deprecated_attr
pub #field_ident: #field_type,
})
}
ItemStatus::NotPresent => None,
ItemStatus::NoChange {
previously_deprecated,
ident,
ty,
..
} => {
// TODO (@Techassi): Also carry along the deprecation
// note.
let deprecated_attr = previously_deprecated.then(|| quote! {#[deprecated]});
Some(quote! {
#(#original_attributes)*
#deprecated_attr
pub #ident: #ty,
})
}
}
}
None => {
// If there is no chain of field actions, the field is not
// versioned and therefore included in all versions.
let field_ident = &self.ident;
let field_type = &self.ty;
Some(quote! {
#(#original_attributes)*
pub #field_ident: #field_type,
})
}
}
}
pub fn generate_for_from_impl(
&self,
direction: Direction,
version: &VersionDefinition,
next_version: &VersionDefinition,
from_struct_ident: &IdentString,
) -> Option<TokenStream> {
match &self.changes {
Some(changes) => {
let next_change = changes.get_expect(&next_version.inner);
let change = changes.get_expect(&version.inner);
match (change, next_change) {
// If both this status and the next one is NotPresent, which means
// a field was introduced after a bunch of versions, we don't
// need to generate any code for the From impl.
(ItemStatus::NotPresent, ItemStatus::NotPresent) => None,
(
_,
ItemStatus::Addition {
ident, default_fn, ..
},
) => match direction {
Direction::Upgrade => Some(quote! { #ident: #default_fn(), }),
Direction::Downgrade => None,
},
(
_,
ItemStatus::Change {
downgrade_with,
upgrade_with,
from_ident,
to_ident,
..
},
) => match direction {
Direction::Upgrade => match upgrade_with {
// The user specified a custom conversion function which
// will be used here instead of the default .into() call
// which utilizes From impls.
Some(upgrade_fn) => Some(quote! {
#to_ident: #upgrade_fn(#from_struct_ident.#from_ident),
}),
// Default .into() call using From impls.
None => {
if self.nested {
let json_path_ident = format_ident!(
"__sv_{ident}_path",
ident = to_ident.as_ident()
);
Some(quote! {
#to_ident: #from_struct_ident.#from_ident.tracking_into(status, &#json_path_ident),
})
} else {
Some(quote! {
#to_ident: #from_struct_ident.#from_ident.into(),
})
}
}
},
Direction::Downgrade => match downgrade_with {
Some(downgrade_fn) => Some(quote! {
#from_ident: #downgrade_fn(#from_struct_ident.#to_ident),
}),
None => {
if self.nested {
let json_path_ident = format_ident!(
"__sv_{ident}_path",
ident = from_ident.as_ident()
);
Some(quote! {
#from_ident: #from_struct_ident.#to_ident.tracking_into(status, &#json_path_ident),
})
} else {
Some(quote! {
#from_ident: #from_struct_ident.#to_ident.into(),
})
}
}
},
},
(old, next) => {
let next_field_ident = next.get_ident();
let old_field_ident = old.get_ident();
// NOTE (@Techassi): Do we really need .into() here. I'm
// currently not sure why it is there and if it is needed
// in some edge cases.
match direction {
Direction::Upgrade => {
if self.nested {
let json_path_ident = format_ident!(
"__sv_{ident}_path",
ident = next_field_ident.as_ident()
);
Some(quote! {
#next_field_ident: #from_struct_ident.#old_field_ident.tracking_into(status, &#json_path_ident),
})
} else {
Some(quote! {
#next_field_ident: #from_struct_ident.#old_field_ident.into(),
})
}
}
Direction::Downgrade => Some(quote! {
#old_field_ident: #from_struct_ident.#next_field_ident.into(),
}),
}
}
}
}
None => {
let field_ident = &*self.ident;
if self.nested {
let json_path_ident =
format_ident!("__sv_{ident}_path", ident = field_ident.as_ident());
Some(quote! {
#field_ident: #from_struct_ident.#field_ident.tracking_into(status, &#json_path_ident),
})
} else {
Some(quote! {
#field_ident: #from_struct_ident.#field_ident.into(),
})
}
}
}
}
pub fn generate_for_status_insertion(
&self,
direction: Direction,
next_version: &VersionDefinition,
from_struct_ident: &IdentString,
mod_gen_ctx: ModuleGenerationContext<'_>,
) -> Option<TokenStream> {
let changes = self.changes.as_ref()?;
match direction {
// This arm is only relevant for removed fields which are currently
// not supported.
Direction::Upgrade => None,
// When we generate code for a downgrade, any changes which need to
// be tracked need to be inserted into the upgrade section for the
// next time an upgrade needs to be done.
Direction::Downgrade => {
let next_change = changes.get_expect(&next_version.inner);
let serde_yaml_path = &*mod_gen_ctx.crates.serde_yaml;
let versioned_path = &*mod_gen_ctx.crates.versioned;
match next_change {
ItemStatus::Addition { ident, .. } => {
// TODO (@Techassi): Only do this formatting once, but that requires extensive
// changes to the field ident and changeset generation
let json_path_ident =
format_ident!("__sv_{ident}_path", ident = ident.as_ident());
Some(quote! {
upgrades.push(#versioned_path::ChangedValue {
json_path: #json_path_ident,
value: #serde_yaml_path::to_value(&#from_struct_ident.#ident).unwrap(),
});
})
}
_ => None,
}
}
}
}
pub fn generate_for_status_removal(
&self,
direction: Direction,
next_version: &VersionDefinition,
) -> Option<TokenStream> {
// If there are no changes for this field, there is also no need to generate a match arm
// for applying a tracked value.
let changes = self.changes.as_ref()?;
match direction {
Direction::Upgrade => {
let next_change = changes.get_expect(&next_version.inner);
match next_change {
// NOTE (@Techassi): We currently only support tracking added fields. As such
// we only need to generate code if the next change is "Addition".
ItemStatus::Addition { ident, .. } => {
let json_path_ident = format_ident!("__sv_{}_path", ident.as_ident());
Some(quote! {
json_path if json_path == #json_path_ident => {
spec.#ident = serde_yaml::from_value(value).unwrap();
},
})
}
_ => None,
}
}
Direction::Downgrade => None,
}
}
pub fn generate_for_json_path(
&self,
next_version: &VersionDefinition,
mod_gen_ctx: ModuleGenerationContext<'_>,
) -> Option<TokenStream> {
let versioned_path = &*mod_gen_ctx.crates.versioned;
match (&self.changes, self.nested) {
// If there are no changes and the field also not marked as nested, there is no need to
// generate a path variable for that field as no tracked values need to be applied/inserted
// and the tracking mechanism doesn't need to be forwarded to a sub struct.
(None, false) => None,
// If the field is marked as nested, a path variable for that field needs to be generated
// which is then passed down to the sub struct. There is however no need to look determine
// if the field itself also has changes. This is explicitly handled by the following match
// arm.
(_, true) => {
let field_ident = format_ident!("__sv_{}_path", &self.ident.as_ident());
let child_string = &self.ident.to_string();
Some(quote! {
let #field_ident = #versioned_path::jthong_path(parent, #child_string);
})
}
(Some(changes), _) => {
let next_change = changes.get_expect(&next_version.inner);
match next_change {
ItemStatus::Addition { ident, .. } => {
let field_ident = format_ident!("__sv_{}_path", ident.as_ident());
let child_string = ident.to_string();
Some(quote! {
let #field_ident = #versioned_path::jthong_path(parent, #child_string);
})
}
_ => None,
}
}
}
}
}