Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize: add global descriptor release api #247

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions thrift_reflection/descriptor_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ func RegisterAST(ast *parser.Thrift) (*GlobalDescriptor, *FileDescriptor) {
return gd, fd
}

// ReleaseGlobalDescriptor release the global descriptor
func ReleaseGlobalDescriptor(gdUUID string) {
lock.Lock()
delete(globalDescriptorMap, gdUUID)
defer lock.Unlock()
}

func generateShortUUID() string {
uuid := make([]byte, 4)
_, _ = rand.Read(uuid)
Expand Down
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
Loading