Files
opencloud/vendor/github.com/gookit/config/v2/write.go
dependabot[bot] 452d40dad7 build(deps): bump github.com/gookit/config/v2 from 2.2.5 to 2.2.6
Bumps [github.com/gookit/config/v2](https://github.com/gookit/config) from 2.2.5 to 2.2.6.
- [Release notes](https://github.com/gookit/config/releases)
- [Commits](https://github.com/gookit/config/compare/v2.2.5...v2.2.6)

---
updated-dependencies:
- dependency-name: github.com/gookit/config/v2
  dependency-version: 2.2.6
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-04-29 14:15:29 +00:00

66 lines
1.3 KiB
Go

package config
import (
"errors"
"strings"
"github.com/gookit/goutil/maputil"
)
// some common errors definitions
var (
ErrReadonly = errors.New("the config instance in 'readonly' mode")
ErrKeyIsEmpty = errors.New("the config key is cannot be empty")
ErrNotFound = errors.New("this key does not exist in the config")
)
// SetData for override the Config.Data
func SetData(data map[string]any) {
dc.SetData(data)
}
// SetData for override the Config.Data
func (c *Config) SetData(data map[string]any) {
c.lock.Lock()
c.data = data
c.lock.Unlock()
c.fireHook(OnSetData)
}
// Set value by key. setByPath default is true
func Set(key string, val any, setByPath ...bool) error {
return dc.Set(key, val, setByPath...)
}
// Set a value by key string. setByPath default is true
func (c *Config) Set(key string, val any, setByPath ...bool) (err error) {
if c.opts.Readonly {
return ErrReadonly
}
c.lock.Lock()
defer c.lock.Unlock()
sep := c.opts.Delimiter
if key = formatKey(key, string(sep)); key == "" {
return ErrKeyIsEmpty
}
defer c.fireHook(OnSetValue)
if strings.IndexByte(key, sep) == -1 {
c.data[key] = val
return
}
// disable set by path.
if len(setByPath) > 0 && !setByPath[0] {
c.data[key] = val
return
}
// set by path
keys := strings.Split(key, string(sep))
return maputil.SetByKeys(&c.data, keys, val)
}