Skip to content

refactor(config): replace manual viper mapping with Unmarshal#870

Merged
appleboy merged 3 commits intomasterfrom
refactor/simplify-config-package
Apr 8, 2026
Merged

refactor(config): replace manual viper mapping with Unmarshal#870
appleboy merged 3 commits intomasterfrom
refactor/simplify-config-package

Conversation

@appleboy
Copy link
Copy Markdown
Owner

@appleboy appleboy commented Apr 8, 2026

Summary

  • Replace 84 manual viper.GetX() calls in loadConfigFromViper() with a single viper.Unmarshal() call using existing yaml struct tags
  • Remove the 116-line defaultConf YAML blob that duplicated setDefaults(), consolidating into a single source of truth for all config defaults
  • Use viper.New() instead of the global viper singleton to prevent test pollution across 65+ LoadConf() calls
  • Extract ~60 shared test assertions from TestValidateConfDefault and TestValidateConf into an assertCommonConfig helper

Test plan

  • go test -v -tags sqlite ./config/... — all config tests pass
  • go test -tags sqlite ./app/... ./core/... ./logx/... ./metric/... ./rpc/... — all non-infra tests pass
  • go test -tags sqlite -run TestMissing ./notify/... — Huawei tests pass with fixed assertions
  • make lint — 0 issues
  • Pre-existing failures (Redis unavailable, FCM credentials missing) are unchanged

🤖 Generated with Claude Code

- Replace 84 manual viper.GetX() calls with viper.Unmarshal() using yaml struct tags
- Remove 116-line defaultConf YAML blob, consolidate all defaults into setDefaults()
- Use viper.New() instead of global singleton to prevent test pollution
- Extract shared test assertions into assertCommonConfig helper
- Remove tests that referenced deleted defaultConf variable
- Fix Huawei test that relied on placeholder default values

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 8, 2026 14:36
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors configuration loading to reduce manual viper key mapping by unmarshalling directly into the existing ConfYaml struct, consolidating defaults into setDefaults(), and reducing test duplication.

Changes:

  • Replace extensive viper.GetX() field-by-field mapping with viper.Unmarshal() (using existing yaml tags) and a small post-processing step for FeedbackHeader.
  • Remove the embedded default YAML blob and rely on a single defaults source (setDefaults(v)), using a per-call viper.New() instance to avoid global state pollution.
  • Refactor config tests to share common assertions via an assertCommonConfig helper and adjust Huawei HMS test setup.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
config/config.go Switch to instance-based viper + Unmarshal and consolidate config defaults in one place.
config/config_test.go Remove tests tied to the removed default YAML blob; dedupe shared assertions via a helper.
go.mod Promote github.com/go-viper/mapstructure/v2 to a direct dependency for decoder config customization.
notify/notification_hms_test.go Ensure TestMissingHuaweiAppID fails for the intended missing AppID case by setting a secret.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread config/config.go Outdated
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread config/config.go Outdated
Comment thread config/config.go
- Fix misleading comment about config file search behavior
- Return error on config parse/permission failures instead of silently
  falling back to defaults; only ignore ConfigFileNotFoundError

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@appleboy appleboy merged commit 05d41f6 into master Apr 8, 2026
16 of 17 checks passed
@appleboy appleboy deleted the refactor/simplify-config-package branch April 8, 2026 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants