🔍 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: validateTrustedBots → applyGatewayDefaults → applyDefaults → validateGuardPolicies
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
-
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
-
Lower priority than pattern 1 — This is latent structural duplication; the immediate risk is low. Consider tackling during a broader config refactor.
Implementation Checklist
Parent Issue
See parent analysis report: #3560
Related to #3560
Generated by Duplicate Code Detector · ● 1M · ◷
🔍 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) andconvertStdinConfig(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
internal/config/config_core.go(lines 364–403) — TOML path,LoadFromFileinternal/config/config_stdin.go(lines 316–360) — JSON stdin path,convertStdinConfigconfig_core.go(TOML path):config_stdin.go(JSON stdin path):Steps shared across both paths:
validateTrustedBots→applyGatewayDefaults→applyDefaults→validateGuardPoliciesImpact Analysis
PayloadSizeThresholdwas added to TOML) requires updating both code paths. ThePayloadSizeThreshold < 0check already exists only inLoadFromFileand not inconvertStdinConfig, suggesting drift has begun.Refactoring Recommendations
Extract
finalizeConfig(cfg *Config) errorinternal/config/config_core.goor a newinternal/config/config_finalize.govalidateTrustedBots,applyGatewayDefaults,applyDefaults,PayloadSizeThresholdvalidation,validateGuardPoliciesLoadFromFileandconvertStdinConfigcall this helper after their path-specific parsingLower priority than pattern 1 — This is latent structural duplication; the immediate risk is low. Consider tackling during a broader config refactor.
Implementation Checklist
finalizeConfig(cfg *Config) errorhelperLoadFromFileandconvertStdinConfigto call the helperPayloadSizeThresholdvalidation is included in the shared helpermake test-allto verify no regressionsParent Issue
See parent analysis report: #3560
Related to #3560