mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 04:14:39 +03:00
Save task options (#4620)
* Support setting nested UI values * Accept partial for configureUI * Send partial UI * Save scan, generate and auto-tag options on change * Send partials in saveUI * Save library task options on change
This commit is contained in:
64
pkg/utils/map.go
Normal file
64
pkg/utils/map.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NestedMap is a map that supports nested keys.
|
||||
// It is expected that the nested maps are of type map[string]interface{}
|
||||
type NestedMap map[string]interface{}
|
||||
|
||||
func (m NestedMap) Get(key string) (interface{}, bool) {
|
||||
fields := strings.Split(key, ".")
|
||||
|
||||
current := m
|
||||
|
||||
for _, f := range fields[:len(fields)-1] {
|
||||
v, found := current[f]
|
||||
if !found {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
current, _ = v.(map[string]interface{})
|
||||
if current == nil {
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
ret, found := current[fields[len(fields)-1]]
|
||||
return ret, found
|
||||
}
|
||||
|
||||
func (m NestedMap) Set(key string, value interface{}) {
|
||||
fields := strings.Split(key, ".")
|
||||
|
||||
current := m
|
||||
|
||||
for _, f := range fields[:len(fields)-1] {
|
||||
v, ok := current[f].(map[string]interface{})
|
||||
if !ok {
|
||||
v = make(map[string]interface{})
|
||||
current[f] = v
|
||||
}
|
||||
|
||||
current = v
|
||||
}
|
||||
|
||||
current[fields[len(fields)-1]] = value
|
||||
}
|
||||
|
||||
// MergeMaps merges src into dest. If a key exists in both maps, the value from src is used.
|
||||
func MergeMaps(dest map[string]interface{}, src map[string]interface{}) {
|
||||
for k, v := range src {
|
||||
if _, ok := dest[k]; ok {
|
||||
if srcMap, ok := v.(map[string]interface{}); ok {
|
||||
if destMap, ok := dest[k].(map[string]interface{}); ok {
|
||||
MergeMaps(destMap, srcMap)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dest[k] = v
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user