• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / inventory / 1401701972

05 Aug 2024 08:32PM UTC coverage: 91.217%. Remained the same
1401701972

push

gitlab-ci

web-flow
Merge pull request #460 from mendersoftware/dependabot/docker/docker-dependencies-03b04ac819

chore: bump golang from 1.22.4-alpine3.19 to 1.22.5-alpine3.19 in the docker-dependencies group

3095 of 3393 relevant lines covered (91.22%)

148.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/model/filters.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//        Licensed under the Apache License, Version 2.0 (the "License");
4
//        you may not use this file except in compliance with the License.
5
//        You may obtain a copy of the License at
6
//
7
//            http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//        Unless required by applicable law or agreed to in writing, software
10
//        distributed under the License is distributed on an "AS IS" BASIS,
11
//        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//        See the License for the specific language governing permissions and
13
//        limitations under the License.
14
package model
15

16
import (
17
        validation "github.com/go-ozzo/ozzo-validation/v4"
18
        "github.com/pkg/errors"
19
)
20

21
var validSelectors = []interface{}{
22
        "$eq",
23
        "$nin",
24
}
25

26
var validSortOrders = []interface{}{"asc", "desc"}
27

28
type SearchParams struct {
29
        Page       int               `json:"page"`
30
        PerPage    int               `json:"per_page"`
31
        Filters    []FilterPredicate `json:"filters"`
32
        Sort       []SortCriteria    `json:"sort"`
33
        Attributes []SelectAttribute `json:"attributes"`
34
        DeviceIDs  []string          `json:"device_ids"`
35
        Text       string            `json:"text"`
36
}
37

38
type Filter struct {
39
        Id    string            `json:"id" bson:"_id"`
40
        Name  string            `json:"name" bson:"name"`
41
        Terms []FilterPredicate `json:"terms" bson:"terms"`
42
}
43

44
type FilterPredicate struct {
45
        Scope     string      `json:"scope" bson:"scope"`
46
        Attribute string      `json:"attribute" bson:"attribute"`
47
        Type      string      `json:"type" bson:"type"`
48
        Value     interface{} `json:"value" bson:"value"`
49
}
50

51
type SortCriteria struct {
52
        Scope     string `json:"scope"`
53
        Attribute string `json:"attribute"`
54
        Order     string `json:"order"`
55
}
56

57
type SelectAttribute struct {
58
        Scope     string `json:"scope" bson:"scope"`
59
        Attribute string `json:"attribute" bson:"attribute"`
60
}
61

62
func (sp SearchParams) Validate() error {
7✔
63
        for _, f := range sp.Filters {
9✔
64
                err := f.Validate()
2✔
65
                if err != nil {
3✔
66
                        return err
1✔
67
                }
1✔
68
        }
69

70
        for _, s := range sp.Sort {
8✔
71
                err := validation.ValidateStruct(&s,
2✔
72
                        validation.Field(&s.Scope, validation.Required),
2✔
73
                        validation.Field(&s.Attribute, validation.Required),
2✔
74
                        validation.Field(&s.Order, validation.Required, validation.In(validSortOrders...)))
2✔
75
                if err != nil {
3✔
76
                        return err
1✔
77
                }
1✔
78
        }
79

80
        for _, s := range sp.Attributes {
7✔
81
                err := validation.ValidateStruct(&s,
2✔
82
                        validation.Field(&s.Scope, validation.Required),
2✔
83
                        validation.Field(&s.Attribute, validation.Required))
2✔
84
                if err != nil {
3✔
85
                        return err
1✔
86
                }
1✔
87
        }
88
        return nil
4✔
89
}
90

91
func (f Filter) Validate() error {
4✔
92
        err := validation.ValidateStruct(&f,
4✔
93
                validation.Field(&f.Name, validation.Required))
4✔
94
        if err != nil {
5✔
95
                return err
1✔
96
        }
1✔
97

98
        if len(f.Terms) == 0 {
4✔
99
                return errors.New("at least one filter term must be provided")
1✔
100
        }
1✔
101

102
        for _, fp := range f.Terms {
4✔
103
                err = fp.Validate()
2✔
104
                if err != nil {
3✔
105
                        return errors.Wrap(err, "validation failed for term")
1✔
106
                }
1✔
107
        }
108

109
        return nil
1✔
110
}
111

112
func (f FilterPredicate) Validate() error {
4✔
113
        return validation.ValidateStruct(&f,
4✔
114
                validation.Field(&f.Scope, validation.Required),
4✔
115
                validation.Field(&f.Attribute, validation.Required),
4✔
116
                validation.Field(&f.Type, validation.Required, validation.In(validSelectors...)),
4✔
117
                validation.Field(&f.Value, validation.NotNil))
4✔
118
}
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc