ParseFromQuery parses URL query parameters into a ParseResult. Returns errors for invalid params rather than silently dropping them.
(queryParams url.Values, opts *ParseOptions)
| 15 | // ParseFromQuery parses URL query parameters into a ParseResult. |
| 16 | // Returns errors for invalid params rather than silently dropping them. |
| 17 | func ParseFromQuery(queryParams url.Values, opts *ParseOptions) *ParseResult { |
| 18 | maxFilters := defaultMaxFilters |
| 19 | maxInValues := defaultMaxInValues |
| 20 | maxCharLen := defaultMaxCharLen |
| 21 | if opts != nil { |
| 22 | if opts.MaxFilters > 0 { |
| 23 | maxFilters = opts.MaxFilters |
| 24 | } |
| 25 | if opts.MaxInValues > 0 { |
| 26 | maxInValues = opts.MaxInValues |
| 27 | } |
| 28 | if opts.MaxCharLen > 0 { |
| 29 | maxCharLen = opts.MaxCharLen |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | result := &ParseResult{ |
| 34 | Filters: &QueryFilterSet{ |
| 35 | Filters: make([]QueryFilter, 0), |
| 36 | LogicalOperator: LogicalAnd, |
| 37 | }, |
| 38 | Errors: make([]ParseError, 0), |
| 39 | } |
| 40 | |
| 41 | // Parse top-level logical operator for combining filters. |
| 42 | logicalOperatorRaw := strings.TrimSpace(queryParams.Get("logical_operator")) |
| 43 | if logicalOperatorRaw != "" { |
| 44 | logicalOperator := ResolveLogicalOperator(logicalOperatorRaw) |
| 45 | if logicalOperator == "" { |
| 46 | result.Errors = append(result.Errors, ParseError{ |
| 47 | Param: "logical_operator", |
| 48 | Message: "invalid logical_operator: must be 'and' or 'or'", |
| 49 | }) |
| 50 | } else { |
| 51 | result.Filters.LogicalOperator = logicalOperator |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | filterCount := 0 |
| 56 | for key, values := range queryParams { |
| 57 | if len(values) == 0 { |
| 58 | continue |
| 59 | } |
| 60 | |
| 61 | if isReservedParam(key) { |
| 62 | continue |
| 63 | } |
| 64 | |
| 65 | // Parse field_operator format |
| 66 | parts := strings.Split(key, "_") |
| 67 | if len(parts) < 2 { |
| 68 | continue |
| 69 | } |
| 70 | |
| 71 | // Extract operator (last part) and field (everything before) |
| 72 | operatorStr := parts[len(parts)-1] |
| 73 | field := strings.Join(parts[:len(parts)-1], "_") |
| 74 |