From d4ec68d29e052c4f9900710cf6f071432d471184 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Sat, 29 Aug 2015 15:29:56 +0300 Subject: [PATCH] Implement callbacks --- confection.go | 8 ++++++++ confection_test.go | 1 + field_test.go | 16 ++++++++-------- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/confection.go b/confection.go index feff6c9..b141ed8 100644 --- a/confection.go +++ b/confection.go @@ -103,6 +103,14 @@ func triggerCallbacks(oldConf, newConf interface{}) { return } + for fname, d := range diff(oldConf, newConf) { + if cbs, ok := callbacks[fname]; ok { + for _, cb := range cbs { + cb(d[0], d[1]) + } + } + } + return } diff --git a/confection_test.go b/confection_test.go index 3ff61a9..85092d9 100644 --- a/confection_test.go +++ b/confection_test.go @@ -10,6 +10,7 @@ type testConf struct { Version float32 `json:"version"` Database testDatabaseConf `json:"database"` } + type testDatabaseConf struct { Adapter string `json:"adapter"` Host string `json:"host"` diff --git a/field_test.go b/field_test.go index bc45c1b..d5290e7 100644 --- a/field_test.go +++ b/field_test.go @@ -16,16 +16,16 @@ func TestExtractFields(t *testing.T) { } fields := extractFields(c, "") - testField := func(fieldName, kind string, val interface{}) { - if f, ok := fields[fieldName]; ok { + testField := func(fname, kind string, val interface{}) { + if f, ok := fields[fname]; ok { if f.Kind != kind { - t.Errorf("%s expected to be of kind %q, got %q", fieldName, kind, f.Kind) + t.Errorf("%s expected to be of kind %q, got %q", fname, kind, f.Kind) } if f.Val != val { - t.Errorf("%s expected to have value %q, got %q", fieldName, val, f.Val) + t.Errorf("%s expected to have value %q, got %q", fname, val, f.Val) } } else { - t.Errorf("Missing %s field", fieldName) + t.Errorf("Missing %s field", fname) } } @@ -58,8 +58,8 @@ func TestDiff(t *testing.T) { } d := diff(c1, c2) - testField := func(fieldName string, oldVal, newVal interface{}) { - if f, ok := d[fieldName]; ok { + testField := func(fname string, oldVal, newVal interface{}) { + if f, ok := d[fname]; ok { if f[0] != oldVal { t.Errorf("%s field old value was %q, not %q", oldVal, f[0]) } @@ -67,7 +67,7 @@ func TestDiff(t *testing.T) { t.Errorf("%s field new value was %q, not %q", newVal, f[1]) } } else { - t.Errorf("Expected %s field to have different values", fieldName) + t.Errorf("Expected %s field to have different values", fname) } }