Skip to content

[duplicate-code] Duplicate Code Pattern: Config Finalization Pipeline (TOML vs JSON Stdin Paths) #3562

@github-actions

Description

@github-actions

🔍 Duplicate Code Pattern: Config Finalization Pipeline (TOML vs JSON Stdin Paths)

Part of duplicate code analysis: #3560

Summary

Both LoadFromFile (TOML path, config_core.go) and convertStdinConfig (JSON stdin path, config_stdin.go) execute nearly identical post-parse finalization steps: validate trusted bots, apply gateway defaults, apply feature defaults, and validate guard policies. This shared pipeline is inlined in both functions rather than extracted into a reusable helper.

Duplication Details

Pattern: Repeated config finalization sequence

  • Severity: Low
  • Occurrences: 2 locations
  • Locations:
    • internal/config/config_core.go (lines 364–403) — TOML path, LoadFromFile
    • internal/config/config_stdin.go (lines 316–360) — JSON stdin path, convertStdinConfig

config_core.go (TOML path):

// Initialize gateway if not present
if cfg.Gateway == nil {
    cfg.Gateway = &GatewayConfig{}
}
if err := validateTrustedBots(cfg.Gateway.TrustedBots); err != nil {
    return nil, err
}
// ... opentelemetry merge ...
applyGatewayDefaults(cfg.Gateway)
applyDefaults(&cfg)
if cfg.Gateway.PayloadSizeThreshold < 0 {
    return nil, fmt.Errorf("gateway.payload_size_threshold must be a positive integer, ...")
}
if err := validateGuardPolicies(&cfg); err != nil {
    return nil, err
}

config_stdin.go (JSON stdin path):

if stdinCfg.Gateway != nil {
    // ...
    if stdinCfg.Gateway.TrustedBots != nil {
        if err := validateTrustedBots(stdinCfg.Gateway.TrustedBots); err != nil {
            return nil, err
        }
        // ...
    }
    applyGatewayDefaults(cfg.Gateway)
} else {
    cfg.Gateway = &GatewayConfig{}
    applyGatewayDefaults(cfg.Gateway)
}
applyDefaults(cfg)
// ...
if err := validateGuardPolicies(cfg); err != nil {
    return nil, err
}

Steps shared across both paths: validateTrustedBotsapplyGatewayDefaultsapplyDefaultsvalidateGuardPolicies

Impact Analysis

  • Maintainability: Adding a new post-parse validation step (like PayloadSizeThreshold was added to TOML) requires updating both code paths. The PayloadSizeThreshold < 0 check already exists only in LoadFromFile and not in convertStdinConfig, suggesting drift has begun.
  • Bug Risk: Low for now, but finalization step drift between config paths could cause subtle behavior differences (e.g., a validation present on TOML path but missing from JSON stdin path, as was the case with this commit's auth fix).
  • Code Bloat: ~15 lines of shared finalization logic scattered across two functions.

Refactoring Recommendations

  1. Extract finalizeConfig(cfg *Config) error

    • Place in: internal/config/config_core.go or a new internal/config/config_finalize.go
    • Encapsulates: validateTrustedBots, applyGatewayDefaults, applyDefaults, PayloadSizeThreshold validation, validateGuardPolicies
    • Both LoadFromFile and convertStdinConfig call this helper after their path-specific parsing
    • Estimated effort: 2–3 hours
    • Benefits: Single place to add/modify finalization steps, prevents future drift between config paths
  2. Lower priority than pattern 1 — This is latent structural duplication; the immediate risk is low. Consider tackling during a broader config refactor.

Implementation Checklist

  • Identify all finalization steps common to both paths
  • Extract finalizeConfig(cfg *Config) error helper
  • Update LoadFromFile and convertStdinConfig to call the helper
  • Verify PayloadSizeThreshold validation is included in the shared helper
  • Run make test-all to verify no regressions

Parent Issue

See parent analysis report: #3560
Related to #3560

Generated by Duplicate Code Detector · ● 1M ·

  • expires on Apr 18, 2026, 5:52 AM UTC

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions