You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by mm...@apache.org on 2019/05/14 16:33:45 UTC

[pulsar-client-go] 08/38: Added serialize method for string map

This is an automated email from the ASF dual-hosted git repository.

mmerli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-client-go.git

commit 0fe868ab5bff52ffc33430f74490beee8d608934
Author: Matteo Merli <mm...@apache.org>
AuthorDate: Wed Apr 3 07:50:08 2019 -0700

    Added serialize method for string map
---
 pulsar/impl/commands.go      | 25 +++++++++++++++++++++++++
 pulsar/impl/commands_test.go | 26 ++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/pulsar/impl/commands.go b/pulsar/impl/commands.go
index c803602..ed9f6b9 100644
--- a/pulsar/impl/commands.go
+++ b/pulsar/impl/commands.go
@@ -32,3 +32,28 @@ func baseCommand(cmdType pb.BaseCommand_Type, msg proto.Message) *pb.BaseCommand
 	return cmd
 }
 
+func ConvertFromStringMap(m map[string]string) []*pb.KeyValue {
+	list := make([]*pb.KeyValue, len(m))
+
+	i := 0
+	for k, v := range m {
+		list[i] = &pb.KeyValue{
+			Key:   proto.String(k),
+			Value: proto.String(v),
+		}
+
+		i += 1
+	}
+
+	return list
+}
+
+func ConvertToStringMap(pbb []*pb.KeyValue) map[string]string {
+	m := make(map[string]string)
+
+	for _, kv := range pbb {
+		m[*kv.Key] = *kv.Value
+	}
+
+	return m
+}
diff --git a/pulsar/impl/commands_test.go b/pulsar/impl/commands_test.go
new file mode 100644
index 0000000..d9f94e6
--- /dev/null
+++ b/pulsar/impl/commands_test.go
@@ -0,0 +1,26 @@
+package impl
+
+import (
+	"github.com/stretchr/testify/assert"
+	"testing"
+)
+
+func TestConvertStringMap(t *testing.T) {
+	m := make(map[string]string)
+	m["a"] = "1"
+	m["b"] = "2"
+
+	pbm := ConvertFromStringMap(m)
+
+	assert.Equal(t, 2, len(pbm))
+	assert.Equal(t, "a", *pbm[0].Key)
+	assert.Equal(t, "1", *pbm[0].Value)
+	assert.Equal(t, "b", *pbm[1].Key)
+	assert.Equal(t, "2", *pbm[1].Value)
+
+	m2 := ConvertToStringMap(pbm)
+	assert.Equal(t, 2, len(m2))
+	assert.Equal(t, "1", m2["a"])
+	assert.Equal(t, "2", m2["b"])
+}
+