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

Generate Model Function GetAll filters fix #813

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
17 changes: 16 additions & 1 deletion generate/g_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ import (
"fmt"
"reflect"
"strings"
"strconv"
{{timePkg}}
"github.com/beego/beego/v2/client/orm"
)
Expand Down Expand Up @@ -183,7 +184,21 @@ func GetAll{{modelName}}(query map[string]string, fields []string, sortby []stri
for k, v := range query {
// rewrite dot-notation to Object__Attribute
k = strings.Replace(k, ".", "__", -1)
qs = qs.Filter(k, v)
//value should be boolean
if v == "true" || v == "false" {
v2, _ := strconv.ParseBool(v)
qs = qs.Filter(k, v2)
//Filter contain array of values using "in" filter
} else if strings.HasSuffix(k, "__in") {
var v2 []interface{}
args := strings.Split(v, ",")
for _, v3 := range args {
v2 = append(v2, v3)
}
qs = qs.Filter(k, v2...)
} else {
qs = qs.Filter(k, v)
}
}
// order by:
var sortFields []string
Expand Down