forked from yandex/pandora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.go
124 lines (114 loc) · 3.08 KB
/
hooks.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2017 Yandex LLC. All rights reserved.
// Use of this source code is governed by a MPL 2.0
// license that can be found in the LICENSE file.
// Author: Vladimir Skipor <skipor@yandex-team.ru>
// Package pluginconfig contains integration plugin with config packages.
// Doing such integration in different package allows to config and plugin packages
// not depend on each other, and set hooks when their are really needed.
package pluginconfig
import (
"fmt"
"reflect"
"strings"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/yandex/pandora/core/config"
"github.com/yandex/pandora/core/plugin"
"github.com/yandex/pandora/lib/tag"
)
func AddHooks() {
config.AddTypeHook(Hook)
config.AddTypeHook(FactoryHook)
}
const PluginNameKey = "type"
func Hook(f reflect.Type, t reflect.Type, data interface{}) (p interface{}, err error) {
if !plugin.Lookup(t) {
return data, nil
}
name, fillConf, err := parseConf(t, data)
if err != nil {
return
}
return plugin.New(t, name, fillConf)
}
func FactoryHook(f reflect.Type, t reflect.Type, data interface{}) (p interface{}, err error) {
if !plugin.LookupFactory(t) {
return data, nil
}
name, fillConf, err := parseConf(t, data)
if err != nil {
return
}
return plugin.NewFactory(t, name, fillConf)
}
func parseConf(t reflect.Type, data interface{}) (name string, fillConf func(conf interface{}) error, err error) {
if tag.Debug {
zap.L().Debug("Parsing plugin config",
zap.Stringer("plugin", t),
zap.Reflect("conf", data),
)
}
confData, err := toStringKeyMap(data)
if err != nil {
return
}
var names []string
for key, val := range confData {
if PluginNameKey == strings.ToLower(key) {
strVal, ok := val.(string)
if !ok {
err = errors.Errorf("%s has non-string value %s", PluginNameKey, val)
return
}
names = append(names, strVal)
delete(confData, key)
}
}
if len(names) == 0 {
err = errors.Errorf("plugin %s expected", PluginNameKey)
return
}
if len(names) > 1 {
err = errors.Errorf("too many %s keys", PluginNameKey)
return
}
name = names[0]
fillConf = func(conf interface{}) error {
if tag.Debug {
zap.L().Debug("Decoding plugin",
zap.String("name", name),
zap.Stringer("type", t),
zap.Stringer("config type", reflect.TypeOf(conf).Elem()),
zap.String("config data", fmt.Sprint(confData)),
)
}
err := config.DecodeAndValidate(confData, conf)
if err != nil {
err = fmt.Errorf("%s %s plugin\n"+
"%s from %v %s",
t, name, reflect.TypeOf(conf).Elem(), confData, err)
}
return err
}
return
}
func toStringKeyMap(data interface{}) (out map[string]interface{}, err error) {
out, ok := data.(map[string]interface{})
if ok {
return
}
untypedKeyData, ok := data.(map[interface{}]interface{})
if !ok {
err = errors.Errorf("unexpected config type %T: should be map[string or interface{}]interface{}", data)
return
}
out = make(map[string]interface{}, len(untypedKeyData))
for key, val := range untypedKeyData {
strKey, ok := key.(string)
if !ok {
err = errors.Errorf("unexpected key type %T: %v", key, key)
}
out[strKey] = val
}
return
}