support prefix for default router

master
Charles Iliya Krempeaux 2016-09-21 16:30:35 -07:00
parent 0c315d3937
commit 444461dffa
2 changed files with 103 additions and 1 deletions

View File

@ -12,10 +12,45 @@ import (
)
// DefaultWritingRouter returns an initialized DefaultWritingRouter
// NewDefaultWritingRouter returns an initialized DefaultWritingRouter
func NewDefaultWritingRouter(writer io.Writer) *DefaultWritingRouter {
return NewDefaultWritingRouterWithPrefix(writer, nil)
}
// NewDefaultWritingRouterWithPrefix returns an initialized DefaultWritingRouter
func NewDefaultWritingRouterWithPrefix(writer io.Writer, prefix map[string]interface{}) *DefaultWritingRouter {
var prefixBuffer []byte
if 0 < len(prefix) {
//@TODO: This is a potential heavy operation. Is there a better way
// to get the ultimate result this is trying to archive?
//
sortedKeys := make([]string, len(prefix))
i := 0
for key, _ := range prefix {
sortedKeys[i] = key
i++
}
sort.Strings(sortedKeys)
for _, key := range sortedKeys {
value := prefix[key]
if s, ok := value.(string); !ok {
prefixBuffer = dotquote.AppendString(prefixBuffer, fmt.Sprintf("%T", value), key, "type")
prefixBuffer = append(prefixBuffer, ' ')
prefixBuffer = dotquote.AppendString(prefixBuffer, fmt.Sprintf("%v", value), key, "value")
prefixBuffer = append(prefixBuffer, ' ')
} else {
prefixBuffer = dotquote.AppendString(prefixBuffer, s, key)
prefixBuffer = append(prefixBuffer, ' ')
}
}
}
router := DefaultWritingRouter{
writer:writer,
prefix:prefixBuffer,
}
return &router
@ -28,6 +63,7 @@ func NewDefaultWritingRouter(writer io.Writer) *DefaultWritingRouter {
// deployment enviornment.
type DefaultWritingRouter struct {
writer io.Writer
prefix []byte
}
@ -48,6 +84,10 @@ func (router *DefaultWritingRouter) Route(message string, context map[string]int
var buffer [512]byte
p := buffer[:0]
if prefix := router.prefix; 0 < len(prefix) {
p = append(p, prefix...)
}
p = dotquote.AppendString(p, message, "text")
p = append(p, ' ')

View File

@ -64,3 +64,65 @@ func TestDefaultWritingRouterRoute(t *testing.T) {
}
}
}
func TestDefaultWritingRouterWithPrefixRoute(t *testing.T) {
tests := []struct{
Message string
Context map[string]interface{}
Prefix map[string]interface{}
ExpectContains []string
}{
{
Message: "Hello world!",
Context: map[string]interface{}{
"apple": "one",
"banana": 2,
"cherry": 3.3,
"kiwi": true,
"~error": errors.New("test error"),
},
Prefix: map[string]interface{}{
"name": "backendapi",
"number": "123",
},
ExpectContains: []string{
`"name"="backendapi" "number"="123"`,
`"text"="Hello world!"`,
` "ctx"."apple"."type"="string" "ctx"."apple"."value"="one" "ctx"."banana"."type"="int" "ctx"."banana"."value"="2" "ctx"."cherry"."type"="float64" "ctx"."cherry"."value"="3.3" "ctx"."kiwi"."type"="bool" "ctx"."kiwi"."value"="true"`,
` "error"."type"="*errors.errorString" "error"."text"="test error" `,
` "when"="`,
},
},
}
for testNumber, test := range tests {
var buffer bytes.Buffer
var router Router = NewDefaultWritingRouterWithPrefix(&buffer, test.Prefix)
ctx := map[string]interface{}{}
for n, v := range test.Context {
ctx[n] = v
}
if err := router.Route(test.Message, ctx); nil != err {
t.Errorf("For test #%d, did not expect an error, but actually got one: (%T) %v", testNumber, err, err)
continue
}
{
actual := buffer.String()
for expectedNumber, expectContains := range test.ExpectContains {
if !strings.Contains(actual, expectContains) {
t.Errorf("For test #%d and expected #%d, expect to contain, actual:\n==)>%s<(==\n==)>%s<(==", testNumber, expectedNumber, expectContains, actual)
continue
}
}
}
}
}