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 decoded interface{}
41 err := json.Unmarshal(data, &decoded)
45 switch decoded := decoded.(type) {
47 // Accept "(foo < bar)" as a more obvious way to spell
48 // ["(foo < bar)","=",true]
49 *f = Filter{decoded, "=", true}
51 if len(decoded) != 3 {
52 return fmt.Errorf("invalid filter %q: must have 3 decoded", data)
54 attr, ok := decoded[0].(string)
56 return fmt.Errorf("invalid filter attr %q", decoded[0])
58 op, ok := decoded[1].(string)
60 return fmt.Errorf("invalid filter operator %q", decoded[1])
63 switch operand.(type) {
64 case string, float64, []interface{}, nil, bool:
66 return fmt.Errorf("invalid filter operand %q", decoded[2])
68 *f = Filter{attr, op, operand}
70 return fmt.Errorf("invalid filter: json decoded as %T instead of array or string", decoded)