filter.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package qs
  2. import (
  3. "errors"
  4. "net/http"
  5. "net/url"
  6. "regexp"
  7. "strconv"
  8. )
  9. // Query error.
  10. var (
  11. ErrInvalidFilter = errors.New("invalid filter")
  12. ErrTooManyFilters = errors.New("too many filters")
  13. )
  14. var filterRegexp = regexp.MustCompile("^([A-z0-9]+) (eq|neq|gt|gte|lt|lte|in|not in|like|not like) (.+)$")
  15. // Filter represents a filter as used in, most likely, a database query.
  16. type Filter struct {
  17. Field string `json:"field"` // Field to filter on.
  18. Operator string `json:"operator"` // Filter operator, e.g. eq, gt...
  19. Value string `json:"value"` // Value to filter by.
  20. }
  21. // BoolValue retrieves the filter value as a bool.
  22. func (filter Filter) BoolValue() (bool, error) {
  23. return strconv.ParseBool(filter.Value)
  24. }
  25. // Float32Value retrieves the filter value as a float32.
  26. func (filter Filter) Float32Value() (float32, error) {
  27. value, err := strconv.ParseFloat(filter.Value, 32)
  28. return float32(value), err
  29. }
  30. // Float64Value retrieves the filter value as a float64.
  31. func (filter Filter) Float64Value() (float64, error) {
  32. return strconv.ParseFloat(filter.Value, 64)
  33. }
  34. // IntValue retrieves the filter value as an int.
  35. func (filter Filter) IntValue() (int, error) {
  36. return strconv.Atoi(filter.Value)
  37. }
  38. // Filters is a slice of Filter structs.
  39. type Filters []Filter
  40. // Field returns a new Filters slice containing only filters for the specified field.
  41. // The original order of filters is preserved.
  42. func (filters Filters) Field(field string) Filters {
  43. ff := Filters{}
  44. for _, filter := range filters {
  45. if filter.Field == field {
  46. ff = append(ff, filter)
  47. }
  48. }
  49. return ff
  50. }
  51. // Fields returns a new Filters slice containing filters for any of the specified fields.
  52. // The original order of filters is preserved.
  53. func (filters Filters) Fields(fields ...string) Filters {
  54. ff := Filters{}
  55. for _, filter := range filters {
  56. for _, field := range fields {
  57. if filter.Field == field {
  58. ff = append(ff, filter)
  59. }
  60. }
  61. }
  62. return ff
  63. }
  64. // HasField returns true if the Filters slice includes any filters for the specified field.
  65. func (filters Filters) HasField(field string) bool {
  66. for _, filter := range filters {
  67. if filter.Field == field {
  68. return true
  69. }
  70. }
  71. return false
  72. }
  73. // ReadFiltersOptions configures the behaviour of ReadFilters.
  74. type ReadFiltersOptions struct {
  75. Key string // Query string key. The default value is "filter"
  76. MaxFilters int // If this is > 0, a maximum number of filters is imposed
  77. }
  78. // ReadFilters parses URL values into a slice of filters.
  79. // This function returns nil if no filters are found.
  80. func ReadFilters(values url.Values, opt *ReadFiltersOptions) (Filters, error) {
  81. opt = initFiltersOptions(opt)
  82. if !values.Has(opt.Key) {
  83. return nil, nil
  84. }
  85. if opt.MaxFilters > 0 && len(values[opt.Key]) > opt.MaxFilters {
  86. return nil, ErrTooManyFilters
  87. }
  88. filters := Filters{}
  89. for _, filterStr := range values[opt.Key] {
  90. match := filterRegexp.FindStringSubmatch(filterStr)
  91. if match == nil {
  92. return nil, ErrInvalidFilter
  93. }
  94. filter := Filter{
  95. Field: match[1],
  96. Operator: match[2],
  97. Value: match[3],
  98. }
  99. filters = append(filters, filter)
  100. }
  101. return filters, nil
  102. }
  103. // ReadRequestFilters parses a request's query string into a slice of filters.
  104. // This function returns nil if no filters are found.
  105. func ReadRequestFilters(req *http.Request, opt *ReadFiltersOptions) (Filters, error) {
  106. return ReadFilters(req.URL.Query(), opt)
  107. }
  108. // ReadStringFilters parses a query string literal into a slice of filters.
  109. // This function returns nil if no filters are found.
  110. func ReadStringFilters(qs string, opt *ReadFiltersOptions) (Filters, error) {
  111. values, err := url.ParseQuery(qs)
  112. if err != nil {
  113. return nil, err
  114. }
  115. return ReadFilters(values, opt)
  116. }
  117. func initFiltersOptions(opt *ReadFiltersOptions) *ReadFiltersOptions {
  118. def := &ReadFiltersOptions{
  119. Key: "filter",
  120. }
  121. if opt != nil {
  122. if len(opt.Key) > 0 {
  123. def.Key = opt.Key
  124. }
  125. if opt.MaxFilters > def.MaxFilters {
  126. def.MaxFilters = opt.MaxFilters
  127. }
  128. }
  129. return def
  130. }