Open Khachi-at opened 3 months ago
The updates introduce a WriteWithBackup
method to enhance the ConfigFile
functionalities, enabling configuration write operations with backup support. Corresponding tests have been added to ensure robustness. The changes affect methods and comments across several files in the config and file handling modules, ensuring uniformity and improved functionality.
File Path | Change Summary |
---|---|
env/config/.../json_config.go |
Renamed comments, updated Load and Write methods, added WriteWithBackup . |
env/config/.../json_config_test.go |
Added tests TestJSONConfigFile_WriteWithBackup and TestJSONConfigFile_WriteWithBackup_error . |
env/file/.../json.go , .../raw.go |
Updated imports, comments, and replaced Write with WriteWithBackup in the WriteConfigFile method. |
sequenceDiagram
participant User
participant ConfigFile
participant FileSystem
User->>ConfigFile: WriteWithBackup(data)
ConfigFile->>FileSystem: Backup existing file
FileSystem-->ConfigFile: Backup success/failure
ConfigFile->>FileSystem: Write new data to file
FileSystem-->ConfigFile: Write success/failure
ConfigFile-->>User: Operation success/failure
In the code, new winds do blow,
With backups in hand, our data flows,
Configs are safe, oh what a feat,
Tests are run, no risks to meet.
Embrace the change, with joy so sweet,
In lines of logic, the worlds do greet. 🌐✨
[!TIP]
AI model upgrade
## `gpt-4o` model for reviews and chat is now live OpenAI claims that this model is better at understanding and generating code than the previous models. Please join our [Discord Community](https://discord.com/invite/GsXnASn26c) to provide any feedback or to report any issues.
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
感谢您提出Pull Request,我会尽快Review。我会在1-2日内进行查看或者回复,如果遇到节假日可能会处理较慢,敬请谅解。
Could you please help to describe the steps to duplicate the original bug?
Totals | |
---|---|
Change from base Build 8956224862: | 0.1% |
Covered Lines: | 1334 |
Relevant Lines: | 1762 |
Could you please help to describe the steps to duplicate the original bug?
package apollofx
import (
"encoding/json"
"testing"
"github.com/apolloconfig/agollo/v4"
"github.com/apolloconfig/agollo/v4/env/config"
ajson "github.com/apolloconfig/agollo/v4/env/config/json"
"github.com/stretchr/testify/require"
)
func TestXxx(t *testing.T) {
loader := &ajson.ConfigFile{}
// Create app config.
appCfg := &config.AppConfig{}
_, err := loader.Load("config.json", func(bytes []byte) (interface{}, error) {
return appCfg, json.Unmarshal(bytes, appCfg)
})
require.NoError(t, err)
// Start apollo client.
client, err := agollo.StartWithConfig(func() (*config.AppConfig, error) {
return appCfg, nil
})
require.NoError(t, err)
_, err = client.GetConfigCache("namespace").Get("content")
require.NoError(t, err)
}
config.json
{
"appId": "testapp",
"cluster": "default",
"ip": "http://127.0.0.1:32026",
"namespaceName": "namespace",
"isBackupConfig": true,
"secret": "secret",
"backupConfigPath": "./"
}
Could you please help to describe the steps to duplicate the original bug?
- The test code is like this. You can duplicate the bug by running it.
package apollofx import ( "encoding/json" "testing" "github.com/apolloconfig/agollo/v4" "github.com/apolloconfig/agollo/v4/env/config" ajson "github.com/apolloconfig/agollo/v4/env/config/json" "github.com/stretchr/testify/require" ) func TestXxx(t *testing.T) { loader := &ajson.ConfigFile{} // Create app config. appCfg := &config.AppConfig{} _, err := loader.Load("config.json", func(bytes []byte) (interface{}, error) { return appCfg, json.Unmarshal(bytes, appCfg) }) require.NoError(t, err) // Start apollo client. client, err := agollo.StartWithConfig(func() (*config.AppConfig, error) { return appCfg, nil }) require.NoError(t, err) _, err = client.GetConfigCache("namespace").Get("content") require.NoError(t, err) }
config.json
{ "appId": "testapp", "cluster": "default", "ip": "http://127.0.0.1:32026", "namespaceName": "namespace", "isBackupConfig": true, "secret": "secret", "backupConfigPath": "./" }
I followed the steps and found no errors. Did I miss something?
Could you please help to describe the steps to duplicate the original bug?
- The test code is like this. You can duplicate the bug by running it.
package apollofx import ( "encoding/json" "testing" "github.com/apolloconfig/agollo/v4" "github.com/apolloconfig/agollo/v4/env/config" ajson "github.com/apolloconfig/agollo/v4/env/config/json" "github.com/stretchr/testify/require" ) func TestXxx(t *testing.T) { loader := &ajson.ConfigFile{} // Create app config. appCfg := &config.AppConfig{} _, err := loader.Load("config.json", func(bytes []byte) (interface{}, error) { return appCfg, json.Unmarshal(bytes, appCfg) }) require.NoError(t, err) // Start apollo client. client, err := agollo.StartWithConfig(func() (*config.AppConfig, error) { return appCfg, nil }) require.NoError(t, err) _, err = client.GetConfigCache("namespace").Get("content") require.NoError(t, err) }
config.json
{ "appId": "testapp", "cluster": "default", "ip": "http://127.0.0.1:32026", "namespaceName": "namespace", "isBackupConfig": true, "secret": "secret", "backupConfigPath": "./" }
I followed the steps and found no errors. Did I miss something?
Adjusting the code like this can increase the probability of duplicating the bug. You need to pay attention to the contents of the backup file. Because a new backup file is created each time, if the program is stopped after the new backup file is created and before the content is written to the backup file, the backup file will have no content.
package apollofx
import (
"encoding/json"
"testing"
"github.com/apolloconfig/agollo/v4"
"github.com/apolloconfig/agollo/v4/env/config"
ajson "github.com/apolloconfig/agollo/v4/env/config/json"
"github.com/stretchr/testify/require"
)
func TestXxx(t *testing.T) {
loader := &ajson.ConfigFile{}
// Create app config.
appCfg := &config.AppConfig{}
_, err := loader.Load("config.json", func(bytes []byte) (interface{}, error) {
return appCfg, json.Unmarshal(bytes, appCfg)
})
require.NoError(t, err)
// Start apollo client.
agollo.StartWithConfig(func() (*config.AppConfig, error) {
return appCfg, nil
})
}
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
It is found that the backup file has no content in use. The specific reason is that the file writing process stops (such as closing the process). Based on the above reasons, the backup file writing logic is optimized, and the configuration file content is first written to a new file, and then the new file is used to overwrite the old file.
Summary by CodeRabbit
New Features
Tests