Skip to content

Commit

Permalink
feat: add string renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
lspgn committed Jan 14, 2024
1 parent 00ae118 commit 74ba25e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions producer/proto/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package protoproducer

import (
"bytes"
"encoding/hex"
"fmt"
"hash"
"hash/fnv"
Expand Down Expand Up @@ -121,7 +120,8 @@ func (m *ProtoProducerMessage) mapUnknown() map[string]interface{} {
value = v
} else if dataType == protowire.BytesType {
v, _ := protowire.ConsumeString(data)
value = hex.EncodeToString([]byte(v))
//value = hex.EncodeToString([]byte(v)) // removed, this conversion is left to the renderer
value = []byte(v)
} else {
continue
}
Expand Down
12 changes: 8 additions & 4 deletions producer/proto/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

"google.golang.org/protobuf/encoding/protowire"

"github.com/stretchr/testify/assert"
)

func TestMarshalJSON(t *testing.T) {
Expand All @@ -30,15 +32,15 @@ func TestMarshalJSON(t *testing.T) {
102: ProtobufFormatterConfig{
Name: "test3",
Index: 102,
Type: "string",
Type: "bytes",
Array: false,
},
},
render: map[string]RenderFunc{
"Etype": EtypeRenderer,
"test1": EtypeRenderer,
"test2": NilRenderer,
//"test3": nil,
"test3": StringRenderer,
},
}

Expand All @@ -54,10 +56,12 @@ func TestMarshalJSON(t *testing.T) {
unk = protowire.AppendString(unk, string("testing"))

unk = protowire.AppendTag(unk, protowire.Number(102), protowire.BytesType)
unk = protowire.AppendString(unk, string("testing"))
unk = protowire.AppendString(unk, string([]byte{0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67}))

fmr.SetUnknown(unk)

out, err := m.MarshalJSON()
t.Log(string(out), err)
assert.Nil(t, err)
t.Log(string(out))
assert.Equal(t, "{\"etype\":\"IPv6\",\"test1\":\"IPv6\",\"test2\":\"74657374696e67\",\"test3\":\"testing\"}", string(out))
}
11 changes: 11 additions & 0 deletions producer/proto/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
RendererNetwork RendererID = "network"
RendererDateTime RendererID = "datetime"
RendererDateTimeNano RendererID = "datetimenano"
RendererString RendererID = "string"
)

var (
Expand All @@ -33,6 +34,7 @@ var (
RendererProto: ProtoRenderer,
RendererDateTime: DateTimeRenderer,
RendererDateTimeNano: DateTimeNanoRenderer,
RendererString: StringRenderer,
}

defaultRenderers = map[string]RenderFunc{
Expand Down Expand Up @@ -95,6 +97,15 @@ func NilRenderer(msg *ProtoProducerMessage, fieldName string, data interface{})
return data
}

func StringRenderer(msg *ProtoProducerMessage, fieldName string, data interface{}) interface{} {
if dataC, ok := data.([]byte); ok {
return string(dataC)
} else if dataC, ok := data.(string); ok {
return string(dataC)
} // maybe should support uint64?
return NilRenderer(msg, fieldName, data)
}

func DateTimeRenderer(msg *ProtoProducerMessage, fieldName string, data interface{}) interface{} {
if dataC, ok := data.(uint64); ok {
ts := time.Unix(int64(dataC), 0).UTC()
Expand Down

0 comments on commit 74ba25e

Please sign in to comment.