Skip to content

Commit

Permalink
Merge pull request YaoApp#237 from yflau/main
Browse files Browse the repository at this point in the history
url param: support with limit
  • Loading branch information
trheyi authored Dec 10, 2024
2 parents 7bba5ea + c1c3a4b commit abbf144
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
23 changes: 23 additions & 0 deletions model/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package model
import (
"net/url"
"regexp"
"strconv"
"strings"

jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/kun/log"
)

const reURLWhereStr = "(where|orwhere|wherein|orwherein)\\.(.+)\\.(eq|gt|lt|ge|le|like|match|in|null|notnull)"
Expand Down Expand Up @@ -61,6 +63,9 @@ func URLToQueryParam(values url.Values) QueryParam {
} else if strings.HasSuffix(name, ".select") {
param.setWithSelect(name, values.Get(name))
continue
} else if strings.HasSuffix(name, ".limit") {
param.setWithLimit(name, values.Get(name))
continue
}
}

Expand Down Expand Up @@ -207,3 +212,21 @@ func (param *QueryParam) setWithSelect(name string, value string) {
with.Query.Select = selects
param.Withs[withName] = with
}

func (param *QueryParam) setWithLimit(name string, value string) {
namer := strings.Split(name, ".")
withName := namer[0]
if _, has := param.Withs[withName]; !has {
param.setWith(withName)
}

limit, err := strconv.ParseInt(value, 10, 64)
if err != nil {
log.Error("got invalid limit value: %v", value)
return
}

with := param.Withs[withName]
with.Query.Limit = int(limit)
param.Withs[withName] = with
}
23 changes: 23 additions & 0 deletions types/query.url.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package types
import (
"net/url"
"regexp"
"strconv"
"strings"

jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/kun/log"
)

const reURLWhereStr = "(where|orwhere|wherein|orwherein)\\.(.+)\\.(eq|gt|lt|ge|le|like|match|in|null|notnull)[\\[]*[\\]]*"
Expand Down Expand Up @@ -61,6 +63,9 @@ func URLToQueryParam(values url.Values) QueryParam {
} else if strings.HasSuffix(name, ".select") {
param.setWithSelect(name, values.Get(name))
continue
} else if strings.HasSuffix(name, ".limit") {
param.setWithLimit(name, values.Get(name))
continue
}
}

Expand Down Expand Up @@ -207,3 +212,21 @@ func (param *QueryParam) setWithSelect(name string, value string) {
with.Query.Select = selects
param.Withs[withName] = with
}

func (param *QueryParam) setWithLimit(name string, value string) {
namer := strings.Split(name, ".")
withName := namer[0]
if _, has := param.Withs[withName]; !has {
param.setWith(withName)
}

limit, err := strconv.ParseInt(value, 10, 64)
if err != nil {
log.Error("got invalid limit value: %v", value)
return
}

with := param.Withs[withName]
with.Query.Limit = int(limit)
param.Withs[withName] = with
}

0 comments on commit abbf144

Please sign in to comment.