mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-22 06:44:27 +03:00
ee619b2dbd
Squashed commit of the following: commit a1bd3c249be043108c84a902d2e88bf80946d444 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Apr 4 14:59:37 2024 +0300 all: upd more commit 9e55bbb02c2af2064aa2a2ca7b49fd28b544a02c Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Apr 4 14:12:45 2024 +0300 all: upd go code
52 lines
1 KiB
Go
52 lines
1 KiB
Go
package configmigrate
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
type (
|
|
// yarr is the convenience alias for YAML array.
|
|
yarr = []any
|
|
|
|
// yobj is the convenience alias for YAML key-value object.
|
|
yobj = map[string]any
|
|
)
|
|
|
|
// fieldVal returns the value of type T for key from obj. Use [any] if the
|
|
// field's type doesn't matter.
|
|
func fieldVal[T any](obj yobj, key string) (v T, ok bool, err error) {
|
|
val, ok := obj[key]
|
|
if !ok {
|
|
return v, false, nil
|
|
}
|
|
|
|
if val == nil {
|
|
return v, true, nil
|
|
}
|
|
|
|
v, ok = val.(T)
|
|
if !ok {
|
|
return v, false, fmt.Errorf("unexpected type of %q: %T", key, val)
|
|
}
|
|
|
|
return v, true, nil
|
|
}
|
|
|
|
// moveVal copies the value for srcKey from src into dst for dstKey and deletes
|
|
// it from src.
|
|
func moveVal[T any](src, dst yobj, srcKey, dstKey string) (err error) {
|
|
newVal, ok, err := fieldVal[T](src, srcKey)
|
|
if !ok {
|
|
return err
|
|
}
|
|
|
|
dst[dstKey] = newVal
|
|
delete(src, srcKey)
|
|
|
|
return nil
|
|
}
|
|
|
|
// moveSameVal moves the value for key from src into dst.
|
|
func moveSameVal[T any](src, dst yobj, key string) (err error) {
|
|
return moveVal[T](src, dst, key, key)
|
|
}
|