types.go 2.4 KB
Newer Older
Z
zryfish 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
package query

import (
	"github.com/emicklei/go-restful"
	"strconv"
)

const (
	ParameterName          = "name"
	ParameterLabelSelector = "labelSelector"
	ParameterFieldSelector = "fieldSelector"
	ParameterPage          = "page"
	ParameterLimit         = "limit"
	ParameterOrderBy       = "sortBy"
	ParameterAscending     = "ascending"
)

// Query represents api search terms
type Query struct {
	Pagination *Pagination

	// sort result in which field, default to FieldCreationTimeStamp
	SortBy Field

	// sort result in ascending or descending order, default to descending
	Ascending bool

	//
	Filters []Filter
}

type Pagination struct {
	// items per page
	Limit int

H
hongming 已提交
36 37
	// offset
	Offset int
Z
zryfish 已提交
38 39
}

H
hongming 已提交
40
var NoPagination = newPagination(-1, 0)
Z
zryfish 已提交
41

H
hongming 已提交
42 43
// make sure that pagination is valid
func newPagination(limit int, offset int) *Pagination {
Z
zryfish 已提交
44
	return &Pagination{
H
hongming 已提交
45 46
		Limit:  limit,
		Offset: offset,
Z
zryfish 已提交
47 48 49
	}
}

H
hongming 已提交
50 51 52 53
func (p *Pagination) GetValidPagination(total int) (startIndex, endIndex int) {
	if p.Limit == NoPagination.Limit {
		return 0, total
	}
Z
zryfish 已提交
54

H
hongming 已提交
55 56 57
	if p.Limit < 0 || p.Offset < 0 {
		return 0, 0
	}
Z
zryfish 已提交
58

H
hongming 已提交
59
	startIndex = p.Limit * p.Offset
Z
zryfish 已提交
60 61 62 63 64 65 66 67 68 69 70
	endIndex = startIndex + p.Limit

	if endIndex > total {
		endIndex = total
	}

	return startIndex, endIndex
}

func New() *Query {
	return &Query{
H
hongming 已提交
71 72 73 74
		Pagination: NoPagination,
		SortBy:     "",
		Ascending:  false,
		Filters:    []Filter{},
Z
zryfish 已提交
75 76 77 78 79
	}
}

type Filter struct {
	Field Field
H
hongming 已提交
80
	Value Value
Z
zryfish 已提交
81 82 83 84 85
}

func ParseQueryParameter(request *restful.Request) *Query {
	query := New()

H
hongming 已提交
86 87
	limit, err := strconv.Atoi(request.QueryParameter("limit"))
	// equivalent to undefined, use the default value
Z
zryfish 已提交
88
	if err != nil {
H
hongming 已提交
89
		limit = -1
Z
zryfish 已提交
90
	}
H
hongming 已提交
91 92 93 94
	page, err := strconv.Atoi(request.QueryParameter("page"))
	// equivalent to undefined, use the default value
	if err != nil {
		page = 1
Z
zryfish 已提交
95 96
	}

H
hongming 已提交
97 98
	query.Pagination = newPagination(limit, page-1)

Z
zryfish 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112
	query.SortBy = Field(defaultString(request.QueryParameter("sortBy"), FieldCreationTimeStamp))

	ascending, err := strconv.ParseBool(defaultString(request.QueryParameter("ascending"), "false"))
	if err != nil {
		query.Ascending = false
	} else {
		query.Ascending = ascending
	}

	for _, field := range ComparableFields {
		f := request.QueryParameter(string(field))
		if len(f) != 0 {
			query.Filters = append(query.Filters, Filter{
				Field: field,
H
hongming 已提交
113
				Value: Value(f),
Z
zryfish 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126
			})
		}
	}

	return query
}

func defaultString(value, defaultValue string) string {
	if len(value) == 0 {
		return defaultValue
	}
	return value
}