-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifiers.go
56 lines (51 loc) · 1.25 KB
/
modifiers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package gosl
import (
"reflect"
)
// ModifyByValue modify an unknown key in the given map[string]any by it value.
// Supports nested maps, but only if their type is map[string]any.
//
// Example:
//
// package main
//
// import (
// "fmt"
//
// "github.com/koddr/gosl"
// )
//
// func main() {
// m := map[string]any{"order": map[string]any{"total_cost": 100}}
// foundValue := 100
// newValue := 250
//
// isFound, result := gosl.ModifyByValue(m, foundValue, newValue)
//
// fmt.Println(isFound, result)
// }
func ModifyByValue(m map[string]any, foundValue, newValue any) (foundKey bool, results map[string]any) {
// Check, if the given map is empty.
if m == nil {
return foundKey, nil
}
// Loop for all keys in the given map.
for key, value := range m {
// Check map by reflect.
if reflect.DeepEqual(value, foundValue) {
// Modify a key of the given map.
m[key] = newValue
foundKey = true // switch the temp variable
} else if mv, ok := value.(map[string]any); ok {
// Run recurrent function.
isFound, modified := ModifyByValue(mv, foundValue, newValue)
// Check, if key is found.
if isFound {
// Modify a key of the given map.
m[key] = modified
foundKey = true // switch the temp variable
}
}
}
return foundKey, m
}