cco 0.2.0

cascading configuration
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
use crate::eval::*;
use crate::expression::*;
use crate::*;
use std::ops::Range;
use std::sync::Arc;

pub struct Builder<'a> {
    evaluation: &'a mut Cco,
    document: Arc<Document>,
}

impl<'a> Builder<'a> {
    pub fn new(evaluation: &'a mut Cco, document: Arc<Document>) -> Self {
        Self {
            evaluation,
            document,
        }
    }

    pub fn push_expr(
        &mut self,
        expression: impl Into<Expression>,
        span: Option<Range<usize>>,
    ) -> Index {
        self.evaluation.push(Item {
            expr: Arc::new(expression.into()),
            resolution: Resolution::default(),
            cloned_from: None,
            context: vec![],
            span,
            document: Some(self.document.clone()),
        })
    }

    pub fn push_hcl_body(&mut self, body: hcl::edit::structure::Body) -> Index {
        use hcl::edit::Span;

        let span = body.span();
        let root: Vec<_> = body
            .into_attributes()
            .map(|attribute| {
                (
                    MapKey::Literal(attribute.key.value().to_string()),
                    self.push_hcl_expression(attribute.value.clone()),
                )
            })
            .collect();
        self.push_expr(Expression::map(MapExpr::from(root)), span)
    }

    pub fn push_hcl_expression(&mut self, value: hcl::edit::expr::Expression) -> Index {
        use hcl::edit::Span;
        use hcl::edit::expr::Expression::*;
        use hcl::edit::expr::ObjectKey;

        match value {
            Bool(v) => self.push_expr(*v.value(), v.span()),
            Number(n) => {
                if let Some(v) = n.value().as_i64() {
                    self.push_expr(v, n.span())
                } else {
                    self.push_expr(n.as_f64().unwrap(), n.span())
                }
            }
            String(v) => {
                let span = v.span();
                self.push_expr(v.into_value(), span)
            }
            Variable(variable) => {
                let span = variable.span();
                self.push_expr(
                    Expression::variable(VariableExpr {
                        name: variable.into_value().to_string(),
                    }),
                    span,
                )
            }
            Array(e) => {
                let span = e.span();
                let mut indices = vec![];
                for element in e {
                    indices.push(self.push_hcl_expression(element));
                }
                self.push_expr(Expression::list(ListExpr { items: indices }), span)
            }
            Object(o) => {
                let span = o.span();
                let mut indices = vec![];
                for (key, value) in o {
                    let key = match key {
                        ObjectKey::Ident(ident) => MapKey::Literal(ident.value().to_string()),
                        ObjectKey::Expression(expr) => {
                            MapKey::Expression(self.push_hcl_expression(expr))
                        }
                    };
                    let value = self.push_hcl_expression(value.into_expr());
                    indices.push((key, value));
                }
                self.push_expr(Expression::map(MapExpr::from(indices)), span)
            }
            BinaryOp(x) => {
                let span = x.span();
                let left = self.push_hcl_expression(x.lhs_expr);
                let right = self.push_hcl_expression(x.rhs_expr);
                let op = match x.operator.value() {
                    hcl::edit::expr::BinaryOperator::Plus => BinaryOperator::Add,
                    hcl::edit::expr::BinaryOperator::Minus => BinaryOperator::Sub,
                    hcl::edit::expr::BinaryOperator::Eq => BinaryOperator::Eq,
                    hcl::edit::expr::BinaryOperator::NotEq => BinaryOperator::NotEq,
                    hcl::edit::expr::BinaryOperator::LessEq => BinaryOperator::LessEq,
                    hcl::edit::expr::BinaryOperator::GreaterEq => BinaryOperator::GreaterEq,
                    hcl::edit::expr::BinaryOperator::Less => BinaryOperator::Less,
                    hcl::edit::expr::BinaryOperator::Greater => BinaryOperator::Greater,
                    hcl::edit::expr::BinaryOperator::Mul => BinaryOperator::Mul,
                    hcl::edit::expr::BinaryOperator::Div => BinaryOperator::Div,
                    hcl::edit::expr::BinaryOperator::Mod => BinaryOperator::Mod,
                    hcl::edit::expr::BinaryOperator::And => BinaryOperator::And,
                    hcl::edit::expr::BinaryOperator::Or => BinaryOperator::Or,
                };
                self.push_expr(
                    Expression::binary_op(BinaryOpExpr { left, op, right }),
                    span,
                )
            }
            StringTemplate(template) => {
                let span = template.span();
                self.push_hcl_template(template, span)
            }
            HeredocTemplate(template) => {
                let span = template.span();
                self.push_hcl_template(template.template, span)
            }
            Parenthesis(expr) => {
                // parenthesis are irrelevant
                self.push_hcl_expression(expr.into_inner())
            }
            Conditional(cond) => {
                let span = cond.span();
                let condition = self.push_hcl_expression(cond.cond_expr);
                let true_expr = self.push_hcl_expression(cond.true_expr);
                let false_expr = self.push_hcl_expression(cond.false_expr);
                self.push_expr(
                    Expression::conditional(ConditionalExpr {
                        condition,
                        true_expr,
                        false_expr,
                    }),
                    span,
                )
            }
            Traversal(traversal) => {
                let span = traversal.span();
                let expr = self.push_hcl_expression(traversal.expr);
                self.push_hcl_traversal(expr, &traversal.operators, span)
            }
            UnaryOp(op) => {
                let span = op.span();
                let expr = self.push_hcl_expression(op.expr);
                let unary_op = match op.operator.value() {
                    hcl::edit::expr::UnaryOperator::Neg => UnaryOperator::Neg,
                    hcl::edit::expr::UnaryOperator::Not => UnaryOperator::Not,
                };
                self.push_expr(
                    Expression::unary_op(UnaryOpExpr { op: unary_op, expr }),
                    span,
                )
            }
            ForExpr(for_expr) => {
                let for_expr_span = for_expr.span();

                let key_var_name = for_expr
                    .intro
                    .key_var
                    .map(|kv| self.push_expr(kv.value().to_string(), kv.span()));

                let key_expr = for_expr
                    .key_expr
                    .clone()
                    .map(|expr| self.push_hcl_expression(expr));

                let value_var_name = self.push_expr(
                    for_expr.intro.value_var.value().to_string(),
                    for_expr.intro.value_var.span(),
                );
                let source = self.push_hcl_expression(for_expr.intro.collection_expr);
                let value_expr = self.push_hcl_expression(for_expr.value_expr);

                let output = key_expr
                    .map(|key_expr| Output::Map {
                        key_expr,
                        value_expr,
                    })
                    .unwrap_or_else(|| Output::List { expr: value_expr });

                self.push_expr(
                    Expression::mapper(MapperExpr {
                        source,
                        key_name: key_var_name,
                        value_name: value_var_name,
                        output,
                    }),
                    for_expr_span,
                )
            }
            FuncCall(function) => {
                let name = function.name.name.value().to_string();
                let namespace = function
                    .name
                    .namespace
                    .iter()
                    .map(|ns| ns.value().to_string())
                    .collect();
                let args = function
                    .args
                    .iter()
                    .map(|arg| self.push_hcl_expression(arg.clone()))
                    .collect();

                self.push_expr(
                    Expression::func_call(FuncCallExpr {
                        name,
                        namespace,
                        args,
                    }),
                    function.span(),
                )
            }
            Null(_) => panic!("null is not supported"),
        }
    }

    fn push_hcl_template(
        &mut self,
        elements: impl IntoIterator<Item = hcl::edit::template::Element>,
        template_span: Option<Range<usize>>,
    ) -> Index {
        use hcl::edit::Span;
        use hcl::edit::template::Element;

        let mut parts = vec![];
        for part in elements.into_iter() {
            let part_index = match part {
                Element::Literal(value) => {
                    let span = value.span();
                    self.push_expr(value.into_value(), span)
                }
                // FIXME: we don't handle interpolation.strip
                Element::Interpolation(interpolation) => {
                    assert_eq!(
                        interpolation.strip,
                        hcl::edit::template::Strip::None,
                        "strip is not implemented"
                    );

                    self.push_hcl_expression(interpolation.expr)
                }
                Element::Directive(directive) => match &*directive {
                    hcl::edit::template::Directive::If(if_directive) => {
                        assert_eq!(
                            if_directive.if_expr.strip,
                            hcl::edit::template::Strip::None,
                            "strip is not implemented"
                        );

                        let condition =
                            self.push_hcl_expression(if_directive.if_expr.cond_expr.clone());
                        let true_expr = self.push_hcl_template(
                            if_directive.if_expr.template.clone(),
                            if_directive.if_expr.template.span(),
                        );
                        let false_expr = if let Some(else_expr) = &if_directive.else_expr {
                            assert_eq!(
                                else_expr.strip,
                                hcl::edit::template::Strip::None,
                                "strip is not implemented"
                            );

                            self.push_hcl_template(
                                else_expr.template.clone(),
                                else_expr.template.span(),
                            )
                        } else {
                            self.push_expr(Value::String("".to_string()), None)
                        };

                        self.push_expr(
                            Expression::conditional(ConditionalExpr {
                                condition,
                                true_expr,
                                false_expr,
                            }),
                            if_directive.span(),
                        )
                    }
                    hcl::edit::template::Directive::For(for_directive) => {
                        assert_eq!(
                            for_directive.for_expr.strip,
                            hcl::edit::template::Strip::None,
                            "strip is not implemented"
                        );

                        assert_eq!(
                            for_directive.endfor_expr.strip,
                            hcl::edit::template::Strip::None,
                            "strip is not implemented"
                        );

                        let source = self
                            .push_hcl_expression(for_directive.for_expr.collection_expr.clone());

                        let key_name = for_directive.for_expr.key_var.as_ref().map(|key_name| {
                            self.push_expr(key_name.value().to_string(), key_name.span())
                        });

                        let value_name = &for_directive.for_expr.value_var;
                        let value_name =
                            self.push_expr(value_name.value().to_string(), value_name.span());

                        let template = &for_directive.for_expr.template;
                        let template = self.push_hcl_template(template.clone(), template.span());

                        let output = Output::StringConcat { expr: template };

                        self.push_expr(
                            Expression::mapper(MapperExpr {
                                source,
                                key_name,
                                value_name,
                                output,
                            }),
                            for_directive.span(),
                        )
                    }
                },
            };
            parts.push(part_index);
        }
        self.push_expr(
            Expression::concat_string(ConcatStringExpr { parts }),
            template_span,
        )
    }

