AdGuardHome/internal/configmigrate/yaml.go
Ainar Garipov d32832735c Pull request 2111: 6545-schema-version
Updates #6545.

Squashed commit of the following:

commit b1969128a99ff21c97feb4e7805b4b8133d7122f
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Dec 15 20:04:37 2023 +0300

    home: fix import

commit 872ccea1491a8da76cc24db79247438d0ce4d256
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Dec 15 20:01:15 2023 +0300

    all: output schema version
2023-12-15 20:27:47 +03:00

68 lines
1.4 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)
}
// coalesceError returns the first non-nil error. It is named after function
// COALESCE in SQL. If all errors are nil, it returns nil.
//
// TODO(e.burkov): Replace with [errors.Join].
//
// TODO(a.garipov): Think of ways to merge with [aghalg.Coalesce].
func coalesceError(errors ...error) (res error) {
for _, err := range errors {
if err != nil {
return err
}
}
return nil
}