-
Notifications
You must be signed in to change notification settings - Fork 255
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
优化 pandora key convert #621
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
优化 pandora key convert
- Loading branch information
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -752,31 +752,74 @@ func GetMapList(data string) map[string]string { | |
return ret | ||
} | ||
|
||
func PandoraKey(key string) string { | ||
var nk string | ||
for _, c := range key { | ||
// 判断时只有数字和字母为合法字符,规则: | ||
// 1. 首字符为数字时,增加首字符 "K" | ||
// 2. 首字符为非法字符时,去掉首字符(例如,如果字符串全为非法字符,则转换后为空) | ||
// 3. 非首字符并且为非法字符时,使用 "_" 替代非法字符 | ||
func PandoraKey(key string) (string, bool) { | ||
// check | ||
valid := true | ||
size := 0 | ||
for idx, c := range key { | ||
if c >= '0' && c <= '9' { | ||
if len(nk) == 0 { | ||
nk = "K" | ||
size++ | ||
if idx == 0 { | ||
size++ | ||
valid = false | ||
} | ||
nk = nk + string(c) | ||
} else if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') { | ||
nk = nk + string(c) | ||
} else if len(nk) > 0 { | ||
nk = nk + "_" | ||
continue | ||
} | ||
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') { | ||
size++ | ||
continue | ||
} | ||
|
||
if idx > 0 && size > 0 { | ||
size++ | ||
} | ||
valid = false | ||
} | ||
return nk | ||
if valid { | ||
return key, true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里是true的话不要反回key There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 性能还能提升 |
||
} | ||
|
||
if size <= 0 { | ||
return "", false | ||
} | ||
|
||
// set | ||
bytes := make([]byte, size) | ||
bp := 0 | ||
for idx, c := range key { | ||
if c >= '0' && c <= '9' { | ||
if idx == 0 { | ||
bp += copy(bytes, "K") | ||
} | ||
bp += copy(bytes[bp:], string(c)) | ||
continue | ||
} | ||
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') { | ||
bp += copy(bytes[bp:], string(c)) | ||
continue | ||
} | ||
|
||
if idx > 0 { | ||
bp += copy(bytes[bp:], "_") | ||
} | ||
} | ||
return string(bytes), false | ||
} | ||
|
||
func DeepConvertKey(data map[string]interface{}) map[string]interface{} { | ||
newData := make(map[string]interface{}) | ||
for k, v := range data { | ||
nk := PandoraKey(k) | ||
nk, valid := PandoraKey(k) | ||
if nv, ok := v.(map[string]interface{}); ok { | ||
v = DeepConvertKey(nv) | ||
} | ||
newData[nk] = v | ||
if !valid { | ||
delete(data, k) | ||
data[nk] = v | ||
} | ||
} | ||
return newData | ||
return data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里要判断是否为true