From 6cc3fc6a07d85ea5989496ae03ba3fdead8a8fd1 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 3 Jan 2025 14:18:08 -0800 Subject: [PATCH] repl: FormatValue factor out printing of null values This value was too long for our function length lint rule, and factoring out the printing of null values makes this more balanced with how we're already handling unknown values and sensitive values so that the main body of FormatValue is focused on the normal value printing case. Signed-off-by: Martin Atkins --- internal/repl/format.go | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/internal/repl/format.go b/internal/repl/format.go index 840bebeccc..13fd14a997 100644 --- a/internal/repl/format.go +++ b/internal/repl/format.go @@ -26,25 +26,7 @@ func FormatValue(v cty.Value, indent int) string { return "(sensitive value)" } if v.IsNull() { - ty := v.Type() - switch { - 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()) - default: - return fmt.Sprintf("null /* %s */", ty.FriendlyName()) - } + return formatNullValue(v.Type()) } ty := v.Type() @@ -82,6 +64,27 @@ func FormatValue(v cty.Value, indent int) string { return fmt.Sprintf("%#v", v) } +func formatNullValue(ty cty.Type) string { + switch { + 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()) + default: + return fmt.Sprintf("null /* %s */", ty.FriendlyName()) + } +} + func formatMultilineString(v cty.Value, indent int) (string, bool) { str := v.AsString() lines := strings.Split(str, "\n")