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 nested conditional #709

Merged
merged 1 commit into from
May 22, 2021
Merged
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
27 changes: 15 additions & 12 deletions core/search/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (t *Tree) next(n *node, route string, result *Result) bool {
if route[i] != slash {
continue
}

token := route[:i]
return n.forEach(func(k string, v *node) bool {
r := match(k, token)
Expand Down Expand Up @@ -163,21 +164,23 @@ func add(nd *node, route string, item interface{}) error {
}

for i := range route {
if route[i] == slash {
token := route[:i]
children := nd.getChildren(token)
if child, ok := children[token]; ok {
if child != nil {
return add(child, route[i+1:], item)
}

return errInvalidState
if route[i] != slash {
continue
}

token := route[:i]
children := nd.getChildren(token)
if child, ok := children[token]; ok {
if child != nil {
return add(child, route[i+1:], item)
}

child := newNode(nil)
children[token] = child
return add(child, route[i+1:], item)
return errInvalidState
}

child := newNode(nil)
children[token] = child
return add(child, route[i+1:], item)
}

children := nd.getChildren(route)
Expand Down
4 changes: 2 additions & 2 deletions core/stores/sqlx/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func format(query string, args ...interface{}) (string, error) {
}

var b strings.Builder
argIndex := 0

var argIndex int
bytes := len(query)

for i := 0; i < bytes; i++ {
ch := query[i]
switch ch {
Expand Down