Introduce LiteralNullConditionalExpression()

This commit is contained in:
John Gardiner Myers 2022-11-26 21:13:38 -08:00
parent 545931f9b7
commit fe065bfe6c
2 changed files with 14 additions and 1 deletions

View File

@ -296,7 +296,10 @@ func (_ *VPC) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *VPC)
return err
}
if err := t.AddOutputVariable("vpc_ipv6_cidr_length", terraformWriter.LiteralFunctionExpression("local.vpc_ipv6_cidr_block == null ? null : tonumber", "regex(\".*/(\\\\d+)\", local.vpc_ipv6_cidr_block)[0]")); err != nil {
if err := t.AddOutputVariable("vpc_ipv6_cidr_length", terraformWriter.LiteralNullConditionalExpression(
terraformWriter.LiteralTokens("local", "vpc_ipv6_cidr_block"),
terraformWriter.LiteralFunctionExpression("tonumber", "regex(\".*/(\\\\d+)\", local.vpc_ipv6_cidr_block)[0]"),
)); err != nil {
return err
}

View File

@ -77,6 +77,16 @@ func LiteralWithIndex(s string) *Literal {
}
}
// LiteralNullConditionalExpression constructs a Literal which returns `null`
// if the supplied "nullable" expression is null, otherwise returns "value".
// It is the caller's responsibility to ensure the supplied parameters do not use operators
// with lower precedence than the conditional operator.
func LiteralNullConditionalExpression(nullable, value *Literal) *Literal {
return &Literal{
String: fmt.Sprintf("%s == null ? null : %s", nullable.String, value.String),
}
}
// SortLiterals sorts a list of Literal, by key. It does so in-place
func SortLiterals(v []*Literal) {
sort.Slice(v, func(i, j int) bool {