helm-schema-ir 0.0.4

Generate an accurate JSON schema for any helm chart
Documentation
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
use std::collections::BTreeSet;

use crate::abstract_value::AbstractValue;

pub(super) fn value_paths(value: Option<&AbstractValue>) -> BTreeSet<String> {
    value.map(AbstractValue::paths).unwrap_or_default()
}

pub(super) fn value_strings(value: Option<&AbstractValue>) -> BTreeSet<String> {
    value.map(AbstractValue::strings).unwrap_or_default()
}

/// Paths whose value this abstract value may literally be. Widened influence
/// is dataflow through an unknown call, not value identity: defaulting or
/// type-hinting the call result (e.g. `required "..." .Values.x | quote`)
/// says nothing about the type or defaultedness of `.Values.x` itself.
pub(super) fn identity_value_paths(value: Option<&AbstractValue>) -> BTreeSet<String> {
    fn collect(value: &AbstractValue, paths: &mut BTreeSet<String>) {
        match value {
            AbstractValue::ValuesPath(path)
            | AbstractValue::JsonDecodedPath(path)
            | AbstractValue::OutputPath(path, _) => {
                paths.insert(path.clone());
            }
            AbstractValue::Choice(choices) => {
                for choice in choices {
                    collect(choice, paths);
                }
            }
            AbstractValue::FirstTruthy(candidates) => {
                for candidate in candidates {
                    collect(candidate, paths);
                }
            }
            AbstractValue::MergedLayers(layers) => {
                for layer in layers {
                    collect(layer, paths);
                }
            }
            AbstractValue::Top
            | AbstractValue::Unknown
            | AbstractValue::RangeKey(_)
            | AbstractValue::KeysList(_)
            | AbstractValue::RootContext
            | AbstractValue::StringSet(_)
            | AbstractValue::DerivedBoolean(_)
            | AbstractValue::Dict(_)
            | AbstractValue::List(_)
            | AbstractValue::Overlay { .. }
            | AbstractValue::SplitList { .. }
            | AbstractValue::SplitSegment { .. }
            | AbstractValue::Widened(_) => {}
        }
    }

    let mut paths = BTreeSet::new();
    if let Some(value) = value {
        collect(value, &mut paths);
    }
    paths
}

/// Values identities contained in a payload serialized by `toJson` or
/// `toYaml`.
/// Constructed containers are not themselves aliases of their leaves, so
/// strict consumers use [`identity_value_paths`] and stop at those container
/// boundaries. Serialization is different: every structurally retained leaf
/// is part of the encoded payload and must be marked so a matching decoder
/// can recover its runtime identity.
pub(super) fn serialization_payload_paths(value: Option<&AbstractValue>) -> BTreeSet<String> {
    fn collect(value: &AbstractValue, paths: &mut BTreeSet<String>) {
        match value {
            AbstractValue::ValuesPath(path)
            | AbstractValue::JsonDecodedPath(path)
            | AbstractValue::OutputPath(path, _) => {
                paths.insert(path.clone());
            }
            AbstractValue::Dict(entries) => {
                for value in entries.values() {
                    collect(value, paths);
                }
            }
            AbstractValue::List(items) => {
                for value in items {
                    collect(value, paths);
                }
            }
            AbstractValue::Overlay { entries, fallback } => {
                for value in entries.values() {
                    collect(value, paths);
                }
                collect(fallback, paths);
            }
            AbstractValue::Choice(choices) => {
                for value in choices {
                    collect(value, paths);
                }
            }
            AbstractValue::FirstTruthy(candidates) => {
                for value in candidates {
                    collect(value, paths);
                }
            }
            AbstractValue::MergedLayers(layers) => {
                for value in layers {
                    collect(value, paths);
                }
            }
            AbstractValue::Top
            | AbstractValue::Unknown
            | AbstractValue::RangeKey(_)
            | AbstractValue::KeysList(_)
            | AbstractValue::RootContext
            | AbstractValue::StringSet(_)
            | AbstractValue::DerivedBoolean(_)
            | AbstractValue::SplitList { .. }
            | AbstractValue::SplitSegment { .. }
            | AbstractValue::Widened(_) => {}
        }
    }

    let mut paths = BTreeSet::new();
    if let Some(value) = value {
        collect(value, &mut paths);
    }
    paths
}

