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:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user