repl: break typeString into some smaller functions

This function was too long for our function length linting rule, so we'll
split each of the type kinds with special handling into their own function
and thus the main typeString function is just a straightforward dispatch
table with only one statement per case.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
This commit is contained in:
Martin Atkins 2025-01-03 14:19:41 -08:00
parent 6cc3fc6a07
commit da8a2ecfce

View File

@ -104,16 +104,13 @@ Control-D.
return strings.TrimSpace(text), nil
}
// Modified copy of TypeString from go-cty:
// typeString returns a string representation of a given type that is
// reminiscent of the OpenTofu type constraint syntax that might be used
// to declare the type as part of an input variable declaration.
//
// This is a modified copy of TypeString from go-cty-debug, adapted to
// produce HCL-like type expressions instead of Go expressions:
// https://github.com/zclconf/go-cty-debug/blob/master/ctydebug/type_string.go
//
// TypeString returns a string representation of a given type that is
// reminiscent of Go syntax calling into the cty package but is mainly
// intended for easy human inspection of values in tests, debug output, etc.
//
// The resulting string will include newlines and indentation in order to
// increase the readability of complex structures. It always ends with a
// newline, so you can print this result directly to your output.
func typeString(ty cty.Type) string {
var b strings.Builder
writeType(ty, &b, 0)
@ -123,9 +120,21 @@ func typeString(ty cty.Type) string {
func writeType(ty cty.Type, b *strings.Builder, indent int) {
switch {
case ty == cty.NilType:
b.WriteString("nil")
return
b.WriteString("nil") // not actually a useful type to print, but handled for robustness
case ty.IsObjectType():
writeObjectType(ty, b, indent)
case ty.IsTupleType():
writeTupleType(ty, b, indent)
case ty.IsCollectionType():
writeCollectionType(ty, b, indent)
default:
// For any other type we'll just use its GoString and assume it'll
// follow the usual GoString conventions.
b.WriteString(ty.FriendlyName())
}
}
func writeObjectType(ty cty.Type, b *strings.Builder, indent int) {
atys := ty.AttributeTypes()
if len(atys) == 0 {
b.WriteString("object({})")
@ -148,7 +157,9 @@ func writeType(ty cty.Type, b *strings.Builder, indent int) {
indent--
b.WriteString(indentSpaces(indent))
b.WriteString("})")
case ty.IsTupleType():
}
func writeTupleType(ty cty.Type, b *strings.Builder, indent int) {
etys := ty.TupleElementTypes()
if len(etys) == 0 {
b.WriteString("tuple([])")
@ -164,7 +175,9 @@ func writeType(ty cty.Type, b *strings.Builder, indent int) {
indent--
b.WriteString(indentSpaces(indent))
b.WriteString("])")
case ty.IsCollectionType():
}
func writeCollectionType(ty cty.Type, b *strings.Builder, indent int) {
ety := ty.ElementType()
switch {
case ty.IsListType():
@ -198,11 +211,6 @@ func writeType(ty cty.Type, b *strings.Builder, indent int) {
b.WriteString(indentSpaces(indent))
}
b.WriteString(")")
default:
// For any other type we'll just use its GoString and assume it'll
// follow the usual GoString conventions.
b.WriteString(ty.FriendlyName())
}
}
func indentSpaces(level int) string {