Code Simplification - 2026-09-20
This issue recommends code simplification to recently modified planner files to improve clarity, consistency, and maintainability while preserving all functionality.
Files to simplify
internal/declarative/planner/ai_gateway_tls_planner.go — Replace 3 identical indexAIGateway{Certificates,CACertificates,SNIs} functions with a shared generic helper
internal/declarative/planner/ai_gateway_config_store_planner.go — Replace indexAIGatewayConfigStores with the same helper
internal/declarative/planner/ai_gateway_consumer_credential_planner.go — Replace indexAIGatewayConsumerCredentials with the same helper
internal/declarative/planner/ai_gateway_consumer_group_planner.go — Replace indexAIGatewayConsumerGroups with the same helper; extract repeated dependency computation block
internal/declarative/planner/name_matched_child_reconciler.go — Optionally use the same helper to replace the inline index loop
Improvements
1. Reduced Complexity — Generic indexByName helper
Six standalone index functions across recently changed planner files share the same structure: build map[string]T from []T by extracting a name. They differ only in type and name accessor. A generic helper consolidates them:
func indexByName[T any](items []T, name func(T) string) map[string]T {
m := make(map[string]T, len(items))
for _, item := range items {
if n := name(item); n != "" {
m[n] = item
}
}
return m
}
Before (repeated 6 times across files, shown for TLS planner):
func indexAIGatewayCertificates(
current []state.AIGatewayCertificate,
) map[string]state.AIGatewayCertificate {
byName := make(map[string]state.AIGatewayCertificate, len(current))
for _, certificate := range current {
byName[certificate.Name] = certificate
}
return byName
}
After (one-liner at each call site):
byName := indexByName(current, func(c state.AIGatewayCertificate) string { return c.Name })
Affected functions across recently changed files:
| Function |
File |
Line |
indexAIGatewayCertificates |
ai_gateway_tls_planner.go |
472 |
indexAIGatewayCACertificates |
ai_gateway_tls_planner.go |
482 |
indexAIGatewaySNIs |
ai_gateway_tls_planner.go |
492 |
indexAIGatewayConfigStores |
ai_gateway_config_store_planner.go |
193 |
indexAIGatewayConsumerCredentials |
ai_gateway_consumer_credential_planner.go |
289 |
indexAIGatewayConsumerGroups |
ai_gateway_consumer_group_planner.go |
349 |
Four additional index functions in unchanged files also follow this pattern and can be consolidated in the same pass:
indexAIGateways (ai_gateway_planner.go:690)
indexAIGatewayConsumers (ai_gateway_consumer_planner.go:344)
indexAIGatewayMCPServers (ai_gateway_mcp_server_planner.go:326)
indexAIGatewayDataPlaneCertificatesByTitle (ai_gateway_data_plane_certificate_planner.go:225)
The reconcileNameMatchedChildren reconciler also has an inline version of this pattern at lines 27-32 of name_matched_child_reconciler.go.
2. Enhanced Clarity — Consumer group dependency extraction
In ai_gateway_consumer_group_planner.go, the same 6-line dependency computation block appears three times (lines 61-66, 89-94, 127-132):
dependsOn := aiGatewayConsumerGroupPolicyCreateDependencies(
desiredGroup,
policyCreateDepsByRefOrName,
)
for _, dep := range aiGatewayConsumerGroupConsumerCreateDependencies(desiredGroup, consumerCreateDepsByRefOrName) {
dependsOn = appendDependsOn(dependsOn, dep)
}
Extract to:
func aiGatewayConsumerGroupDependsOn(
group resources.AIGatewayConsumerGroupResource,
policyDeps, consumerDeps map[string]string,
) []string {
dependsOn := aiGatewayConsumerGroupPolicyCreateDependencies(group, policyDeps)
for _, dep := range aiGatewayConsumerGroupConsumerCreateDependencies(group, consumerDeps) {
dependsOn = appendDependsOn(dependsOn, dep)
}
return dependsOn
}
Each call site becomes a single line, making the main loop body significantly more readable.
Changes Based On
Recent changes from:
Implementor Must Ensure
- ✅ Format passes (
make format produces no changes)
- ✅ Build succeeds (
make build)
- ✅ Linting passes (
make lint)
- ✅ All tests pass (
make test-all)
- ✅ No functional changes — behavior is identical
Review Focus
Please verify:
- Functionality is preserved
- Simplifications improve code quality
- Changes align with project conventions (Go 1.26 generics are already used in the codebase)
- No unintended side effects
- Tests are not changed
Recommended by Code Simplifier Agent
Generated by Code Simplifier · opus46 · 378.3 AIC · ⌖ 30.3 AIC · ⊞ 5.9K · ◷
Add this agentic workflow to your repo
To install this agentic workflow, run
gh aw add githubnext/agentics/workflows/code-simplifier.md@eb7950f37d350af6fa09d19827c4883e72947221
Code Simplification - 2026-09-20
This issue recommends code simplification to recently modified planner files to improve clarity, consistency, and maintainability while preserving all functionality.
Files to simplify
internal/declarative/planner/ai_gateway_tls_planner.go— Replace 3 identicalindexAIGateway{Certificates,CACertificates,SNIs}functions with a shared generic helperinternal/declarative/planner/ai_gateway_config_store_planner.go— ReplaceindexAIGatewayConfigStoreswith the same helperinternal/declarative/planner/ai_gateway_consumer_credential_planner.go— ReplaceindexAIGatewayConsumerCredentialswith the same helperinternal/declarative/planner/ai_gateway_consumer_group_planner.go— ReplaceindexAIGatewayConsumerGroupswith the same helper; extract repeated dependency computation blockinternal/declarative/planner/name_matched_child_reconciler.go— Optionally use the same helper to replace the inline index loopImprovements
1. Reduced Complexity — Generic
indexByNamehelperSix standalone index functions across recently changed planner files share the same structure: build
map[string]Tfrom[]Tby extracting a name. They differ only in type and name accessor. A generic helper consolidates them:Before (repeated 6 times across files, shown for TLS planner):
After (one-liner at each call site):
Affected functions across recently changed files:
indexAIGatewayCertificatesai_gateway_tls_planner.goindexAIGatewayCACertificatesai_gateway_tls_planner.goindexAIGatewaySNIsai_gateway_tls_planner.goindexAIGatewayConfigStoresai_gateway_config_store_planner.goindexAIGatewayConsumerCredentialsai_gateway_consumer_credential_planner.goindexAIGatewayConsumerGroupsai_gateway_consumer_group_planner.goFour additional index functions in unchanged files also follow this pattern and can be consolidated in the same pass:
indexAIGateways(ai_gateway_planner.go:690)indexAIGatewayConsumers(ai_gateway_consumer_planner.go:344)indexAIGatewayMCPServers(ai_gateway_mcp_server_planner.go:326)indexAIGatewayDataPlaneCertificatesByTitle(ai_gateway_data_plane_certificate_planner.go:225)The
reconcileNameMatchedChildrenreconciler also has an inline version of this pattern at lines 27-32 ofname_matched_child_reconciler.go.2. Enhanced Clarity — Consumer group dependency extraction
In
ai_gateway_consumer_group_planner.go, the same 6-line dependency computation block appears three times (lines 61-66, 89-94, 127-132):Extract to:
Each call site becomes a single line, making the main loop body significantly more readable.
Changes Based On
Recent changes from:
Implementor Must Ensure
make formatproduces no changes)make build)make lint)make test-all)Review Focus
Please verify:
Recommended by Code Simplifier Agent
Add this agentic workflow to your repo
To install this agentic workflow, run