From 3fcf22baa48969d8f51ba875e18a608fae7c7f06 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Sat, 29 Aug 2015 13:18:32 +0300 Subject: [PATCH] Add callbacks draft --- confection.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/confection.go b/confection.go index 3d40025..cb3b7cc 100644 --- a/confection.go +++ b/confection.go @@ -8,7 +8,8 @@ import ( var ( // config stores application config. - config interface{} + config interface{} + callbacks = make(map[string][]func(oldVal, newVal interface{})) ) // Manage accepts a pointer to a configuration struct. @@ -20,6 +21,12 @@ func Manage(target interface{}) { 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) { dupe := duplicate(config) if err := unmarshal(body, dupe); err != nil { @@ -27,9 +34,16 @@ func update(body []byte) { return } + defer triggerCallbacks(config, dupe) + + // Setting new config config = dupe } +func triggerCallbacks(oldConf, newConf interface{}) { + return +} + func isStructPtr(target interface{}) bool { if val := reflect.ValueOf(target); val.Kind() == reflect.Ptr { if val = reflect.Indirect(val); val.Kind() == reflect.Struct {