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

gosonzhang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-inlong.git


The following commit(s) were added to refs/heads/master by this push:
     new 46188e8  [INLONG-1769][TubeMQ]Fix bug in util function `SplitToMap` in 
Go SDK (#1770)
46188e8 is described below

commit 46188e8dfd05396113946b87af26b2118ad03113
Author: Zijie Lu <[email protected]>
AuthorDate: Tue Nov 9 10:35:10 2021 +0800

    [INLONG-1769][TubeMQ]Fix bug in util function `SplitToMap` in Go SDK (#1770)
    
    Signed-off-by: Zijie Lu <[email protected]>
---
 .../tubemq-client-go/util/util.go                  | 37 ++++++-------------
 .../tubemq-client-go/util/util_test.go             | 41 ++++++++++++++++++++++
 2 files changed, 52 insertions(+), 26 deletions(-)

diff --git a/inlong-tubemq/tubemq-client-twins/tubemq-client-go/util/util.go 
b/inlong-tubemq/tubemq-client-twins/tubemq-client-go/util/util.go
index 8dd78fa..36c907d 100644
--- a/inlong-tubemq/tubemq-client-twins/tubemq-client-go/util/util.go
+++ b/inlong-tubemq/tubemq-client-twins/tubemq-client-go/util/util.go
@@ -94,39 +94,24 @@ func ParseConfirmContext(confirmContext string) (string, 
int64, error) {
 }
 
 // SplitToMap split the given string by the two step strings to map.
+// Source format 
$msgType$=metadata_journal_log,$msgTime$=202111081911,tdbusip=10.56.15.232
 func SplitToMap(source string, step1 string, step2 string) map[string]string {
-       pos1 := 0
-       pos2 := strings.Index(source, step1)
-       pos3 := 0
        m := make(map[string]string)
-       for pos2 != -1 {
-               itemStr := strings.TrimSpace(source[pos1 : pos2-pos1])
-               if len(itemStr) == 0 {
-                       continue
-               }
-               pos1 = pos2 + len(step1)
-               pos2 = strings.Index(source[pos1:], step1)
-               pos3 = strings.Index(itemStr, step2)
-               if pos3 == -1 {
+       if len(source) == 0 {
+               return m
+       }
+
+       str := strings.Split(source, step1)
+       for _, ss := range str {
+               s := strings.Split(ss, step2)
+               if len(s) == 1 {
                        continue
                }
-               key := strings.TrimSpace(itemStr[:pos3])
-               val := strings.TrimSpace(itemStr[pos3+len(step2):])
+               key := strings.TrimSpace(s[0])
                if len(key) == 0 {
                        continue
                }
-               m[key] = val
-       }
-       if pos1 != len(source) {
-               itemStr := strings.TrimSpace(source[pos1:])
-               pos3 = strings.Index(itemStr, step2)
-               if pos3 != -1 {
-                       key := strings.TrimSpace(itemStr[:pos3])
-                       val := strings.TrimSpace(itemStr[pos3+len(step2):])
-                       if len(key) > 0 {
-                               m[key] = val
-                       }
-               }
+               m[key] = strings.TrimSpace(s[1])
        }
        return m
 }
diff --git 
a/inlong-tubemq/tubemq-client-twins/tubemq-client-go/util/util_test.go 
b/inlong-tubemq/tubemq-client-twins/tubemq-client-go/util/util_test.go
new file mode 100644
index 0000000..071ea27
--- /dev/null
+++ b/inlong-tubemq/tubemq-client-twins/tubemq-client-go/util/util_test.go
@@ -0,0 +1,41 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package util
+
+import (
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+)
+
+func TestSplitToMap(t *testing.T) {
+       source := 
"$msgType$=metadata_journal_log,$msgTime$=202111081911,tdbusip=10.56.15.232"
+       m := SplitToMap(source, ",", "=")
+       properties := make(map[string]string)
+       properties["$msgType$"] = "metadata_journal_log"
+       properties["$msgTime$"] = "202111081911"
+       properties["tdbusip"] = "10.56.15.232"
+       assert.Equal(t, properties, m)
+
+       source = "key=val&key1&key2="
+       m = SplitToMap(source, "&", "=")
+       properties = make(map[string]string)
+       properties["key"] = "val"
+       properties["key2"] = ""
+       assert.Equal(t, properties, m)
+}

Reply via email to