2016-11-14 00:04:21 -06:00
|
|
|
package repl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-05-03 22:44:55 -05:00
|
|
|
"strconv"
|
2016-11-14 00:04:21 -06:00
|
|
|
"strings"
|
|
|
|
|
2021-06-24 16:53:43 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/lang/marks"
|
2020-09-09 13:13:53 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
2016-11-14 00:04:21 -06:00
|
|
|
|
2020-09-09 13:13:53 -05:00
|
|
|
// FormatValue formats a value in a way that resembles Terraform language syntax
|
|
|
|
// and uses the type conversion functions where necessary to indicate exactly
|
|
|
|
// what type it is given, so that equality test failures can be quickly
|
|
|
|
// understood.
|
|
|
|
func FormatValue(v cty.Value, indent int) string {
|
|
|
|
if !v.IsKnown() {
|
|
|
|
return "(known after apply)"
|
2018-05-03 22:44:55 -05:00
|
|
|
}
|
2021-06-25 13:27:43 -05:00
|
|
|
if v.Type().Equals(cty.String) && v.HasMark(marks.Raw) {
|
2021-04-23 09:29:50 -05:00
|
|
|
raw, _ := v.Unmark()
|
|
|
|
return raw.AsString()
|
|
|
|
}
|
2021-06-24 16:53:43 -05:00
|
|
|
if v.HasMark(marks.Sensitive) {
|
2020-10-05 12:29:51 -05:00
|
|
|
return "(sensitive)"
|
|
|
|
}
|
2020-09-09 13:13:53 -05:00
|
|
|
if v.IsNull() {
|
|
|
|
ty := v.Type()
|
2018-05-03 22:44:55 -05:00
|
|
|
switch {
|
2020-09-09 13:13:53 -05:00
|
|
|
case ty == cty.DynamicPseudoType:
|
|
|
|
return "null"
|
|
|
|
case ty == cty.String:
|
|
|
|
return "tostring(null)"
|
|
|
|
case ty == cty.Number:
|
|
|
|
return "tonumber(null)"
|
|
|
|
case ty == cty.Bool:
|
|
|
|
return "tobool(null)"
|
|
|
|
case ty.IsListType():
|
|
|
|
return fmt.Sprintf("tolist(null) /* of %s */", ty.ElementType().FriendlyName())
|
|
|
|
case ty.IsSetType():
|
|
|
|
return fmt.Sprintf("toset(null) /* of %s */", ty.ElementType().FriendlyName())
|
|
|
|
case ty.IsMapType():
|
|
|
|
return fmt.Sprintf("tomap(null) /* of %s */", ty.ElementType().FriendlyName())
|
2018-05-03 22:44:55 -05:00
|
|
|
default:
|
2020-09-09 13:13:53 -05:00
|
|
|
return fmt.Sprintf("null /* %s */", ty.FriendlyName())
|
2018-05-03 22:44:55 -05:00
|
|
|
}
|
2016-11-14 00:04:21 -06:00
|
|
|
}
|
|
|
|
|
2020-09-09 13:13:53 -05:00
|
|
|
ty := v.Type()
|
|
|
|
switch {
|
|
|
|
case ty.IsPrimitiveType():
|
|
|
|
switch ty {
|
|
|
|
case cty.String:
|
2020-11-26 10:42:31 -06:00
|
|
|
if formatted, isMultiline := formatMultilineString(v, indent); isMultiline {
|
|
|
|
return formatted
|
|
|
|
}
|
2020-09-09 13:13:53 -05:00
|
|
|
return strconv.Quote(v.AsString())
|
|
|
|
case cty.Number:
|
|
|
|
bf := v.AsBigFloat()
|
2021-01-07 15:29:49 -06:00
|
|
|
return bf.Text('f', -1)
|
2020-09-09 13:13:53 -05:00
|
|
|
case cty.Bool:
|
|
|
|
if v.True() {
|
|
|
|
return "true"
|
|
|
|
} else {
|
|
|
|
return "false"
|
|
|
|
}
|
2016-11-14 00:04:21 -06:00
|
|
|
}
|
2020-09-09 13:13:53 -05:00
|
|
|
case ty.IsObjectType():
|
|
|
|
return formatMappingValue(v, indent)
|
|
|
|
case ty.IsTupleType():
|
|
|
|
return formatSequenceValue(v, indent)
|
|
|
|
case ty.IsListType():
|
|
|
|
return fmt.Sprintf("tolist(%s)", formatSequenceValue(v, indent))
|
|
|
|
case ty.IsSetType():
|
|
|
|
return fmt.Sprintf("toset(%s)", formatSequenceValue(v, indent))
|
|
|
|
case ty.IsMapType():
|
|
|
|
return fmt.Sprintf("tomap(%s)", formatMappingValue(v, indent))
|
2016-11-14 00:04:21 -06:00
|
|
|
}
|
|
|
|
|
2020-09-09 13:13:53 -05:00
|
|
|
// Should never get here because there are no other types
|
|
|
|
return fmt.Sprintf("%#v", v)
|
2016-11-14 00:04:21 -06:00
|
|
|
}
|
|
|
|
|
2020-11-26 10:42:31 -06:00
|
|
|
func formatMultilineString(v cty.Value, indent int) (string, bool) {
|
|
|
|
str := v.AsString()
|
|
|
|
lines := strings.Split(str, "\n")
|
|
|
|
if len(lines) < 2 {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the value is indented, we use the indented form of heredoc for readability.
|
|
|
|
operator := "<<"
|
|
|
|
if indent > 0 {
|
|
|
|
operator = "<<-"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default delimiter is "End Of Text" by convention
|
|
|
|
delimiter := "EOT"
|
|
|
|
|
|
|
|
OUTER:
|
|
|
|
for {
|
|
|
|
// Check if any of the lines are in conflict with the delimiter. The
|
|
|
|
// parser allows leading and trailing whitespace, so we must remove it
|
|
|
|
// before comparison.
|
|
|
|
for _, line := range lines {
|
|
|
|
// If the delimiter matches a line, extend it and start again
|
|
|
|
if strings.TrimSpace(line) == delimiter {
|
|
|
|
delimiter = delimiter + "_"
|
|
|
|
continue OUTER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// None of the lines match the delimiter, so we're ready
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the heredoc, with indentation as appropriate.
|
|
|
|
var buf strings.Builder
|
|
|
|
|
|
|
|
buf.WriteString(operator)
|
|
|
|
buf.WriteString(delimiter)
|
|
|
|
for _, line := range lines {
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
buf.WriteString(strings.Repeat(" ", indent))
|
|
|
|
buf.WriteString(line)
|
|
|
|
}
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
buf.WriteString(strings.Repeat(" ", indent))
|
|
|
|
buf.WriteString(delimiter)
|
|
|
|
|
|
|
|
return buf.String(), true
|
|
|
|
}
|
|
|
|
|
2020-09-09 13:13:53 -05:00
|
|
|
func formatMappingValue(v cty.Value, indent int) string {
|
|
|
|
var buf strings.Builder
|
|
|
|
count := 0
|
|
|
|
buf.WriteByte('{')
|
|
|
|
indent += 2
|
|
|
|
for it := v.ElementIterator(); it.Next(); {
|
|
|
|
count++
|
|
|
|
k, v := it.Element()
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
buf.WriteString(strings.Repeat(" ", indent))
|
|
|
|
buf.WriteString(FormatValue(k, indent))
|
|
|
|
buf.WriteString(" = ")
|
|
|
|
buf.WriteString(FormatValue(v, indent))
|
2016-11-14 00:04:21 -06:00
|
|
|
}
|
2020-09-09 13:13:53 -05:00
|
|
|
indent -= 2
|
|
|
|
if count > 0 {
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
buf.WriteString(strings.Repeat(" ", indent))
|
2016-11-14 00:04:21 -06:00
|
|
|
}
|
2020-09-09 13:13:53 -05:00
|
|
|
buf.WriteByte('}')
|
|
|
|
return buf.String()
|
2016-11-14 00:04:21 -06:00
|
|
|
}
|
|
|
|
|
2020-09-09 13:13:53 -05:00
|
|
|
func formatSequenceValue(v cty.Value, indent int) string {
|
|
|
|
var buf strings.Builder
|
|
|
|
count := 0
|
|
|
|
buf.WriteByte('[')
|
|
|
|
indent += 2
|
|
|
|
for it := v.ElementIterator(); it.Next(); {
|
|
|
|
count++
|
|
|
|
_, v := it.Element()
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
buf.WriteString(strings.Repeat(" ", indent))
|
|
|
|
buf.WriteString(FormatValue(v, indent))
|
|
|
|
buf.WriteByte(',')
|
2016-11-14 00:04:21 -06:00
|
|
|
}
|
2020-09-09 13:13:53 -05:00
|
|
|
indent -= 2
|
|
|
|
if count > 0 {
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
buf.WriteString(strings.Repeat(" ", indent))
|
|
|
|
}
|
|
|
|
buf.WriteByte(']')
|
|
|
|
return buf.String()
|
2016-11-14 00:04:21 -06:00
|
|
|
}
|