Skip to content

[simplifier] Consolidate repeated planner index-by-name and consumer group dependency blocks #2254

Description

@github-actions

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
  • expires on Sep 25, 2026, 2:58 AM UTC

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions