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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
use std::collections::{BTreeMap, BTreeSet};

use crate::contract::FinalizedContract;
use crate::contract_normalization::{
    canonicalize_contract_uses, drop_default_guard_subsumed_duplicates,
    drop_self_truthy_subsumed_duplicates, normalize_contract_uses,
};
use crate::{ContractUse, Guard, ValueKind, YamlPath};

/// Opaque guarded contract graph for one template interpretation.
///
/// Accumulation, path rebasing, and normalization live behind this
/// contract-layer artifact instead of a raw vector owned by callers.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ContractIr {
    uses: Vec<ContractUse>,
    dependency_uses: Vec<ContractUse>,
    type_hints: BTreeMap<String, BTreeSet<String>>,
    /// Input-type hints observed only under branch predicates: they hold
    /// where those branches render, so they type conditional overlays but
    /// never the unconditional base.
    guarded_type_hints: BTreeMap<String, BTreeSet<String>>,
    /// Input-type hints from literal `default`/`coalesce` fallbacks: they
    /// type only the truthy arm of the path, so lowering must keep the
    /// whole Helm-falsy set open beside them.
    fallback_type_hints: BTreeMap<String, BTreeSet<String>>,
    /// Fallback hints observed under branch predicates: fallback-grade
    /// intent that may type conditional overlays, but never a branch whose
    /// renders all totally format.
    guarded_fallback_type_hints: BTreeMap<String, BTreeSet<String>>,
    /// Paths consumed through total stringifications (`quote`, `toString`,
    /// `join`, `printf`) anywhere in the interpretation: the chart tolerates
    /// any input type at them even when no placed row exists.
    shape_erased_value_paths: BTreeSet<String>,
    /// Paths carrying a real runtime string contract (`trunc`, `b64enc`,
    /// `fromYaml`, a dynamic `printf` format) anywhere.
    string_contract_value_paths: BTreeSet<String>,
    /// The chart's per-path range facts (direct iteration, JSON-decoded
    /// values, key/value destructuring).
    range_modes: crate::range_modes::RangeModes,
    /// Chart value subtrees supplying defaults to effective values subtrees.
    values_default_sources: BTreeSet<crate::ValuesDefaultSource>,
    /// Values subtrees merged in place over the values root; root contracts
    /// project back onto the prefixed spellings.
    values_root_overlay_prefixes: BTreeSet<String>,
    values_program_wrappers: BTreeSet<helm_schema_core::ValuesProgramWrapper>,
    /// Values paths whose nodes must NOT gain a wrapper alternative: a
    /// strict string consumer reads them BEFORE the engine's values-root
    /// rewrite, so a wrapper map there aborts rendering (nats'
    /// `nameOverride` through `fullname | trunc`).
    values_program_wrapper_exclusions: BTreeSet<String>,
    /// `fail` captures: no valid values document may satisfy one of these
    /// conjunctions.
    fail_conditions: Vec<crate::eval_effect::FailCapture>,
    dependency_values_root_fragments: BTreeSet<String>,
}

impl ContractIr {
    /// Build a contract graph from already-structured contract claims.
    ///
    /// This is the contract-layer constructor for tests and expert callers
    /// that already have semantic claims. Schema signals are still derived
    /// through [`ContractIr::finalize`], so semantic finalization stays on
    /// the contract graph rather than a serialized document.
    #[must_use]
    pub fn from_contract_uses(uses: Vec<ContractUse>) -> Self {
        Self {
            uses,
            ..Self::default()
        }
    }

    pub(crate) fn push(&mut self, contract_use: ContractUse) {
        self.uses.push(contract_use);
    }

    pub(crate) fn push_dependency_use(&mut self, contract_use: ContractUse) {
        self.dependency_uses.push(contract_use);
    }

    /// Add a pathless scalar claim for a value path.
    ///
    /// Pathless claims make a value path visible to downstream schema
    /// generation without asserting any rendered Kubernetes field shape.
    pub fn push_pathless_scalar(&mut self, source_expr: impl Into<String>) {
        self.push(ContractUse::new(
            source_expr.into(),
            YamlPath(Vec::new()),
            ValueKind::Scalar,
            Vec::new(),
            None,
        ));
    }