pub(super) fn identity_range_key_paths(value: Option<&AbstractValue>) -> BTreeSet<String> {
    value
        .map(AbstractValue::range_key_paths)
        .unwrap_or_default()
}

/// The exact element count of a statically known collection value.
pub(super) fn concrete_collection_len(value: &AbstractValue) -> Option<usize> {
    match value {
        AbstractValue::List(items) => Some(items.len()),
        AbstractValue::Dict(entries) => Some(entries.len()),
        _ => None,
    }
}

/// The exact integer a statically known scalar value denotes.
pub(super) fn concrete_integer(value: &AbstractValue) -> Option<i64> {
    let AbstractValue::StringSet(strings) = value else {
        return None;
    };
    let mut strings = strings.iter();
    let (Some(text), None) = (strings.next(), strings.next()) else {
        return None;
    };
    text.parse().ok()
}

/// Wraps a raw-identity value with a lexical escape token: the enclosing
/// transform (`replace TOKEN …`, `(split TOKEN …)._0`) is the identity on
/// raw strings that do not contain `token`, so the output keeps the raw
/// path qualified by the token instead of degrading to derived text.
/// Values that are not a clean raw identity (already derived, serialized,
/// or shape-erased) return `None` — callers fall back to their legacy
/// lowering.
pub(super) fn escape_wrapped_identity(
    value: &AbstractValue,
    effects: &crate::eval_effect::Effects,
    escape: crate::helper_meta::LexicalEscape,
) -> Option<AbstractValue> {
    match value {
        AbstractValue::ValuesPath(path) => {
            if effects.shape_erased_paths.contains(path)
                || effects.derived_text_paths.contains(path)
                || effects
                    .local_output_meta
                    .get(path)
                    .is_some_and(|meta| meta.shape_erased || meta.derived_text)
            {
                return None;
            }
            let mut meta = crate::helper_meta::HelperOutputMeta::default();
            meta.lexical_escapes.insert(escape);
            Some(AbstractValue::OutputPath(path.clone(), meta))
        }
        AbstractValue::OutputPath(path, meta) => {
            if meta.shape_erased
                || meta.derived_text
                || meta.yaml_serialized
                || meta.json_serialized
            {
                return None;
            }
            let mut meta = meta.clone();
            meta.lexical_escapes.insert(escape);
            Some(AbstractValue::OutputPath(path.clone(), meta))
        }
        _ => None,
    }
}

/// The transformed value of `replace OLD NEW subject` when OLD is one
/// static literal: static string arms replace textually, raw-identity arms
/// keep their path qualified by OLD as a lexical escape. `None` when any
/// arm has neither meaning — the caller falls back to the legacy
/// derived-text lowering.
pub(super) fn replace_transformed_value(
    value: &AbstractValue,
    effects: &crate::eval_effect::Effects,
    old: &str,
    new_values: &BTreeSet<String>,
) -> Option<AbstractValue> {
    match value {
        AbstractValue::StringSet(strings) => {
            if new_values.is_empty() {
                return None;
            }
            let mut rendered = BTreeSet::new();
            for subject in strings {
                for new in new_values {
                    rendered.insert(subject.replace(old, new));
                }
            }
            Some(AbstractValue::StringSet(rendered))
        }
        AbstractValue::Choice(choices) => {
            let mapped = choices
                .iter()
                .map(|choice| replace_transformed_value(choice, effects, old, new_values))
                .collect::<Option<Vec<_>>>()?;
            AbstractValue::choice(mapped)
        }
        // Transforms change candidate truthiness, so the mapped chain
        // degrades to the unordered choice (the pre-chain behavior).
        AbstractValue::FirstTruthy(candidates) => {
            let mapped = candidates
                .iter()
                .map(|candidate| replace_transformed_value(candidate, effects, old, new_values))
                .collect::<Option<Vec<_>>>()?;
            AbstractValue::choice(mapped)
        }
        other => escape_wrapped_identity(
            other,
            effects,
            crate::helper_meta::LexicalEscape::Contains(old.to_string()),
        ),
    }
}

/// The map value of `split SEP subject` when SEP is a static literal:
/// static string arms split into their exact `_N` member maps, raw-identity
/// arms become a map whose `_0` member keeps the raw path qualified by SEP
/// as a lexical escape (`_0` IS the whole raw string exactly when SEP does
/// not occur in it). `None` when any arm has neither meaning.
pub(super) fn split_transformed_value(
    value: &AbstractValue,
    effects: &crate::eval_effect::Effects,
    separator: &str,
) -> Option<AbstractValue> {
    match value {
        AbstractValue::StringSet(strings) => {
            let members = strings
                .iter()
                .map(|text| {
                    AbstractValue::Dict(
                        text.split(separator)
                            .enumerate()
                            .map(|(index, part)| {
                                (
                                    format!("_{index}"),
                                    AbstractValue::StringSet(
                                        [part.to_string()].into_iter().collect(),
                                    ),
                                )
                            })
                            .collect(),
                    )
                })
                .collect();
            AbstractValue::choice(members)
        }
        AbstractValue::Choice(choices) => {
            let mapped = choices
                .iter()
                .map(|choice| split_transformed_value(choice, effects, separator))
                .collect::<Option<Vec<_>>>()?;
            AbstractValue::choice(mapped)
        }
        AbstractValue::FirstTruthy(candidates) => {
            let mapped = candidates
                .iter()
                .map(|candidate| split_transformed_value(candidate, effects, separator))
                .collect::<Option<Vec<_>>>()?;
            AbstractValue::choice(mapped)
        }
        other => {
            let prefix = escape_wrapped_identity(
                other,
                effects,
                crate::helper_meta::LexicalEscape::Contains(separator.to_string()),
            )?;
            Some(AbstractValue::Overlay {
                entries: std::collections::BTreeMap::from([("_0".to_string(), prefix)]),
                fallback: Box::new(AbstractValue::Unknown),
            })
        }
    }
}

/// Marks a passthrough value's `OutputPath` arms as derived text: the
/// enclosing transform produced NEW text from them (a dynamic `replace`, a
/// truncation, a numeric cast), so an escape-qualified identity they still
/// spell no longer holds. `ValuesPath` arms need no marking — the
/// expression-level derived flags already cover them.
/// Marks `toString`'s identity-bearing operand arms as `stringified`: the
/// result IS the exact `%v` rendering of that path, so total-string-preimage
/// consumers (strict parsers, equality preimages) may keep binding the raw
/// path through the rendered text. An arm that was already text-derived
/// stringifies its derivation instead and keeps its flags alone.
pub(super) fn mark_stringified_identities(value: Option<AbstractValue>) -> Option<AbstractValue> {
    fn mark(value: AbstractValue) -> AbstractValue {
        match value {
            AbstractValue::OutputPath(path, mut meta) => {
                if !meta.derived_text
                    && !meta.shape_erased
                    && !meta.yaml_serialized
                    && !meta.json_serialized
                {
                    meta.stringified = true;
                }
                AbstractValue::OutputPath(path, meta)
            }
            AbstractValue::Choice(choices) => {
                AbstractValue::Choice(choices.into_iter().map(mark).collect())
            }
            AbstractValue::FirstTruthy(candidates) => {
                AbstractValue::FirstTruthy(candidates.into_iter().map(mark).collect())
            }
            other => other,
        }
    }
    value.map(mark)
}

pub(super) fn derive_value_text(value: Option<AbstractValue>) -> Option<AbstractValue> {
    fn mark(value: AbstractValue) -> AbstractValue {
        match value {
            AbstractValue::OutputPath(path, mut meta) => {
                meta.derived_text = true;
                AbstractValue::OutputPath(path, meta)
            }
            AbstractValue::Choice(choices) => {
                AbstractValue::Choice(choices.into_iter().map(mark).collect())
            }
            AbstractValue::FirstTruthy(candidates) => {
                AbstractValue::FirstTruthy(candidates.into_iter().map(mark).collect())
            }
            other => other,
        }
    }
    value.map(mark)
}