    fn push_hcl_traversal(
        &mut self,
        expr: Index,
        traversal_operators: &[hcl::edit::Decorated<hcl::edit::expr::TraversalOperator>],
        span: Option<Range<usize>>,
    ) -> Index {
        use hcl::edit::Span;

        let splat_position = traversal_operators.iter().position(|op| {
            matches!(
                op.value(),
                TraversalOperator::FullSplat(_) | TraversalOperator::AttrSplat(_)
            )
        });

        if let Some(splat_position) = splat_position {
            let (head, splat_and_tail) = traversal_operators.split_at(splat_position);
            let tail = &splat_and_tail[1..];

            let source = self.push_hcl_traversal(expr, head, None);

            let name = format!("$$SPLAT${source}$$");
            let value_name = self.push_expr(name.clone(), None);
            let variable = self.push_expr(Expression::Variable(name.into()), None);
            let tail = self.push_hcl_traversal(variable, tail, None);

            let mapper = Expression::mapper(MapperExpr {
                source,
                key_name: None,
                value_name,
                output: Output::List { expr: tail },
            });

            return self.push_expr(mapper, span);
        }

        use hcl::edit::expr::TraversalOperator;
        match traversal_operators.last().map(|dec| dec.value()) {
            Some(TraversalOperator::GetAttr(attr)) => {
                let expr = self.push_hcl_traversal(
                    expr,
                    &traversal_operators[..traversal_operators.len() - 1],
                    traversal_operators[0].span(),
                );
                self.push_expr(
                    Expression::traversal(TraversalExpr {
                        object: expr,
                        key: TraversalKey::String(attr.value().to_string()),
                    }),
                    span,
                )
            }
            Some(TraversalOperator::LegacyIndex(index)) => {
                let expr = self.push_hcl_traversal(
                    expr,
                    &traversal_operators[..traversal_operators.len() - 1],
                    traversal_operators[0].span(),
                );
                self.push_expr(
                    Expression::traversal(TraversalExpr {
                        object: expr,
                        key: TraversalKey::Number(*index.value() as usize),
                    }),
                    span,
                )
            }
            Some(TraversalOperator::Index(index)) => {
                let expr = self.push_hcl_traversal(
                    expr,
                    &traversal_operators[..traversal_operators.len() - 1],
                    traversal_operators[0].span(),
                );
                let key = self.push_hcl_expression(index.clone());
                self.push_expr(
                    Expression::Traversal(TraversalExpr {
                        object: expr,
                        key: TraversalKey::Expression(key),
                    }),
                    span,
                )
            }
            Some(TraversalOperator::AttrSplat(_)) | Some(TraversalOperator::FullSplat(_)) => {
                unreachable!("FullSplat/AttrSplat survived?")
            }
            None => expr,
        }
    }

    pub fn push_json(&mut self, value: serde_json::Value) -> Index {
        use serde::Deserialize;
        let value = Value::deserialize(&value).expect("Failed to import json");
        self.push_expr(Expression::literal(LiteralExpr { value }), None)
    }

    pub fn push_yaml(&mut self, value: serde_yaml::Value) -> Index {
        use serde::Deserialize;
        let value = Value::deserialize(&value).expect("Failed to import yaml");
        self.push_expr(Expression::literal(LiteralExpr { value }), None)
    }
}