refactor resources API

Signed-off-by: hongming <talonwan@yunify.com>
This commit is contained in:
hongming
2020-01-09 15:16:05 +08:00
parent 1f5defb044
commit 05e949103e
40 changed files with 469 additions and 1276 deletions

View File

@@ -30,22 +30,26 @@ const (
OrderByParam = "orderBy"
ConditionsParam = "conditions"
ReverseParam = "reverse"
NameParam = "name"
defaultLimit = 10
defaultOffset = 0
)
func ParsePaging(paging string) (limit, offset int) {
limit = 10
offset = 0
func ParsePaging(req *restful.Request) (limit, offset int) {
paging := req.QueryParameter(PagingParam)
if groups := regexp.MustCompile(`^limit=(-?\d+),page=(\d+)$`).FindStringSubmatch(paging); len(groups) == 3 {
limit, _ = strconv.Atoi(groups[1])
page, _ := strconv.Atoi(groups[2])
offset = (page - 1) * limit
} else {
limit = defaultLimit
offset = defaultOffset
}
return
}
func ParseConditions(conditionsStr string) (*Conditions, error) {
func ParseConditions(req *restful.Request) (*Conditions, error) {
conditionsStr := req.QueryParameter(ConditionsParam)
conditions := &Conditions{Match: make(map[string]string, 0), Fuzzy: make(map[string]string, 0)}
@@ -76,16 +80,23 @@ func ParseConditions(conditionsStr string) (*Conditions, error) {
return conditions, nil
}
func ParseReverse(req *restful.Request) bool {
reverse := req.QueryParameter(ReverseParam)
b, err := strconv.ParseBool(reverse)
if err != nil {
return false
}
return b
}
type Conditions struct {
Match map[string]string
Fuzzy map[string]string
}
func GetBoolValueWithDefault(req *restful.Request, name string, dv bool) bool {
reverse := req.QueryParameter(name)
if v, err := strconv.ParseBool(reverse); err == nil {
return v
}
return dv
}
func GetStringValueWithDefault(req *restful.Request, name string, dv string) string {
v := req.QueryParameter(name)
if v == "" {
v = dv
}
return v
}