From 77ef25e2cc718fb5adece62fa0a4dc8d4ff050e2 Mon Sep 17 00:00:00 2001 From: Li2CO3 Date: Fri, 20 Dec 2024 18:02:57 +0800 Subject: [PATCH] fix: fix option parser (#246) (#248) --- utils/string_utils.go | 24 ++++++++++++++++++++++-- utils/utils_test.go | 24 ++++++++++++++++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/utils/string_utils.go b/utils/string_utils.go index 2303334f..3843f204 100644 --- a/utils/string_utils.go +++ b/utils/string_utils.go @@ -29,6 +29,8 @@ func ParseArr(str string) ([]string, error) { newstr = strings.ReplaceAll(newstr, " ]", "]") newstr = strings.ReplaceAll(newstr, "[ ", "[") newstr = strings.ReplaceAll(newstr, " ", " ") + // 特殊处理 ', 不进行转义分析 + newstr = strings.ReplaceAll(newstr, "'", "@&@") newstr = strings.TrimSpace(newstr) if len(newstr) == len(str) { break @@ -75,6 +77,10 @@ func ParseArr(str string) ([]string, error) { kend = i key = str[kstart:kend] kstart = i + 1 + key = strings.TrimSpace(key) + key = strings.TrimPrefix(key, `"`) + key = strings.TrimSuffix(key, `"`) + key = strings.ReplaceAll(key, `@&@`, `'`) result = append(result, key) } continue @@ -86,6 +92,10 @@ func ParseArr(str string) ([]string, error) { return nil, errors.New("grammar error for:" + str) } key = str[kstart:kend] + key = strings.TrimSpace(key) + key = strings.TrimPrefix(key, `"`) + key = strings.TrimSuffix(key, `"`) + key = strings.ReplaceAll(key, `@&@`, `'`) result = append(result, key) return result, nil } else { @@ -109,6 +119,8 @@ func ParseKV(str string) (map[string]string, error) { newstr = strings.ReplaceAll(newstr, ": ", ":") newstr = strings.ReplaceAll(newstr, " ,", ",") newstr = strings.ReplaceAll(newstr, " ", " ") + // 特殊处理 ', 不进行转义分析 + newstr = strings.ReplaceAll(newstr, "'", "@&@") newstr = strings.TrimSpace(newstr) if len(newstr) == len(str) { break @@ -171,7 +183,11 @@ func ParseKV(str string) (map[string]string, error) { if strings.HasSuffix(value, ",") { value = strings.TrimSuffix(value, ",") } - result[strings.TrimSpace(key)] = strings.TrimSpace(value) + value = strings.TrimSpace(value) + value = strings.TrimPrefix(value, `"`) + value = strings.TrimSuffix(value, `"`) + value = strings.ReplaceAll(value, `@&@`, `'`) + result[strings.TrimSpace(key)] = value } continue } @@ -189,7 +205,11 @@ func ParseKV(str string) (map[string]string, error) { if strings.HasSuffix(value, ",") { value = strings.TrimSuffix(value, ",") } - result[strings.TrimSpace(key)] = strings.TrimSpace(value) + value = strings.TrimSpace(value) + value = strings.TrimPrefix(value, `"`) + value = strings.TrimSuffix(value, `"`) + value = strings.ReplaceAll(value, `@&@`, `'`) + result[strings.TrimSpace(key)] = value return result, nil } else { if dq && sq { diff --git a/utils/utils_test.go b/utils/utils_test.go index 33d2d061..66582361 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -75,6 +75,14 @@ func TestParseKV(t *testing.T) { input = "{\n valuemap:{k1:v1 k2:v2 k3:}\n valuelist:[a,b,c,d]\n valueset:[{email:e1},{email:e2}]\n valuelistset:[[a,b,c],[d,e,f]]\n valuelistsetstruct:[[{email:e1},{email:e2}],[{email:e3},{email:e4}]]\n valuemapStruct:[k1:{email:e1} k2:{email:e2}]\n }" kv, err = ParseKV(input) assert(t, err == nil) + + // quote test + input = `{k1:"Hes Value" k2:normal, k3:"He's fine"}` + kv, err = ParseKV(input) + assert(t, err == nil && len(kv) == 3) + assert(t, kv["k1"] == `Hes Value`) + assert(t, kv["k2"] == "normal") + assert(t, kv["k3"] == `He's fine`) } func TestParseArr(t *testing.T) { @@ -89,12 +97,12 @@ func TestParseArr(t *testing.T) { arr, err = ParseArr(input) assert(t, err == nil && len(arr) == 3) assert(t, arr[0] == "a") - assert(t, arr[1] == "\"b\"") + assert(t, arr[1] == "b") assert(t, arr[2] == "c") - input = "[a,'b,c]" - arr, err = ParseArr(input) - assert(t, err != nil) + // input = "[a,'b,c]" + // arr, err = ParseArr(input) + // assert(t, err != nil) input = "[a,[b,c],c]" arr, err = ParseArr(input) @@ -118,6 +126,14 @@ func TestParseArr(t *testing.T) { assert(t, arr[1] == "[b,{c,d}]") assert(t, arr[2] == "c") assert(t, arr[3] == "{e,f}") + + input = "[a's,b,c,\"d's ok\"]" + arr, err = ParseArr(input) + assert(t, err == nil && len(arr) == 4) + assert(t, arr[0] == "a's") + assert(t, arr[1] == "b") + assert(t, arr[2] == "c") + assert(t, arr[3] == "d's ok") } func assert(t *testing.T, cond bool, val ...interface{}) {