forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_utils.go
66 lines (60 loc) · 1.72 KB
/
config_utils.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
57
58
59
60
61
62
63
64
65
66
package broker
import (
"fmt"
)
//------------------------------------------------------------------------------
// GetGenericType returns the type of a generically parsed config structure.
func GetGenericType(boxedConfig interface{}) string {
switch unboxed := boxedConfig.(type) {
case map[string]interface{}:
if t, ok := unboxed["type"].(string); ok {
return t
}
case map[interface{}]interface{}:
if t, ok := unboxed["type"].(string); ok {
return t
}
}
return ""
}
// RemoveGenericType removes the type of a generically parsed config structure.
func RemoveGenericType(boxedConfig interface{}) {
switch unboxed := boxedConfig.(type) {
case map[string]interface{}:
delete(unboxed, "type")
case map[interface{}]interface{}:
delete(unboxed, "type")
}
}
// ComplementGenericConfig copies fields from one generic config to another, but
// avoids overriding existing values in the destination config.
func ComplementGenericConfig(target, complement interface{}) error {
switch t := target.(type) {
case map[string]interface{}:
cMap, ok := complement.(map[string]interface{})
if !ok {
return fmt.Errorf("ditto config type mismatch: %T != %T", target, complement)
}
for k, v := range cMap {
if tv, exists := t[k]; !exists {
t[k] = v
} else {
_ = ComplementGenericConfig(tv, v)
}
}
case map[interface{}]interface{}:
cMap, ok := complement.(map[interface{}]interface{})
if !ok {
return fmt.Errorf("ditto config type mismatch: %T != %T", target, complement)
}
for k, v := range cMap {
if tv, exists := t[k]; !exists {
t[k] = v
} else {
_ = ComplementGenericConfig(tv, v)
}
}
}
return nil
}
//------------------------------------------------------------------------------