Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Configuration
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Libraries
Go
Application
Configuration
Commits
e478250f
Verified
Commit
e478250f
authored
2 years ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
feat add HasOnChangeHook and RemoveOnChangeHook
parent
be8418e6
No related branches found
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
README.md
+16
-3
16 additions, 3 deletions
README.md
change.go
+26
-2
26 additions, 2 deletions
change.go
change_test.go
+66
-11
66 additions, 11 deletions
change_test.go
http-handler_test.go
+10
-4
10 additions, 4 deletions
http-handler_test.go
watch_test.go
+10
-5
10 additions, 5 deletions
watch_test.go
with
128 additions
and
25 deletions
README.md
+
16
−
3
View file @
e478250f
...
@@ -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"
...
...
This diff is collapsed.
Click to expand it.
change.go
+
26
−
2
View file @
e478250f
...
@@ -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
}
}
...
...
This diff is collapsed.
Click to expand it.
change_test.go
+
66
−
11
View file @
e478250f
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
http-handler_test.go
+
10
−
4
View file @
e478250f
...
@@ -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
()
...
...
This diff is collapsed.
Click to expand it.
watch_test.go
+
10
−
5
View file @
e478250f
...
@@ -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
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment