Skip to content
Snippets Groups Projects
Verified Commit 474b968c authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat: new postprocessing #2

parent 43baeb5a
No related branches found
No related tags found
No related merge requests found
// Copyright 2022 schukai GmbH
// SPDX-License-Identifier: AGPL-3.0
package configuration
import (
"github.com/r3labs/diff/v3"
)
type PostprocessingEvent struct {
Changelog diff.Changelog
}
type PostprocessingHook interface {
Handle(event PostprocessingEvent)
}
// OnPostprocessing registers a hook that is called when the configuration changes.
func (s *Settings[C]) OnPostprocessing(hook PostprocessingHook) *Settings[C] {
s.hooks.postprocessing = append(s.hooks.postprocessing, hook)
return s
}
// HasOnPostprocessingHook returns true if there are registered hooks.
func (s *Settings[C]) HasOnPostprocessingHook(hook PostprocessingHook) *Settings[C] {
for _, h := range s.hooks.postprocessing {
if h == hook {
break
}
}
return s
}
// RemoveOnPostprocessingHook removes a change hook from the list of hooks.
func (s *Settings[C]) RemoveOnPostprocessingHook(hook PostprocessingHook) *Settings[C] {
for i, h := range s.hooks.postprocessing {
if h == hook {
s.hooks.postprocessing = append(s.hooks.postprocessing[:i], s.hooks.postprocessing[i+1:]...)
break
}
}
return s
}
func (s *Settings[C]) notifyPostprocessingHooks(changelog diff.Changelog) *Settings[C] {
for _, h := range s.hooks.postprocessing {
h.Handle(PostprocessingEvent{Changelog: changelog})
}
return s
}
// Copyright 2022 schukai GmbH
// SPDX-License-Identifier: AGPL-3.0
package configuration
import (
"bytes"
"io"
"testing"
)
type mockTestPostprocessingEventHandler struct {
ChangeHook
}
func (m *mockTestPostprocessingEventHandler) Handle(event PostprocessingEvent) {
// do nothing
}
func TestAddRemovePostprocessingHook(t *testing.T) {
config := struct {
Host string
}{
Host: "localhost",
}
s := New(config)
var h PostprocessingHook
h = &mockTestPostprocessingEventHandler{}
s.OnPostprocessing(h)
if len(s.hooks.postprocessing) != 1 {
t.Error("Expected 1 got ", len(s.hooks.postprocessing))
}
s.RemoveOnPostprocessingHook(h)
if len(s.hooks.postprocessing) != 0 {
t.Error("Expected 0 got ", len(s.hooks.postprocessing))
}
}
func TestPostprocessingOnChange(t *testing.T) {
defaults := ConfigStruct2{
A: "",
B: false,
C: nil,
D: nil,
F: 0,
G: "",
}
s := New(defaults)
s.SetMnemonic("test")
var buf bytes.Buffer
buf.WriteString("A: a\n")
r := (io.Reader)(&buf)
s.AddReader(r, Yaml)
s.Import()
if s.HasErrors() {
t.Error("Expected not error", s.Errors())
}
c := s.Config()
if c.A != "a" {
t.Error("Expected \"a\" got ", c)
}
counter := 0
var p PostprocessingHook
p = &PostprocessingHandler{
Callback: func(event PostprocessingEvent) {
counter++
},
}
s.OnPostprocessing(p)
c.A = "x"
s.SetConfig(c)
if counter != 1 {
t.Error("Expected 1 got ", counter)
}
}
......@@ -4,10 +4,11 @@
package configuration
import (
"github.com/fsnotify/fsnotify"
"reflect"
"strconv"
"sync"
"github.com/fsnotify/fsnotify"
)
type fileWatch struct {
......@@ -30,6 +31,7 @@ type Settings[C any] struct {
hooks struct {
change []ChangeHook
error []ErrorHook
postprocessing []PostprocessingHook
}
fileWatch fileWatch
......
......@@ -10,35 +10,13 @@ type reader struct {
reader io.Reader
}
//type writer struct {
// format Format
// reader io.Writer
//}
type streamBackend struct {
readers []reader
//writers []writer
}
//type StreamOption struct {
//}
//func (s *Settings[C]) AddStream(stream io.ReadWriter, format Format) *Settings[C] {
// return s.
// AddReader(stream, format)
// //AddWriter(stream, format)
//}
func (s *Settings[C]) AddReader(r io.Reader, format Format) *Settings[C] {
s.Lock()
defer s.Unlock()
s.stream.readers = append(s.stream.readers, reader{format, r})
return s
}
//func (s *Settings[C]) AddWriter(w io.Writer, format Format) *Settings[C] {
// s.Lock()
// defer s.Unlock()
// s.stream.writers = append(s.stream.writers, writer{format, w})
// return s
//}
......@@ -73,8 +73,8 @@ func TestMultiChange(t *testing.T) {
var h ChangeHook
h = &ChangeEventHandler{
Callback: func(event ChangeEvent) {
result = append(result, event.Changlog[0].To.(string))
signal <- event.Changlog[0].To.(string)
result = append(result, event.Changelog[0].To.(string))
signal <- event.Changelog[0].To.(string)
},
}
......@@ -167,8 +167,8 @@ func TestWatch(t *testing.T) {
var h ChangeHook
h = &ChangeEventHandler{
Callback: func(event ChangeEvent) {
assert.Equal(t, event.Changlog[0].From, "localhost")
assert.Equal(t, event.Changlog[0].To, "example.org")
assert.Equal(t, event.Changelog[0].From, "localhost")
assert.Equal(t, event.Changelog[0].To, "example.org")
signal <- true
},
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment