Add callbacks draft
This commit is contained in:
parent
8bf455246d
commit
3fcf22baa4
|
@ -8,7 +8,8 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// config stores application config.
|
// config stores application config.
|
||||||
config interface{}
|
config interface{}
|
||||||
|
callbacks = make(map[string][]func(oldVal, newVal interface{}))
|
||||||
)
|
)
|
||||||
|
|
||||||
// Manage accepts a pointer to a configuration struct.
|
// Manage accepts a pointer to a configuration struct.
|
||||||
|
@ -20,6 +21,12 @@ func Manage(target interface{}) {
|
||||||
config = target
|
config = target
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnChange adds a callback function that is triggered every time a value of
|
||||||
|
// a field changes.
|
||||||
|
func OnChange(field string, fun func(oldVal, newVal interface{})) {
|
||||||
|
callbacks[field] = append(callbacks[field], fun)
|
||||||
|
}
|
||||||
|
|
||||||
func update(body []byte) {
|
func update(body []byte) {
|
||||||
dupe := duplicate(config)
|
dupe := duplicate(config)
|
||||||
if err := unmarshal(body, dupe); err != nil {
|
if err := unmarshal(body, dupe); err != nil {
|
||||||
|
@ -27,9 +34,16 @@ func update(body []byte) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer triggerCallbacks(config, dupe)
|
||||||
|
|
||||||
|
// Setting new config
|
||||||
config = dupe
|
config = dupe
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func triggerCallbacks(oldConf, newConf interface{}) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func isStructPtr(target interface{}) bool {
|
func isStructPtr(target interface{}) bool {
|
||||||
if val := reflect.ValueOf(target); val.Kind() == reflect.Ptr {
|
if val := reflect.ValueOf(target); val.Kind() == reflect.Ptr {
|
||||||
if val = reflect.Indirect(val); val.Kind() == reflect.Struct {
|
if val = reflect.Indirect(val); val.Kind() == reflect.Struct {
|
||||||
|
|
Loading…
Reference in New Issue