Repository: opentofu/opentofu
File: internal/tfdiags/consolidate_warnings.go (Go)

Code before:
	diagnosticGroups := make(map[string]*consolidatedGroup)

	for _, diag := range diags {
		severity := diag.Severity()
		if severity != level || diag.Source().Subject == nil {
			// Only the given level can get special treatment, and we only
			// consolidate diagnostics that have source locations because
			// our primary goal here is to deal with the situation where
			// some configuration language feature is producing a diagnostic
			// each time it's used across a potentially-large config.
			newDiags = newDiags.Append(diag)
			continue
		}

		if DoNotConsolidateDiagnostic(diag) {
			// Then do not consolidate this diagnostic.
			newDiags = newDiags.Append(diag)
			continue
		}

		desc := diag.Description()
		summary := desc.Summary
		var consolidationKey string
		// If the diagnostic has a keyable extra info, use it as the consolidation key,
		// otherwise use the summary.
		if key, keyOk := interface{}(diag.ExtraInfo()).(Keyable); keyOk {
			consolidationKey = key.ExtraInfoKey()
		} else {
			consolidationKey = summary
		}

		if g, ok := diagnosticGroups[consolidationKey]; ok {
			// We're already grouping this one, so we'll just continue it.
			g.Append(diag)
			continue
		}

		diagnosticStats[consolidationKey]++
		if diagnosticStats[consolidationKey] == threshold {
			// Initially creating the group doesn't really change anything
			// visibly in the result, since a group with only one diagnostic
			// is just a passthrough anyway, but once we do this any additional
			// diagnostics with the same consolidationKey will get appended to this group.
			g := &consolidatedGroup{}
			newDiags = newDiags.Append(g)
			diagnosticGroups[consolidationKey] = g
			g.Append(diag)
			continue
		}

		// If this diagnostic is not consolidating yet then we'll just append

Review comment (suggestion):
```suggestion
		if key, keyOk := diag.ExtraInfo().(Keyable); keyOk {
```

Code after:
			// Only the given level can get special treatment, and we only
			// consolidate diagnostics that have source locations because
			// our primary goal here is to deal with the situation where
			// some configuration language feature is producing a diagnostic
			// each time it's used across a potentially-large config.
			newDiags = newDiags.Append(diag)
			continue
		}

		if DoNotConsolidateDiagnostic(diag) {
			// Then do not consolidate this diagnostic.
			newDiags = newDiags.Append(diag)
			continue
		}

		desc := diag.Description()
		consolidationKey := desc.Summary
		// If the diagnostic has a keyable extra info and it's not empty,
		// use it as the consolidation key, along with the summary.
		// Otherwise use the summary only.
		if key, keyOk := diag.ExtraInfo().(Keyable); keyOk {
			consolidationKey += key.ExtraInfoKey()
		}

		if g, ok := diagnosticGroups[consolidationKey]; ok {
			// We're already grouping this one, so we'll just continue it.
			g.Append(diag)
			continue
		}

		diagnosticStats[consolidationKey]++
		if diagnosticStats[consolidationKey] == threshold {
			// Initially creating the group doesn't really change anything
			// visibly in the result, since a group with only one diagnostic
			// is just a passthrough anyway, but once we do this any additional
			// diagnostics with the same consolidationKey will get appended to this group.
			g := &consolidatedGroup{}
			newDiags = newDiags.Append(g)
			diagnosticGroups[consolidationKey] = g
			g.Append(diag)
			continue
		}

		// If this diagnostic is not consolidating yet then we'll just append
		// it directly.
		newDiags = newDiags.Append(diag)
	}

	return newDiags
}