From 931817a9c049b02ce3b3b4abe8fa3b0af8791584 Mon Sep 17 00:00:00 2001 From: bl17zar Date: Sat, 28 Dec 2019 14:22:21 +0300 Subject: [PATCH] added msgpack plugin decoder --- examples/plugins/msgpack/main.go | 48 ++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 3 files changed, 51 insertions(+) create mode 100644 examples/plugins/msgpack/main.go diff --git a/examples/plugins/msgpack/main.go b/examples/plugins/msgpack/main.go new file mode 100644 index 0000000..44f8854 --- /dev/null +++ b/examples/plugins/msgpack/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "encoding/json" + "github.com/vmihailenco/msgpack" +) + +// Msgpack is an example of how to create a plugin +// to decode msgpack encoded kafka messages. +// To compile: +// go build -buildmode=plugin -o msgpack.so msgpack.go +// Then start kcli like: +// kcli -d ./msgpack.so +type Msgpack struct{} + +// Decode is required in order to be a plugin +func (m Msgpack) Decode(topic string, b []byte) ([]byte, error) { + + // accepts any topic + if topic == "" { + return b, nil + } + + // schema free container + // for result + var out map[string]interface{} + + // fill container with decoded values + err := msgpack.Unmarshal(b, &out) + if err != nil { + return nil, err + } + + // represent result as json + // for beautiful output with kcli + repr, err := json.Marshal(out) + if err != nil { + return b, nil + } + + return repr, nil +} + +// Decoder is the symbol that kcli will search for when +// using this as a plugin. +var Decoder Msgpack + +func main() {} diff --git a/go.mod b/go.mod index 3c345d5..b24d86f 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( github.com/pierrec/lz4 v1.0.2-0.20171218195038-2fcda4cb7018 github.com/pierrec/xxHash v0.1.1 github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a + github.com/vmihailenco/msgpack v4.0.4+incompatible golang.org/x/crypto v0.0.0-20190404164418-38d8ce5564a5 golang.org/x/net v0.0.0-20191007182048-72f939374954 golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e diff --git a/go.sum b/go.sum index 7e486e1..92c57d5 100644 --- a/go.sum +++ b/go.sum @@ -57,6 +57,8 @@ github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqn github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= +github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I= github.com/xdg/stringprep v1.0.0/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y= golang.org/x/crypto v0.0.0-20180119165957-a66000089151/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=