    /// Records a pathless fragment accepted at a dependency values root.
    pub fn push_pathless_dependency_fragment(&mut self, source_expr: impl Into<String>) {
        self.dependency_values_root_fragments
            .insert(source_expr.into());
    }

    /// Move all claims from another contract graph into this graph.
    pub fn append(&mut self, mut other: Self) {
        self.uses.append(&mut other.uses);
        self.dependency_uses.append(&mut other.dependency_uses);
        self.dependency_values_root_fragments
            .append(&mut other.dependency_values_root_fragments);
        for (path, schema_types) in other.type_hints {
            self.type_hints
                .entry(path)
                .or_default()
                .extend(schema_types);
        }
        for (path, schema_types) in other.guarded_type_hints {
            self.guarded_type_hints
                .entry(path)
                .or_default()
                .extend(schema_types);
        }
        for (path, schema_types) in other.fallback_type_hints {
            self.fallback_type_hints
                .entry(path)
                .or_default()
                .extend(schema_types);
        }
        for (path, schema_types) in other.guarded_fallback_type_hints {
            self.guarded_fallback_type_hints
                .entry(path)
                .or_default()
                .extend(schema_types);
        }
        self.shape_erased_value_paths
            .append(&mut other.shape_erased_value_paths);
        self.string_contract_value_paths
            .append(&mut other.string_contract_value_paths);
        self.range_modes.merge(&other.range_modes);
        self.values_default_sources
            .append(&mut other.values_default_sources);
        self.values_root_overlay_prefixes
            .append(&mut other.values_root_overlay_prefixes);
        self.values_program_wrappers
            .append(&mut other.values_program_wrappers);
        self.values_program_wrapper_exclusions
            .append(&mut other.values_program_wrapper_exclusions);
        for condition in std::mem::take(&mut other.fail_conditions) {
            if !self.fail_conditions.contains(&condition) {
                self.fail_conditions.push(condition);
            }
        }
    }

    /// Append guards to every claim in the graph without rewriting any paths.
    ///
    /// This is used for chart-structural activation predicates that apply to
    /// an already-scoped batch of claims, such as dependency `condition:` /
    /// `tags:` liveness from `Chart.yaml`.
    /// Record that rendering FAILS whenever `condition` holds — an
    /// unconditionally reached `include` whose helper only an inactive
    /// optional dependency defines aborts with "no template". The predicate
    /// lowers through the standard terminal-clause machinery.
    pub fn add_terminal_fail_condition(&mut self, condition: helm_schema_core::Predicate) {
        let capture = crate::eval_effect::FailCapture {
            conjunction: vec![condition],
            ranged: crate::range_modes::RangeModes::default(),
            kind: crate::eval_effect::CaptureKind::Fail,
        };
        if !self.fail_conditions.contains(&capture) {
            self.fail_conditions.push(capture);
        }
    }

    /// Conjoins activation guards onto every use and terminating failure.
    pub fn append_guards_to_all_uses(&mut self, guards: &[Guard]) {
        for contract_use in self.uses.iter_mut().chain(&mut self.dependency_uses) {
            contract_use.condition = contract_use
                .condition
                .conjoined_with_guards(guards.iter().cloned());
        }
        // Fail captures are claims too: a `fail` inside a dependency gated
        // off by `condition:` / `tags:` cannot abort rendering, so its
        // conjunction must carry the activation predicate like every row.
        for capture in &mut self.fail_conditions {
            capture.conjunction.splice(
                0..0,
                guards
                    .iter()
                    .cloned()
                    .map(helm_schema_core::Predicate::from),
            );
        }
        // A conditionally active chart cannot contribute unconditional
        // effective defaults. Conditional default overlays are not yet part
        // of the schema-signal vocabulary, so abstain instead of leaking them.
        if !guards.is_empty() {
            self.values_default_sources.clear();
            self.values_root_overlay_prefixes.clear();
            // A path-wide runtime string contract is unconditional only
            // within its own chart's rendering: under activation guards the
            // consumer may never run, so the fact must not type the base.
            self.string_contract_value_paths.clear();
        }
    }

    /// Mark rendered claims as textual output rather than structured YAML
    /// placements. Runtime operand contracts and terminal effects remain
    /// unchanged.
    pub fn mark_rendered_output_textual(&mut self) {
        for contract_use in self.uses.iter_mut().chain(&mut self.dependency_uses) {
            contract_use.kind = ValueKind::Serialized;
        }
    }

    /// Rewrite all referenced values paths while preserving rendered YAML paths.
    ///
    /// This is used at chart boundaries where a dependency's `.Values.foo`
    /// contract becomes `.Values.subchart.foo`, while rendered manifest paths
    /// such as `metadata.name` stay unchanged.
    pub fn map_value_paths<F>(&mut self, mut map: F)
    where
        F: FnMut(&str) -> String,
    {
        for contract_use in self.uses.iter_mut().chain(&mut self.dependency_uses) {
            contract_use.map_value_paths(&mut map);
        }
        self.dependency_values_root_fragments =
            std::mem::take(&mut self.dependency_values_root_fragments)
                .into_iter()
                .map(|path| map(&path))
                .collect();
        let mut type_hints: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
        for (path, schema_types) in std::mem::take(&mut self.type_hints) {
            type_hints
                .entry(map(&path))
                .or_default()
                .extend(schema_types);
        }
        self.type_hints = type_hints;
        let mut guarded_type_hints: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
        for (path, schema_types) in std::mem::take(&mut self.guarded_type_hints) {
            guarded_type_hints
                .entry(map(&path))
                .or_default()
                .extend(schema_types);
        }
        self.guarded_type_hints = guarded_type_hints;
        let mut fallback_type_hints: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
        for (path, schema_types) in std::mem::take(&mut self.fallback_type_hints) {
            fallback_type_hints
                .entry(map(&path))
                .or_default()
                .extend(schema_types);
        }
        self.fallback_type_hints = fallback_type_hints;
        let mut guarded_fallback_type_hints: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
        for (path, schema_types) in std::mem::take(&mut self.guarded_fallback_type_hints) {
            guarded_fallback_type_hints
                .entry(map(&path))
                .or_default()
                .extend(schema_types);
        }
        self.guarded_fallback_type_hints = guarded_fallback_type_hints;
        self.shape_erased_value_paths = std::mem::take(&mut self.shape_erased_value_paths)
            .into_iter()
            .map(|path| map(&path))
            .collect();
        self.string_contract_value_paths = std::mem::take(&mut self.string_contract_value_paths)
            .into_iter()
            .map(|path| map(&path))
            .collect();
        self.range_modes.map_value_paths(&mut map);
        self.values_default_sources = std::mem::take(&mut self.values_default_sources)
            .into_iter()
            .map(|source| crate::ValuesDefaultSource {
                target_path: map(&source.target_path),
                source_path: map(&source.source_path),
            })
            .collect();
        self.values_root_overlay_prefixes = std::mem::take(&mut self.values_root_overlay_prefixes)
            .into_iter()
            .map(|path| map(&path))
            .collect();
        self.values_program_wrappers = std::mem::take(&mut self.values_program_wrappers)
            .into_iter()
            .map(|wrapper| helm_schema_core::ValuesProgramWrapper {
                scope_path: map(&wrapper.scope_path),
                key: wrapper.key,
                spread: wrapper.spread,
            })
            .collect();
        self.values_program_wrapper_exclusions =
            std::mem::take(&mut self.values_program_wrapper_exclusions)
                .into_iter()
                .map(|path| map(&path))
                .collect();
        self.fail_conditions = std::mem::take(&mut self.fail_conditions)
            .into_iter()
            .map(|mut capture| {
                capture.conjunction = capture
                    .conjunction
                    .into_iter()
                    .map(|predicate| predicate.map_value_paths(&mut map))
                    .collect();
                capture.ranged.map_value_paths(&mut map);
                capture.kind.map_value_paths(&mut map);
                capture
            })
            .collect();
    }

    /// Add declared input-type hints for values paths without projecting them
    /// as inspection rows.
    pub fn add_type_hint(&mut self, path: impl Into<String>, schema_type: impl Into<String>) {
        let path = path.into();
        let schema_type = schema_type.into();
        if path.trim().is_empty() || schema_type.trim().is_empty() {
            return;
        }
        self.type_hints.entry(path).or_default().insert(schema_type);
    }

    /// Extend the graph with already-grouped path type hints.
    pub(crate) fn extend_type_hints(
        &mut self,
        type_hints: impl IntoIterator<Item = (String, BTreeSet<String>)>,
    ) {
        for (path, schema_types) in type_hints {
            if path.trim().is_empty() {
                continue;
            }
            let schema_types = schema_types
                .into_iter()
                .filter(|schema_type| !schema_type.trim().is_empty())
                .collect::<BTreeSet<_>>();
            if schema_types.is_empty() {
                continue;
            }
            self.type_hints
                .entry(path)
                .or_default()
                .extend(schema_types);
        }
    }

    pub(crate) fn extend_fallback_type_hints(
        &mut self,
        type_hints: impl IntoIterator<Item = (String, BTreeSet<String>)>,
    ) {
        for (path, schema_types) in type_hints {
            if path.trim().is_empty() {
                continue;
            }
            let schema_types = schema_types
                .into_iter()
                .filter(|schema_type| !schema_type.trim().is_empty())
                .collect::<BTreeSet<_>>();
            if schema_types.is_empty() {
                continue;
            }
            self.fallback_type_hints
                .entry(path)
                .or_default()
                .extend(schema_types);
        }
    }

    pub(crate) fn extend_guarded_fallback_type_hints(
        &mut self,
        type_hints: impl IntoIterator<Item = (String, BTreeSet<String>)>,
    ) {
        for (path, schema_types) in type_hints {
            if path.trim().is_empty() {
                continue;
            }
            let schema_types = schema_types
                .into_iter()
                .filter(|schema_type| !schema_type.trim().is_empty())
                .collect::<BTreeSet<_>>();
            if schema_types.is_empty() {
                continue;
            }
            self.guarded_fallback_type_hints
                .entry(path)
                .or_default()
                .extend(schema_types);
        }
    }

    pub(crate) fn extend_guarded_type_hints(
        &mut self,
        type_hints: impl IntoIterator<Item = (String, BTreeSet<String>)>,
    ) {
        for (path, schema_types) in type_hints {
            if path.trim().is_empty() {
                continue;
            }
            let schema_types = schema_types
                .into_iter()
                .filter(|schema_type| !schema_type.trim().is_empty())
                .collect::<BTreeSet<_>>();
            if schema_types.is_empty() {
                continue;
            }
            self.guarded_type_hints
                .entry(path)
                .or_default()
                .extend(schema_types);
        }
    }

    pub(crate) fn extend_shape_erased_value_paths(
        &mut self,
        paths: impl IntoIterator<Item = String>,
    ) {
        self.shape_erased_value_paths
            .extend(paths.into_iter().filter(|path| !path.trim().is_empty()));
    }

    pub(crate) fn extend_string_contract_value_paths(
        &mut self,
        paths: impl IntoIterator<Item = String>,
    ) {
        self.string_contract_value_paths
            .extend(paths.into_iter().filter(|path| !path.trim().is_empty()));
    }

    pub(crate) fn merge_range_modes(&mut self, range_modes: &crate::range_modes::RangeModes) {
        self.range_modes.merge(range_modes);
    }

    pub(crate) fn extend_values_default_sources(
        &mut self,
        sources: impl IntoIterator<Item = crate::ValuesDefaultSource>,
    ) {
        self.values_default_sources.extend(sources);
    }

    pub(crate) fn extend_values_root_overlay_prefixes(
        &mut self,
        prefixes: impl IntoIterator<Item = String>,
    ) {
        self.values_root_overlay_prefixes.extend(prefixes);
    }

    pub(crate) fn extend_values_program_wrappers(
        &mut self,
        wrappers: impl IntoIterator<Item = helm_schema_core::ValuesProgramWrapper>,
    ) {
        self.values_program_wrappers.extend(wrappers);
    }

    pub(crate) fn extend_values_program_wrapper_exclusions(
        &mut self,
        paths: impl IntoIterator<Item = String>,
    ) {
        self.values_program_wrapper_exclusions.extend(paths);
    }

    /// Drop evidence recorded AT a program-wrapper sentinel key: within a
    /// wrapper-engine chart a `$tplYaml`-keyed member is the engine's own
    /// dispatch convention — probed by the recursive walker, replaced
    /// before ordinary consumers read the tree — never an ordinary chart
    /// value, so reads and fail predicates over such paths must not mint
    /// values properties. The wrapper alternatives model those nodes.
    pub(crate) fn scrub_program_wrapper_sentinel_evidence(&mut self) {
        let keys: std::collections::BTreeSet<String> = self
            .values_program_wrappers
            .iter()
            .map(|wrapper| wrapper.key.clone())
            .collect();
        if keys.is_empty() {
            return;
        }
        let touches = |path: &str| {
            helm_schema_core::split_value_path(path)
                .iter()
                .any(|segment| keys.contains(segment))
        };
        self.uses
            .retain(|contract_use| !touches(&contract_use.source_expr));
        self.dependency_uses
            .retain(|contract_use| !touches(&contract_use.source_expr));
        self.fail_conditions.retain(|capture| {
            let mut paths: Vec<String> = capture
                .conjunction
                .iter()
                .flat_map(helm_schema_core::Predicate::value_paths)
                .collect();
            let mut kind = capture.kind.clone();
            kind.map_value_paths(&mut |path: &str| {
                paths.push(path.to_string());
                path.to_string()
            });
            !paths.iter().any(|path| touches(path))
        });
    }

    pub(crate) fn extend_fail_conditions(
        &mut self,
        conditions: impl IntoIterator<Item = crate::eval_effect::FailCapture>,
    ) {
        for conjunction in conditions {
            if !self.fail_conditions.contains(&conjunction) {
                self.fail_conditions.push(conjunction);
            }
        }
    }

    /// Finalize the contract once and derive downstream artifacts from that
    /// one normalized contract representation.
    #[must_use]
    #[tracing::instrument(skip_all)]
    pub fn finalize(mut self) -> FinalizedContract {
        self.scrub_program_wrapper_sentinel_evidence();
        let Self {
            mut uses,
            mut dependency_uses,
            type_hints,
            guarded_type_hints,
            fallback_type_hints,
            guarded_fallback_type_hints,
            shape_erased_value_paths,
            string_contract_value_paths,
            range_modes,
            values_default_sources,
            values_root_overlay_prefixes,
            values_program_wrappers,
            values_program_wrapper_exclusions,
            mut fail_conditions,
            dependency_values_root_fragments,
        } = self;
        for source_expr in &dependency_values_root_fragments {
            dependency_uses.push(ContractUse::new(
                source_expr.clone(),
                YamlPath(Vec::new()),
                ValueKind::Fragment,
                Vec::new(),
                None,
            ));
        }
        normalize_contract_uses(&mut uses);
        drop_self_truthy_subsumed_duplicates(&mut dependency_uses);
        canonicalize_contract_uses(&mut dependency_uses);
        uses.append(&mut dependency_uses);
        drop_default_guard_subsumed_duplicates(&mut uses);
        drop_self_truthy_subsumed_duplicates(&mut uses);
        canonicalize_contract_uses(&mut uses);
        fail_conditions.sort();
        fail_conditions.dedup();
        FinalizedContract::new(
            uses,
            &type_hints,
            &guarded_type_hints,
            &fallback_type_hints,
            &guarded_fallback_type_hints,
            &shape_erased_value_paths,
            &string_contract_value_paths,
            &range_modes,
            values_default_sources,
            values_root_overlay_prefixes,
            values_program_wrappers,
            values_program_wrapper_exclusions,
            &fail_conditions,
            &dependency_values_root_fragments,
        )
    }
}