made it use dotquote.AppendMap(). added more tests.

master
Charles Iliya Krempeaux 2016-09-22 07:51:43 -07:00
parent 444461dffa
commit 4024ee0241
2 changed files with 65 additions and 43 deletions

View File

@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"io" "io"
"sort"
"time" "time"
) )
@ -21,31 +20,7 @@ func NewDefaultWritingRouter(writer io.Writer) *DefaultWritingRouter {
func NewDefaultWritingRouterWithPrefix(writer io.Writer, prefix map[string]interface{}) *DefaultWritingRouter { func NewDefaultWritingRouterWithPrefix(writer io.Writer, prefix map[string]interface{}) *DefaultWritingRouter {
var prefixBuffer []byte var prefixBuffer []byte
if 0 < len(prefix) { if 0 < len(prefix) {
//@TODO: This is a potential heavy operation. Is there a better way prefixBuffer = dotquote.AppendMap(prefixBuffer, prefix)
// 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{ router := DefaultWritingRouter{
@ -107,24 +82,12 @@ func (router *DefaultWritingRouter) Route(message string, context map[string]int
//@TODO: This is a potential heavy operation. Is there a better way //@TODO: This is a potential heavy operation. Is there a better way
// to get the ultimate result this is trying to archive? // to get the ultimate result this is trying to archive?
// //
sortedKeys := make([]string, len(context)) if 0 < len(context) {
i := 0
for key, _ := range context {
sortedKeys[i] = key
i++
}
sort.Strings(sortedKeys)
for _, key := range sortedKeys {
value := context[key]
p = append(p, ' ') p = append(p, ' ')
p = dotquote.AppendString(p, fmt.Sprintf("%T", value), "ctx", key, "type") p = dotquote.AppendMap(p, context, "ctx")
p = append(p, ' ')
p = dotquote.AppendString(p, fmt.Sprintf("%v", value), "ctx", key, "value")
} }
_,_ = oi.LongWrite(router.writer, p) _,_ = oi.LongWrite(router.writer, p)
//@TODO: Should this be checking for errors from oi.LongWrite()? //@TODO: Should this be checking for errors from oi.LongWrite()?

View File

@ -28,11 +28,70 @@ func TestDefaultWritingRouterRoute(t *testing.T) {
}, },
ExpectContains: []string{ ExpectContains: []string{
`"text"="Hello world!"`, `"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"`, ` "ctx"."apple"="one" "ctx"."banana"="2" "ctx"."cherry"="3.300000" "ctx"."kiwi"="true"`,
` "error"."type"="*errors.errorString" "error"."text"="test error" `, ` "error"."type"="*errors.errorString" "error"."text"="test error" `,
` "when"="`, ` "when"="`,
}, },
}, },
{
Message: "Apple\tBANANA\nCherry",
Context: map[string]interface{}{
"apple": "one",
"banana": 2,
"cherry": 3.3,
"kiwi": true,
"~error": errors.New("test error"),
"more": map[string]interface{}{
"ONE": "1",
"TWO": "2",
"THREE": "3",
},
},
ExpectContains: []string{
`"text"="Apple\tBANANA\nCherry"`,
` "ctx"."apple"="one" "ctx"."banana"="2" "ctx"."cherry"="3.300000" "ctx"."kiwi"="true"`,
` "error"."type"="*errors.errorString" "error"."text"="test error" `,
` "ctx"."more"."ONE"="1" "ctx"."more"."THREE"="3" "ctx"."more"."TWO"="2"`,
` "when"="`,
},
},
{
Message: "Apple\tBANANA\nCherry",
Context: map[string]interface{}{
"apple": "one",
"banana": 2,
"cherry": 3.3,
"kiwi": true,
"~error": errors.New("test error"),
"more": map[string]interface{}{
"ONE": "1",
"TWO": "2",
"THREE": "3",
"FOUR": map[string]interface{}{
"a": "1st",
"b": "2nd",
"c": []string{
"th",
"i",
"rd",
},
},
},
},
ExpectContains: []string{
`"text"="Apple\tBANANA\nCherry"`,
` "ctx"."apple"="one" "ctx"."banana"="2" "ctx"."cherry"="3.300000" "ctx"."kiwi"="true"`,
` "error"."type"="*errors.errorString" "error"."text"="test error" `,
` "ctx"."more"."FOUR"."a"="1st" "ctx"."more"."FOUR"."b"="2nd" "ctx"."more"."FOUR"."c"=["th","i","rd"] "ctx"."more"."ONE"="1" "ctx"."more"."THREE"="3" "ctx"."more"."TWO"="2"`,
` "when"="`,
},
},
} }
@ -90,7 +149,7 @@ func TestDefaultWritingRouterWithPrefixRoute(t *testing.T) {
ExpectContains: []string{ ExpectContains: []string{
`"name"="backendapi" "number"="123"`, `"name"="backendapi" "number"="123"`,
`"text"="Hello world!"`, `"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"`, ` "ctx"."apple"="one" "ctx"."banana"="2" "ctx"."cherry"="3.300000" "ctx"."kiwi"="true"`,
` "error"."type"="*errors.errorString" "error"."text"="test error" `, ` "error"."type"="*errors.errorString" "error"."text"="test error" `,
` "when"="`, ` "when"="`,
}, },