🔧 Semantic Refactor: Duplicate kbapi-union decode diagnostics helper in dashboard panel packages
Analysis of repository: elastic/terraform-provider-elasticstack
Summary
Two dashboard-panel sub-packages each define a private helper with an identical signature and near-identical body whose only difference is the hard-coded entity name in the error message. This is a textbook "extract common function" opportunity, and likely a template for other panel types added in the future.
Concrete Evidence
Opportunity: *DecodeDiagnostics(err error, branch string) diag.Diagnostics duplicated verbatim
-
Severity: Low
-
Type: duplicate-function
-
Locations:
internal/kibana/dashboard/panel/fieldstatstable/from_api.go — function fieldStatsTableDecodeDiagnostics(err error, branch string) diag.Diagnostics (lines 118-125)
internal/kibana/dashboard/panel/discoversession/populate.go — function discoverSessionDecodeDiagnostics(err error, branch string) diag.Diagnostics (lines 139-146)
-
Code Sample:
// internal/kibana/dashboard/panel/fieldstatstable/from_api.go:118
func fieldStatsTableDecodeDiagnostics(err error, branch string) diag.Diagnostics {
var diags diag.Diagnostics
diags.AddError(
"Failed to decode field_stats_table API config",
"Could not decode the API field_stats_table "+branch+" config: "+err.Error(),
)
return diags
}
// internal/kibana/dashboard/panel/discoversession/populate.go:139
func discoverSessionDecodeDiagnostics(err error, branch string) diag.Diagnostics {
var diags diag.Diagnostics
diags.AddError(
"Failed to decode discover_session API config",
"Could not decode the API discover_session "+branch+" config: "+err.Error(),
)
return diags
}
The bodies are structurally identical: build a diag.Diagnostics, add one error whose summary is "Failed to decode "+entityName+" API config" and whose detail is "Could not decode the API "+entityName+" "+branch+" config: "+err.Error(). Only the literal entityName string (field_stats_table vs discover_session) differs.
-
Related but distinct: fieldstatstable/from_api.go:129 also has a sibling fieldStatsTableProbeDiagnostics(err error) diag.Diagnostics for a slightly different failure mode (union probing vs union decoding). If a shared helper is introduced, consider covering both shapes so future panel packages (there are ~35 panel sub-packages under internal/kibana/dashboard/panel/) don't reintroduce the same boilerplate a third time.
Impact Analysis
- Maintainability: Any change to the error-message wording or diagnostic shape (e.g. adding an error code, wrapping the original error, adding remediation text) has to be made in two places today, and in every future panel package that copies this pattern.
- Organization:
internal/kibana/dashboard/panel/iface already exists as a shared package for panel-related cross-cutting concerns, making it a natural home for a shared helper without introducing a new package.
- Duplication Risk: With ~35 panel sub-packages sharing the same kbapi-union-decoding shape (
AsKibanaHTTPAPIs...N() variants), this two-instance duplication is likely to silently grow as new panel types are added by copy-paste from an existing package.
Refactoring Recommendations
- Extract a shared helper into
internal/kibana/dashboard/panel/iface (or a new small panelutil package)
- Target:
internal/kibana/dashboard/panel/iface/diagnostics.go (new file)
- Action: extract
- Suggested signature:
func UnionDecodeDiagnostics(entityName string, err error, branch string) diag.Diagnostics
- Update
fieldstatstable/from_api.go and discoversession/populate.go to call iface.UnionDecodeDiagnostics("field_stats_table", err, attrByEsql) / iface.UnionDecodeDiagnostics("discover_session", err, "by-reference") respectively, and delete the two private duplicates.
- Estimated effort: <1 hour (small, mechanical, well-covered by existing unit-level behavior since the error string is preserved).
- Benefits: single place to evolve diagnostic wording; removes ~16 lines of duplicated code; establishes the pattern new panel packages should follow instead of copy-pasting.
Implementation Checklist
Analysis Metadata
- Analyzed Files: ~1457 non-test
.go files under internal/ and provider/
- Detection Method: Serena project activation + targeted regex sweeps for
*DecodeDiagnostics/*ProbeDiagnostics-shaped helpers across internal/kibana/dashboard/panel/**, followed by manual line-level comparison of the two matching bodies
- Analysis Date: 2026-07-31
Generated by Semantic Function Refactor · sonnet50 · 24 AIC · ⌖ 0.497 AIC · ◷
🔧 Semantic Refactor: Duplicate kbapi-union decode diagnostics helper in dashboard panel packages
Analysis of repository: elastic/terraform-provider-elasticstack
Summary
Two dashboard-panel sub-packages each define a private helper with an identical signature and near-identical body whose only difference is the hard-coded entity name in the error message. This is a textbook "extract common function" opportunity, and likely a template for other panel types added in the future.
Concrete Evidence
Opportunity:
*DecodeDiagnostics(err error, branch string) diag.Diagnosticsduplicated verbatimSeverity: Low
Type: duplicate-function
Locations:
internal/kibana/dashboard/panel/fieldstatstable/from_api.go— functionfieldStatsTableDecodeDiagnostics(err error, branch string) diag.Diagnostics(lines 118-125)internal/kibana/dashboard/panel/discoversession/populate.go— functiondiscoverSessionDecodeDiagnostics(err error, branch string) diag.Diagnostics(lines 139-146)Code Sample:
The bodies are structurally identical: build a
diag.Diagnostics, add one error whose summary is"Failed to decode "+entityName+" API config"and whose detail is"Could not decode the API "+entityName+" "+branch+" config: "+err.Error(). Only the literalentityNamestring (field_stats_tablevsdiscover_session) differs.Related but distinct:
fieldstatstable/from_api.go:129also has a siblingfieldStatsTableProbeDiagnostics(err error) diag.Diagnosticsfor a slightly different failure mode (union probing vs union decoding). If a shared helper is introduced, consider covering both shapes so future panel packages (there are ~35 panel sub-packages underinternal/kibana/dashboard/panel/) don't reintroduce the same boilerplate a third time.Impact Analysis
internal/kibana/dashboard/panel/ifacealready exists as a shared package for panel-related cross-cutting concerns, making it a natural home for a shared helper without introducing a new package.AsKibanaHTTPAPIs...N()variants), this two-instance duplication is likely to silently grow as new panel types are added by copy-paste from an existing package.Refactoring Recommendations
internal/kibana/dashboard/panel/iface(or a new smallpanelutilpackage)internal/kibana/dashboard/panel/iface/diagnostics.go(new file)func UnionDecodeDiagnostics(entityName string, err error, branch string) diag.Diagnosticsfieldstatstable/from_api.goanddiscoversession/populate.goto calliface.UnionDecodeDiagnostics("field_stats_table", err, attrByEsql)/iface.UnionDecodeDiagnostics("discover_session", err, "by-reference")respectively, and delete the two private duplicates.Implementation Checklist
Analysis Metadata
.gofiles underinternal/andprovider/*DecodeDiagnostics/*ProbeDiagnostics-shaped helpers acrossinternal/kibana/dashboard/panel/**, followed by manual line-level comparison of the two matching bodies