/// The transformed value of `trimPrefix`/`trimSuffix` with one static
/// nonempty affix: static string arms trim exactly, raw-identity arms keep
/// their path qualified by a typed affix escape — downstream captures
/// project their language through the exact stripped-affix preimage
/// (datadog's `trimSuffix "-jmx"` semver tag). `None` when any arm has
/// neither meaning.
pub(super) fn trim_affix_transformed_value(
    value: &AbstractValue,
    effects: &crate::eval_effect::Effects,
    token: &str,
    prefix: bool,
) -> Option<AbstractValue> {
    match value {
        AbstractValue::StringSet(strings) => Some(AbstractValue::StringSet(
            strings
                .iter()
                .map(|text| {
                    let trimmed = if prefix {
                        text.strip_prefix(token)
                    } else {
                        text.strip_suffix(token)
                    };
                    trimmed.unwrap_or(text).to_string()
                })
                .collect(),
        )),
        AbstractValue::Choice(choices) => {
            let mapped = choices
                .iter()
                .map(|choice| trim_affix_transformed_value(choice, effects, token, prefix))
                .collect::<Option<Vec<_>>>()?;
            AbstractValue::choice(mapped)
        }
        AbstractValue::FirstTruthy(candidates) => {
            let mapped = candidates
                .iter()
                .map(|candidate| trim_affix_transformed_value(candidate, effects, token, prefix))
                .collect::<Option<Vec<_>>>()?;
            AbstractValue::choice(mapped)
        }
        other => {
            let escape = if prefix {
                crate::helper_meta::LexicalEscape::TrimPrefix(token.to_string())
            } else {
                crate::helper_meta::LexicalEscape::TrimSuffix(token.to_string())
            };
            escape_wrapped_identity(other, effects, escape)
        }
    }
}

/// The transformed value of a `regexReplaceAll`-family call whose pattern
/// has a mandatory literal: the replacement is the identity on strings not
/// containing that literal, so raw-identity arms keep their path qualified
/// by `escape` — the exact `CutAtToken` erasure when the call strips a
/// token-opened tail, the `Contains` exemption otherwise. `None` when the
/// subject is not a clean raw identity.
pub(super) fn regex_replace_transformed_value(
    value: &AbstractValue,
    effects: &crate::eval_effect::Effects,
    escape: &crate::helper_meta::LexicalEscape,
) -> Option<AbstractValue> {
    match value {
        AbstractValue::Choice(choices) => {
            let mapped = choices
                .iter()
                .map(|choice| regex_replace_transformed_value(choice, effects, escape))
                .collect::<Option<Vec<_>>>()?;
            AbstractValue::choice(mapped)
        }
        AbstractValue::FirstTruthy(candidates) => {
            let mapped = candidates
                .iter()
                .map(|candidate| regex_replace_transformed_value(candidate, effects, escape))
                .collect::<Option<Vec<_>>>()?;
            AbstractValue::choice(mapped)
        }
        other => escape_wrapped_identity(other, effects, escape.clone()),
    }
}

/// A literal substring every match of `pattern` must contain: the literal
/// run at the pattern start (after an optional `^` anchor), minus a final
/// character an immediately following quantifier would make optional.
/// `None` when the pattern opens with a metacharacter.
pub(super) fn regex_mandatory_literal(pattern: &str) -> Option<String> {
    let pattern = pattern.strip_prefix('^').unwrap_or(pattern);
    let mut literal = String::new();
    let mut chars = pattern.chars().peekable();
    while let Some(character) = chars.peek().copied() {
        if matches!(
            character,
            '\\' | '.' | '^' | '$' | '|' | '?' | '*' | '+' | '(' | ')' | '[' | ']' | '{' | '}'
        ) {
            break;
        }
        literal.push(character);
        chars.next();
    }
    if matches!(chars.peek(), Some('?' | '*' | '{')) {
        literal.pop();
    }
    (!literal.is_empty()).then_some(literal)
}