Add generic config field framework #2483
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Introduces a declarative framework for managing configuration fields that reduces boilerplate and ensures consistency. Demonstrates the framework's immediate value by migrating OTEL configuration, eliminating ~450 lines of boilerplate code.
Changes
Part 1: Generic Config Field Framework
New files in
pkg/config/:fields.go: Core framework + helper constructors (259 lines)fields_builtin.go: Built-in field registrations (142 lines)fields_test.go: Framework tests (582 lines)fields_builtin_test.go: Built-in field tests (369 lines)doc.go: Package documentation (110 lines)Key Components:
RegisterStringField(): For simple string fieldsRegisterBoolField(): For boolean fields with auto conversionRegisterFloatField(): For float64 fields with auto conversionRegisterStringSliceField(): For comma-separated string listsPart 2: OTEL Migration (Demonstrates Framework Value)
Refactored
cmd/thv/app/otel.go:Refactored
pkg/config/fields_otel.go:Framework Compression Techniques Applied
✅ Removed unused metadata: DisplayName, HelpText (~30 lines saved)
✅ Removed optional IsSet: Defaults to checking empty string (~20 lines saved)
✅ Added helper constructors: 4 type-specific helpers (~150 lines saved in field registrations)
Benefits
✅ Minimal Boilerplate: Add string field in 3 lines, bool field in 3 lines
✅ Proven Immediately: OTEL migration shows 78% code reduction
✅ Consistent Validation: All fields use same patterns and error messages
✅ Easy Maintenance: Change logic once, applies to all fields
✅ Comprehensive Tests: Full test coverage with parallel execution
Example: Adding a New Field
Test Plan
🤖 Generated with Claude Code