Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
ff6a2d01
Commit
ff6a2d01
authored
Dec 01, 2014
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored command/query interface
parent
246f41b8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
15 deletions
+21
-15
pkg/bus/bus.go
+15
-9
pkg/bus/bus_test.go
+6
-6
No files found.
pkg/bus/bus.go
View file @
ff6a2d01
...
...
@@ -9,19 +9,26 @@ import (
type
QueryHandler
interface
{}
type
Query
interface
{}
var
(
type
Bus
interface
{
SendQuery
(
query
Query
)
error
AddQueryHandler
(
handler
QueryHandler
)
}
type
InProcBus
struct
{
handlerIndex
map
[
string
]
QueryHandler
)
}
func
InitBus
()
{
handlerIndex
=
make
(
map
[
string
]
QueryHandler
)
func
New
()
Bus
{
bus
:=
&
InProcBus
{}
bus
.
handlerIndex
=
make
(
map
[
string
]
QueryHandler
)
return
bus
}
func
SendQuery
(
query
interface
{}
)
error
{
func
(
b
*
InProcBus
)
SendQuery
(
query
Query
)
error
{
var
queryName
=
reflect
.
TypeOf
(
query
)
.
Elem
()
.
Name
()
fmt
.
Printf
(
"sending query for type: %v
\n
"
,
queryName
)
var
handler
=
handlerIndex
[
queryName
]
var
handler
=
b
.
handlerIndex
[
queryName
]
if
handler
==
nil
{
return
errors
.
New
(
"handler not found"
)
...
...
@@ -38,10 +45,9 @@ func SendQuery(query interface{}) error {
}
}
func
AddQueryHandler
(
handler
QueryHandler
)
{
func
(
b
*
InProcBus
)
AddQueryHandler
(
handler
QueryHandler
)
{
handlerType
:=
reflect
.
TypeOf
(
handler
)
queryTypeName
:=
handlerType
.
In
(
0
)
.
Elem
()
.
Name
()
fmt
.
Printf
(
"QueryType %v
\n
"
,
queryTypeName
)
handlerIndex
[
queryTypeName
]
=
handler
//fmt.Printf("Adding handler for type: %v\n", queryTypeName)
b
.
handlerIndex
[
queryTypeName
]
=
handler
}
pkg/bus/bus_test.go
View file @
ff6a2d01
...
...
@@ -11,13 +11,13 @@ type TestQuery struct {
}
func
TestHandlerReturnsError
(
t
*
testing
.
T
)
{
InitBus
()
bus
:=
New
()
AddQueryHandler
(
func
(
query
*
TestQuery
)
error
{
bus
.
AddQueryHandler
(
func
(
query
*
TestQuery
)
error
{
return
errors
.
New
(
"handler error"
)
})
err
:=
SendQuery
(
&
TestQuery
{})
err
:=
bus
.
SendQuery
(
&
TestQuery
{})
if
err
==
nil
{
t
.
Fatal
(
"Send query failed %v"
,
err
)
...
...
@@ -27,15 +27,15 @@ func TestHandlerReturnsError(t *testing.T) {
}
func
TestHandlerReturn
(
t
*
testing
.
T
)
{
InitBus
()
bus
:=
New
()
AddQueryHandler
(
func
(
q
*
TestQuery
)
error
{
bus
.
AddQueryHandler
(
func
(
q
*
TestQuery
)
error
{
q
.
Resp
=
"hello from handler"
return
nil
})
query
:=
&
TestQuery
{}
err
:=
SendQuery
(
query
)
err
:=
bus
.
SendQuery
(
query
)
if
err
!=
nil
{
t
.
Fatal
(
"Send query failed %v"
,
err
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment