Skip to content

Commit

Permalink
fix: fix option parser (#246) (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean authored Dec 20, 2024
1 parent 3cd70a7 commit 77ef25e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
24 changes: 22 additions & 2 deletions utils/string_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
24 changes: 20 additions & 4 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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{}) {
Expand Down

0 comments on commit 77ef25e

Please sign in to comment.