Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
  • v1.0.0
  • v1.0.1
  • v1.1.0
  • v1.10.0
  • v1.11.0
  • v1.11.1
  • v1.11.2
  • v1.11.3
  • v1.11.4
  • v1.12.0
  • v1.13.0
  • v1.14.0
  • v1.15.0
  • v1.16.0
  • v1.17.1
  • v1.17.2
  • v1.17.3
  • v1.17.4
  • v1.17.5
  • v1.17.6
  • v1.17.7
  • v1.18.0
  • v1.18.1
  • v1.18.2
  • v1.18.3
  • v1.19.0
  • v1.2.0
  • v1.20.0
  • v1.20.1
  • v1.20.2
  • v1.20.3
  • v1.20.4
  • v1.20.5
  • v1.21.0
  • v1.22.0
  • v1.22.1
  • v1.22.3
  • v1.22.4
  • v1.22.5
  • v1.22.6
  • v1.22.7
  • v1.22.8
  • v1.22.9
  • v1.3.0
  • v1.4.0
  • v1.4.1
  • v1.4.2
  • v1.4.3
  • v1.5.0
  • v1.6.0
  • v1.7.0
  • v1.7.1
  • v1.8.0
  • v1.9.0
55 results

Target

Select target project
  • oss/libraries/go/application/configuration
1 result
Select Git revision
  • master
  • v1.0.0
  • v1.0.1
  • v1.1.0
  • v1.10.0
  • v1.11.0
  • v1.11.1
  • v1.11.2
  • v1.11.3
  • v1.11.4
  • v1.12.0
  • v1.13.0
  • v1.14.0
  • v1.15.0
  • v1.16.0
  • v1.17.1
  • v1.17.2
  • v1.17.3
  • v1.17.4
  • v1.17.5
  • v1.17.6
  • v1.17.7
  • v1.18.0
  • v1.18.1
  • v1.18.2
  • v1.18.3
  • v1.19.0
  • v1.2.0
  • v1.20.0
  • v1.20.1
  • v1.20.2
  • v1.20.3
  • v1.20.4
  • v1.20.5
  • v1.21.0
  • v1.22.0
  • v1.22.1
  • v1.22.3
  • v1.22.4
  • v1.22.5
  • v1.22.6
  • v1.22.7
  • v1.22.8
  • v1.22.9
  • v1.3.0
  • v1.4.0
  • v1.4.1
  • v1.4.2
  • v1.4.3
  • v1.5.0
  • v1.6.0
  • v1.7.0
  • v1.7.1
  • v1.8.0
  • v1.9.0
55 results
Show changes
Commits on Source (3)
<a name="v1.9.0"></a>
## [v1.9.0] - 2022-10-17
### Add Features
- feat add HasOnChangeHook and RemoveOnChangeHook
<a name="v1.8.0"></a> <a name="v1.8.0"></a>
## [v1.8.0] - 2022-10-17 ## [v1.8.0] - 2022-10-17
### Add Features ### Add Features
...@@ -84,6 +90,7 @@ ...@@ -84,6 +90,7 @@
<a name="v1.0.0"></a> <a name="v1.0.0"></a>
## v1.0.0 - 2022-09-18 ## v1.0.0 - 2022-09-18
[v1.9.0]: https://gitlab.schukai.com/oss/libraries/go/application/configuration/compare/v1.8.0...v1.9.0
[v1.8.0]: https://gitlab.schukai.com/oss/libraries/go/application/configuration/compare/v1.7.1...v1.8.0 [v1.8.0]: https://gitlab.schukai.com/oss/libraries/go/application/configuration/compare/v1.7.1...v1.8.0
[v1.7.1]: https://gitlab.schukai.com/oss/libraries/go/application/configuration/compare/v1.7.0...v1.7.1 [v1.7.1]: https://gitlab.schukai.com/oss/libraries/go/application/configuration/compare/v1.7.0...v1.7.1
[v1.7.0]: https://gitlab.schukai.com/oss/libraries/go/application/configuration/compare/v1.6.0...v1.7.0 [v1.7.0]: https://gitlab.schukai.com/oss/libraries/go/application/configuration/compare/v1.6.0...v1.7.0
......
...@@ -303,6 +303,14 @@ import ( ...@@ -303,6 +303,14 @@ import (
"gitlab.schukai.com/oss/libraries/go/application/configuration" "gitlab.schukai.com/oss/libraries/go/application/configuration"
) )
type ChangeEventHandler struct {
callback func(event configuration.ChangeEvent)
}
func (c *ChangeEventHandler) Handle(event configuration.ChangeEvent) {
c.callback(event)
}
func main() { func main() {
config := struct { config := struct {
Host string Host string
...@@ -314,12 +322,17 @@ func main() { ...@@ -314,12 +322,17 @@ func main() {
closeChan := make(chan bool) closeChan := make(chan bool)
s.OnChange(func(event configuration.ChangeEvent) { var h configuration.EventHook
h = &ChangeEventHandler{
callback: func(event configuration.ChangeEvent) {
log := event.Changlog log := event.Changlog
msg := fmt.Sprintf("Change from %s to %s", log[0].From, log[0].To) msg = fmt.Sprintf("Change from %s to %s", log[0].From, log[0].To)
fmt.Println(msg) fmt.Println(msg)
closeChan <- true closeChan <- true
}) },
}
s.OnChange(h)
c := s.Config() c := s.Config()
c.Host = "www.example.com" c.Host = "www.example.com"
......
...@@ -11,16 +11,40 @@ type ChangeEvent struct { ...@@ -11,16 +11,40 @@ type ChangeEvent struct {
Changlog diff.Changelog Changlog diff.Changelog
} }
type EventHook func(event ChangeEvent) type EventHook interface {
Handle(event ChangeEvent)
}
// OnChange registers a hook that is called when the configuration changes.
func (s *Settings[C]) OnChange(hook EventHook) *Settings[C] { func (s *Settings[C]) OnChange(hook EventHook) *Settings[C] {
s.hooks.change = append(s.hooks.change, hook) s.hooks.change = append(s.hooks.change, hook)
return s return s
} }
// HasOnChangeHook returns true if there are registered hooks.
func (s *Settings[C]) HasOnChangeHook(hook EventHook) *Settings[C] {
for _, h := range s.hooks.change {
if h == hook {
break
}
}
return s
}
// RemoveOnChangeHook removes a change hook from the list of hooks.
func (s *Settings[C]) RemoveOnChangeHook(hook EventHook) *Settings[C] {
for i, h := range s.hooks.change {
if h == hook {
s.hooks.change = append(s.hooks.change[:i], s.hooks.change[i+1:]...)
break
}
}
return s
}
func (s *Settings[C]) notifyChangeHooks(changelog diff.Changelog) *Settings[C] { func (s *Settings[C]) notifyChangeHooks(changelog diff.Changelog) *Settings[C] {
for _, h := range s.hooks.change { for _, h := range s.hooks.change {
h(ChangeEvent{Changlog: changelog}) h.Handle(ChangeEvent{Changlog: changelog})
} }
return s return s
} }
......
...@@ -11,6 +11,49 @@ import ( ...@@ -11,6 +11,49 @@ import (
"time" "time"
) )
type mockTestEventHandler struct {
EventHook
}
func (m *mockTestEventHandler) Handle(event ChangeEvent) {
// do nothing
}
func TestAddRemoveHook(t *testing.T) {
config := struct {
Host string
}{
Host: "localhost",
}
s := New(config)
var h EventHook
h = &mockTestEventHandler{}
s.OnChange(h)
if len(s.hooks.change) != 1 {
t.Error("Expected 1 got ", len(s.hooks.change))
}
s.RemoveOnChangeHook(h)
if len(s.hooks.change) != 0 {
t.Error("Expected 0 got ", len(s.hooks.change))
}
}
type ChangeEventTester struct {
callback func(event ChangeEvent)
}
func (c *ChangeEventTester) Handle(event ChangeEvent) {
c.callback(event)
}
func TestReadmeExample(t *testing.T) { func TestReadmeExample(t *testing.T) {
config := struct { config := struct {
...@@ -24,13 +67,19 @@ func TestReadmeExample(t *testing.T) { ...@@ -24,13 +67,19 @@ func TestReadmeExample(t *testing.T) {
closeChan := make(chan bool) closeChan := make(chan bool)
msg := "" msg := ""
s.OnChange(func(event ChangeEvent) {
log := event.Changlog var h EventHook
msg = fmt.Sprintf("Change from %s to %s", log[0].From, log[0].To) h = &ChangeEventTester{
// for Readme callback: func(event ChangeEvent) {
//fmt.Println(msg) log := event.Changlog
closeChan <- true msg = fmt.Sprintf("Change from %s to %s", log[0].From, log[0].To)
}) // for Readme
//fmt.Println(msg)
closeChan <- true
},
}
s.OnChange(h)
c := s.Config() c := s.Config()
c.Host = "www.example.com" c.Host = "www.example.com"
...@@ -84,10 +133,16 @@ func TestCangeOnChange(t *testing.T) { ...@@ -84,10 +133,16 @@ func TestCangeOnChange(t *testing.T) {
closeChan := make(chan bool) closeChan := make(chan bool)
counter := 0 counter := 0
s.OnChange(func(event ChangeEvent) {
counter++ var h EventHook
closeChan <- true h = &ChangeEventTester{
}) callback: func(event ChangeEvent) {
counter++
closeChan <- true
},
}
s.OnChange(h)
c.A = "b" c.A = "b"
s.SetConfig(c) s.SetConfig(c)
......
...@@ -271,10 +271,16 @@ func TestConfigurationServePostJson(t *testing.T) { ...@@ -271,10 +271,16 @@ func TestConfigurationServePostJson(t *testing.T) {
closeChan := make(chan bool) closeChan := make(chan bool)
counter := 0 counter := 0
s.OnChange(func(event ChangeEvent) {
counter++ var h EventHook
closeChan <- true h = &ChangeEventTester{
}) callback: func(event ChangeEvent) {
counter++
closeChan <- true
},
}
s.OnChange(h)
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response. // We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
......
{"version":"1.8.0"} {"version":"1.9.0"}
...@@ -35,11 +35,16 @@ func TestWatch(t *testing.T) { ...@@ -35,11 +35,16 @@ func TestWatch(t *testing.T) {
signal := make(chan bool) signal := make(chan bool)
c.OnChange(func(event ChangeEvent) { var h EventHook
assert.Equal(t, event.Changlog[0].From, "localhost") h = &ChangeEventTester{
assert.Equal(t, event.Changlog[0].To, "example.org") callback: func(event ChangeEvent) {
signal <- true assert.Equal(t, event.Changlog[0].From, "localhost")
}) assert.Equal(t, event.Changlog[0].To, "example.org")
signal <- true
},
}
c.OnChange(h)
c.Watch() c.Watch()
......