made it use dotquote.AppendMap(). added more tests.
parent
444461dffa
commit
4024ee0241
|
@ -7,7 +7,6 @@ import (
|
|||
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -21,31 +20,7 @@ func NewDefaultWritingRouter(writer io.Writer) *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, ' ')
|
||||
}
|
||||
}
|
||||
prefixBuffer = dotquote.AppendMap(prefixBuffer, prefix)
|
||||
}
|
||||
|
||||
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
|
||||
// to get the ultimate result this is trying to archive?
|
||||
//
|
||||
sortedKeys := make([]string, len(context))
|
||||
i := 0
|
||||
for key, _ := range context {
|
||||
sortedKeys[i] = key
|
||||
i++
|
||||
}
|
||||
sort.Strings(sortedKeys)
|
||||
|
||||
for _, key := range sortedKeys {
|
||||
|
||||
value := context[key]
|
||||
|
||||
if 0 < len(context) {
|
||||
p = append(p, ' ')
|
||||
p = dotquote.AppendString(p, fmt.Sprintf("%T", value), "ctx", key, "type")
|
||||
p = append(p, ' ')
|
||||
p = dotquote.AppendString(p, fmt.Sprintf("%v", value), "ctx", key, "value")
|
||||
p = dotquote.AppendMap(p, context, "ctx")
|
||||
}
|
||||
|
||||
|
||||
_,_ = oi.LongWrite(router.writer, p)
|
||||
|
||||
//@TODO: Should this be checking for errors from oi.LongWrite()?
|
||||
|
|
|
@ -28,11 +28,70 @@ func TestDefaultWritingRouterRoute(t *testing.T) {
|
|||
},
|
||||
ExpectContains: []string{
|
||||
`"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" `,
|
||||
` "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{
|
||||
`"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"`,
|
||||
` "ctx"."apple"="one" "ctx"."banana"="2" "ctx"."cherry"="3.300000" "ctx"."kiwi"="true"`,
|
||||
` "error"."type"="*errors.errorString" "error"."text"="test error" `,
|
||||
` "when"="`,
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue