Repository: opentofu/opentofu
File: internal/command/views/json/diagnostic.go (Go)

Code before:
}

func newDiagnosticExpressionValues(diag tfdiags.Diagnostic) []DiagnosticExpressionValue {
	fromExpr := diag.FromExpr()
	if fromExpr == nil {
		// no expression-related information on this diagnostic, but our
		// callers always want a non-nil slice in this case because that's
		// friendlier for JSON serialization.
		return make([]DiagnosticExpressionValue, 0)
	}

	// We may also be able to generate information about the dynamic
	// values of relevant variables at the point of evaluation, then.
	// This is particularly useful for expressions that get evaluated
	// multiple times with different values, such as blocks using
	// "count" and "for_each", or within "for" expressions.
	expr := fromExpr.Expression
	ctx := fromExpr.EvalContext
	vars := expr.Variables()
	values := make([]DiagnosticExpressionValue, 0, len(vars))
	seen := make(map[string]struct{}, len(vars))
	includeUnknown := tfdiags.DiagnosticCausedByUnknown(diag)
	includeSensitive := tfdiags.DiagnosticCausedBySensitive(diag)
Traversals:
	for _, traversal := range vars {
		for len(traversal) > 1 {
			val, diags := traversal.TraverseAbs(ctx)
			if diags.HasErrors() {
				// Skip anything that generates errors, since we probably
				// already have the same error in our diagnostics set
				// already.
				traversal = traversal[:len(traversal)-1]
				continue
			}

			traversalStr := traversalStr(traversal)
			if _, exists := seen[traversalStr]; exists {
				continue Traversals // don't show duplicates when the same variable is referenced multiple times
			}
			statement := newDiagnosticSnippetValueDescription(val, includeUnknown, includeSensitive)
			if statement == "" {
				// If we don't have anything to say about this value then we won't include
				// an entry for it at all.
				continue Traversals
			}
			values = append(values, DiagnosticExpressionValue{
				Traversal: traversalStr,
				Statement: statement,
			})
			seen[traversalStr] = struct{}{}
		}

Review comment (suggestion):
Right?
```suggestion
		// We want to describe as specific as possible a value but since
```

Code after:
}

func newDiagnosticExpressionValues(diag tfdiags.Diagnostic) []DiagnosticExpressionValue {
	fromExpr := diag.FromExpr()
	if fromExpr == nil {
		// no expression-related information on this diagnostic, but our
		// callers always want a non-nil slice in this case because that's
		// friendlier for JSON serialization.
		return make([]DiagnosticExpressionValue, 0)
	}

	// We may also be able to generate information about the dynamic
	// values of relevant variables at the point of evaluation, then.
	// This is particularly useful for expressions that get evaluated
	// multiple times with different values, such as blocks using
	// "count" and "for_each", or within "for" expressions.
	expr := fromExpr.Expression
	ctx := fromExpr.EvalContext
	vars := expr.Variables()
	values := make([]DiagnosticExpressionValue, 0, len(vars))
	seen := make(map[string]struct{}, len(vars))
	includeUnknown := tfdiags.DiagnosticCausedByUnknown(diag)
	includeSensitive := tfdiags.DiagnosticCausedBySensitive(diag)
Traversals:
	for _, traversal := range vars {
		// We want to describe as specific a value as possible but since
		// evaluation failed it's possible that the full traversal is
		// not actually valid, so we'll try gradually-shorter prefixes
		// of the traversal until we're able to find an associated
		// value, reporting the value of the entire top-level symbol
		// as our worst-case successful outcome.
		for len(traversal) >= 1 {
			val, diags := traversal.TraverseAbs(ctx)
			if diags.HasErrors() {
				// Skip anything that generates errors, since we probably
				// already have the same error in our diagnostics set
				// already.
				traversal = traversal[:len(traversal)-1]
				continue
			}

			traversalStr := traversalStr(traversal)
			if _, exists := seen[traversalStr]; exists {
				continue Traversals // don't show duplicates when the same variable is referenced multiple times
			}
			statement := newDiagnosticSnippetValueDescription(val, includeUnknown, includeSensitive)
			if statement == "" {
				// If we don't have anything to say about this value then we won't include
				// an entry for it at all.
				continue Traversals
			}