1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
12 // ResourceListParams expresses which results are requested in a
14 type ResourceListParams struct {
15 Select []string `json:"select,omitempty"`
16 Filters []Filter `json:"filters,omitempty"`
17 IncludeTrash bool `json:"include_trash,omitempty"`
18 IncludeOldVersions bool `json:"include_old_versions,omitempty"`
19 Limit *int `json:"limit,omitempty"`
20 Offset int `json:"offset,omitempty"`
21 Order string `json:"order,omitempty"`
22 Distinct bool `json:"distinct,omitempty"`
23 Count string `json:"count,omitempty"`
26 // A Filter restricts the set of records returned by a list/index API.
33 // MarshalJSON encodes a Filter to a JSON array.
34 func (f *Filter) MarshalJSON() ([]byte, error) {
35 return json.Marshal([]interface{}{f.Attr, f.Operator, f.Operand})
38 // UnmarshalJSON decodes a JSON array to a Filter.
39 func (f *Filter) UnmarshalJSON(data []byte) error {
40 var elements []interface{}
41 err := json.Unmarshal(data, &elements)
45 if len(elements) != 3 {
46 return fmt.Errorf("invalid filter %q: must have 3 elements", data)
48 attr, ok := elements[0].(string)
50 return fmt.Errorf("invalid filter attr %q", elements[0])
52 op, ok := elements[1].(string)
54 return fmt.Errorf("invalid filter operator %q", elements[1])
56 operand := elements[2]
57 switch operand.(type) {
58 case string, float64, []interface{}, nil, bool:
60 return fmt.Errorf("invalid filter operand %q", elements[2])
62 *f = Filter{attr, op, operand}