-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathfield.rs
More file actions
504 lines (455 loc) · 19.2 KB
/
Copy pathfield.rs
File metadata and controls
504 lines (455 loc) · 19.2 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use std::collections::BTreeMap;
use darling::{FromField, Result, util::IdentString};
use k8s_version::Version;
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Attribute, Field, Ident, Path, Type, spanned::Spanned};
use crate::{
attrs::item::{FieldAttributes, Hint},
codegen::{
Direction, VersionDefinition,
changes::{BTreeMapExt, ChangesetExt},
item::ItemStatus,
module::ModuleGenerationContext,
},
utils::{ItemIdentExt, ItemIdents},
};
#[derive(Debug)]
pub struct VersionedField {
pub original_attributes: Vec<Attribute>,
pub changes: Option<BTreeMap<Version, ItemStatus>>,
pub idents: FieldIdents,
pub hint: Option<Hint>,
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 field_span = field.span();
let ident = field.ident.ok_or_else(|| {
darling::Error::custom("internal error: field must have an ident")
.with_span(&field_span)
})?;
let idents = FieldIdents::from(ident);
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,
hint: field_attributes.hint,
ty: field.ty,
changes,
idents,
nested,
})
}
pub fn insert_container_versions(&mut self, versions: &[VersionDefinition]) {
if let Some(changes) = &mut self.changes {
changes.insert_container_versions(versions, &self.ty);
}
}
/// Generates field definitions for the use inside container (struct) definitions.
///
/// This function needs to take into account multiple conditions:
///
/// - Only emit the field if it exists for the currently generated version.
/// - Emit field with new name and type if there was a name and/or type change.
/// - Handle deprecated fields accordingly.
///
/// ### Example
///
/// ```ignore
/// struct Foo {
/// bar: usize, // This function generates one or more of these definitions
/// }
/// ```
pub fn generate_for_container(&self, version: &VersionDefinition) -> Option<TokenStream> {
let original_attributes = &self.original_attributes;
#[allow(clippy::single_match_else)]
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): `unwrap_or_else` used instead of `expect`.
// See: https://rust-lang.github.io/rust-clippy/master/index.html#expect_fun_call
// We could use expect here, but we would lose the version in the panic message. We need to allow
// a lint in either case anyway.
#[allow(clippy::panic)]
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.idents.original;
let field_type = &self.ty;
Some(quote! {
#(#original_attributes)*
pub #field_ident: #field_type,
})
}
}
}
/// Generates field definitions for the use inside `From` impl blocks.
///
/// This function needs to take into account multiple conditions:
///
/// - Only emit the field if it exists for the currently generated version.
/// - Emit fields which previously didn't exist with the correct initialization function.
/// - Emit field with new name and type if there was a name and/or type change.
/// - Handle tracking conversions without data-loss.
/// - Handle deprecated fields accordingly.
///
/// ### Example
///
/// ```ignore
/// impl From<v1alpha1::Foo> for v1alpha2::Foo {
/// fn from(value: v1alpha1::Foo) -> Self {
/// Self {
/// bar: value.bar, // This function generates one or more of these definitions
/// }
/// }
/// }
/// ```
pub fn generate_for_from_impl(
&self,
direction: Direction,
version: &VersionDefinition,
next_version: &VersionDefinition,
from_struct_ident: &IdentString,
) -> Option<TokenStream> {
#[allow(clippy::single_match_else)]
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 => Some(self.generate_from_impl_field(
to_ident,
from_struct_ident,
from_ident,
upgrade_with.as_ref(),
)),
Direction::Downgrade => Some(self.generate_from_impl_field(
from_ident,
from_struct_ident,
to_ident,
downgrade_with.as_ref(),
)),
},
(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 => Some(self.generate_from_impl_field(
next_field_ident,
from_struct_ident,
old_field_ident,
None,
)),
Direction::Downgrade => Some(self.generate_from_impl_field(
old_field_ident,
from_struct_ident,
next_field_ident,
None,
)),
}
}
}
}
None => {
let field_ident = &self.idents.original;
Some(self.generate_from_impl_field(
field_ident,
from_struct_ident,
field_ident,
None,
))
}
}
}
/// Generates code needed when a tracked conversion for this field needs to be inserted into the
/// status.
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 = ident.json_path_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,
}
}
}
}
/// Generates code needed when a tracked conversion for this field needs to be removed from the
/// status.
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 = ident.json_path_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 determine if
// the field itself also has changes. This is explicitly handled by the following match
// arm.
(_, true) => {
let field_ident = &self.idents.json_path;
let child_string = self.idents.original.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 = ident.json_path_ident();
let child_string = ident.to_string();
Some(quote! {
let #field_ident = #versioned_path::jthong_path(parent, #child_string);
})
}
_ => None,
}
}
}
}
/// Generates field definitions to be used inside `From` impl blocks.
fn generate_from_impl_field(
&self,
lhs_field_ident: &IdentString,
rhs_struct_ident: &IdentString,
rhs_field_ident: &IdentString,
custom_conversion_function: Option<&Path>,
) -> TokenStream {
match custom_conversion_function {
// The user specified a custom conversion function which will be used here instead of the
// default conversion call which utilizes From impls.
Some(convert_fn) => quote! {
#lhs_field_ident: #convert_fn(#rhs_struct_ident.#rhs_field_ident),
},
// Default conversion call using From impls.
None => {
if self.nested {
let json_path_ident = lhs_field_ident.json_path_ident();
let func = self.generate_tracking_conversion_function(&json_path_ident);
quote! {
#lhs_field_ident: #rhs_struct_ident.#rhs_field_ident.#func,
}
} else {
let func = self.generate_conversion_function();
quote! {
#lhs_field_ident: #rhs_struct_ident.#rhs_field_ident.#func,
}
}
}
}
}
/// Generates tracking conversion functions used by field definitions in `From` impl blocks.
fn generate_tracking_conversion_function(&self, json_path_ident: &IdentString) -> TokenStream {
if let Some(hint) = &self.hint {
match hint {
Hint::Option => {
quote! { map(|v| v.tracking_into(status, &#json_path_ident)) }
}
Hint::Vec => {
quote! { into_iter().map(|v| v.tracking_into(status, &#json_path_ident)).collect() }
}
}
} else {
quote! { tracking_into(status, &#json_path_ident) }
}
}
/// Generates conversion functions used by field definitions in `From` impl blocks.
fn generate_conversion_function(&self) -> TokenStream {
if let Some(hint) = &self.hint {
match hint {
Hint::Option => quote! { map(Into::into) },
Hint::Vec => quote! { into_iter().map(Into::into).collect() },
}
} else {
quote! { into() }
}
}
}
/// A collection of field idents used for different purposes.
#[derive(Debug)]
pub struct FieldIdents {
/// The original ident.
pub original: IdentString,
/// The cleaned ident, with the deprecation prefix removed.
pub cleaned: IdentString,
/// The cleaned ident used for JSONPath variables.
pub json_path: IdentString,
}
impl ItemIdents for FieldIdents {
const DEPRECATION_PREFIX: &str = "deprecated_";
fn cleaned(&self) -> &IdentString {
&self.cleaned
}
fn original(&self) -> &IdentString {
&self.original
}
}
impl From<Ident> for FieldIdents {
fn from(ident: Ident) -> Self {
let original = IdentString::new(ident);
let cleaned = original
.clone()
.map(|s| s.trim_start_matches(Self::DEPRECATION_PREFIX).to_owned());
let json_path = cleaned.json_path_ident();
Self {
original,
cleaned,
json_path,
}
}
}