1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-08-06 19:44:01 +08:00

go-ipfs-config: add a Clone function

The user must call this before modifying the config. Given that the config
contains slices/maps modifications can modified *shared* state, even after
dereferencing.
This commit is contained in:
Steven Allen
2018-10-23 09:47:51 -07:00
parent 16f3d2269a
commit b627585f28
2 changed files with 39 additions and 0 deletions

View File

@ -110,3 +110,19 @@ func ToMap(conf *Config) (map[string]interface{}, error) {
}
return m, nil
}
// Clone copies the config. Use when updating.
func (c *Config) Clone() (*Config, error) {
var newConfig Config
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(c); err != nil {
return nil, fmt.Errorf("failure to encode config: %s", err)
}
if err := json.NewDecoder(&buf).Decode(&newConfig); err != nil {
return nil, fmt.Errorf("failure to decode config: %s", err)
}
return &newConfig, nil
}