-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
config.go
116 lines (92 loc) · 2.37 KB
/
config.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
package resolver
import (
"encoding/json"
"fmt"
"reflect"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/core/config"
"github.com/smartcontractkit/chainlink/core/config/envvar"
)
type ConfigItemResolver struct {
key string
cfg ConfigItemValue
}
func NewConfigItem(key string, value ConfigItemValue) *ConfigItemResolver {
return &ConfigItemResolver{key: key, cfg: value}
}
func (r *ConfigItemResolver) Key() string {
return r.key
}
func (r *ConfigItemResolver) Value() ConfigItemValue {
return r.cfg
}
type ConfigItemValue struct {
Value interface{} `json:"value"`
}
func (ConfigItemValue) ImplementsGraphQLType(name string) bool {
return name == "ConfigItemValue"
}
func (c *ConfigItemValue) UnmarshalGraphQL(input interface{}) error {
switch t := input.(type) {
case int32:
c.Value = t
return nil
case string:
c.Value = t
return nil
case []string:
c.Value = t
return nil
default:
return errors.New("wrong type")
}
}
func (c ConfigItemValue) MarshalJSON() ([]byte, error) {
return json.Marshal(c.Value)
}
type ConfigResolver struct {
cfg config.EnvPrinter
}
func NewConfig(cfg config.EnvPrinter) *ConfigResolver {
return &ConfigResolver{cfg: cfg}
}
func (r *ConfigResolver) Items() []*ConfigItemResolver {
var cfgs []*ConfigItemResolver
schemaT := reflect.TypeOf(envvar.ConfigSchema{})
t := reflect.TypeOf(r.cfg)
v := reflect.ValueOf(r.cfg)
for i := 0; i < t.NumField(); i++ {
item := t.Field(i)
// Using the same logic as the renderer does to render the configuration values
schemaItem, ok := schemaT.FieldByName(item.Name)
if !ok {
continue
}
envName, ok := schemaItem.Tag.Lookup("env")
if !ok {
continue
}
field := v.FieldByIndex(item.Index)
var cfg *ConfigItemResolver
if stringer, ok := field.Interface().(fmt.Stringer); ok {
if stringer != reflect.Zero(reflect.TypeOf(stringer)).Interface() {
cfg = NewConfigItem(envName, ConfigItemValue{Value: stringer.String()})
}
} else {
cfg = NewConfigItem(envName, ConfigItemValue{Value: fmt.Sprintf("%v", field)})
}
if cfg != nil {
cfgs = append(cfgs, cfg)
}
}
return cfgs
}
type ConfigPayloadResolver struct {
cfg config.EnvPrinter
}
func NewConfigPayload(cfg config.EnvPrinter) *ConfigPayloadResolver {
return &ConfigPayloadResolver{cfg: cfg}
}
func (r *ConfigPayloadResolver) Items() []*ConfigItemResolver {
return NewConfig(r.cfg).Items()
}