github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/jsonformat/computed/renderers/string.go (about)

     1  package renderers
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
     8  )
     9  
    10  type evaluatedString struct {
    11  	String string
    12  	Json   interface{}
    13  
    14  	IsMultiline bool
    15  }
    16  
    17  func evaluatePrimitiveString(value interface{}, opts computed.RenderHumanOpts) evaluatedString {
    18  	if value == nil {
    19  		return evaluatedString{String: opts.Colorize.Color("[dark_gray]null[reset]")}
    20  	}
    21  
    22  	str := value.(string)
    23  
    24  	if strings.HasPrefix(str, "{") || strings.HasPrefix(str, "[") {
    25  		var jv interface{}
    26  		if err := json.Unmarshal([]byte(str), &jv); err == nil {
    27  			return evaluatedString{
    28  				String: str,
    29  				Json:   jv,
    30  			}
    31  		}
    32  	}
    33  
    34  	if strings.Contains(str, "\n") {
    35  		return evaluatedString{
    36  			String:      strings.TrimSpace(str),
    37  			IsMultiline: true,
    38  		}
    39  	}
    40  
    41  	return evaluatedString{
    42  		String: str,
    43  	}
    44  }