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

mendersoftware / go-lib-micro / 1377056244

17 Jul 2024 10:41AM UTC coverage: 82.042% (-0.005%) from 82.047%
1377056244

Pull #245

gitlab-ci

kjaskiewiczz
chore: update source code after introducing proper linter checks

Signed-off-by: Krzysztof Jaskiewicz <krzysztof.jaskiewicz@northern.tech>
Pull Request #245: ci: do static checks using golangci-lint

22 of 31 new or added lines in 8 files covered. (70.97%)

6 existing lines in 1 file now uncovered.

1567 of 1910 relevant lines covered (82.04%)

4.22 hits per line

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

30.3
/rest_utils/paging.go
1
// Copyright 2024 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

15
package rest_utils
16

17
import (
18
        "errors"
19
        "fmt"
20
        "math"
21
        "net/url"
22
        "strconv"
23
        "strings"
24

25
        "github.com/ant0ine/go-json-rest/rest"
26

27
        micro_strings "github.com/mendersoftware/go-lib-micro/strings"
28
)
29

30
// pagination constants
31
const (
32
        PageName       = "page"
33
        PerPageName    = "per_page"
34
        PageMin        = 1
35
        PageDefault    = 1
36
        PerPageMin     = 1
37
        PerPageMax     = 500
38
        PerPageDefault = 20
39
        LinkHdr        = "Link"
40
        LinkTmpl       = "<%s>; rel=\"%s\""
41
        LinkPrev       = "prev"
42
        LinkNext       = "next"
43
        LinkFirst      = "first"
44
        DefaultScheme  = "http"
45
)
46

47
// error msgs
48
func MsgQueryParmInvalid(name string) string {
×
49
        return fmt.Sprintf("Can't parse param %s", name)
×
50
}
×
51

52
func MsgQueryParmMissing(name string) string {
×
53
        return fmt.Sprintf("Missing required param %s", name)
×
54
}
×
55

56
func MsgQueryParmLimit(name string) string {
×
57
        return fmt.Sprintf("Param %s is out of bounds", name)
×
58
}
×
59

60
func MsgQueryParmOneOf(name string, allowed []string) string {
×
61
        return fmt.Sprintf("Param %s must be one of %v", name, allowed)
×
62
}
×
63

64
// query param parsing/validation
65
func ParseQueryParmUInt(
66
        r *rest.Request,
67
        name string,
68
        required bool,
69
        min, max, def uint64,
NEW
70
) (uint64, error) {
×
71
        strVal := r.URL.Query().Get(name)
×
72

×
73
        if strVal == "" {
×
74
                if required {
×
75
                        return 0, errors.New(MsgQueryParmMissing(name))
×
76
                } else {
×
77
                        return def, nil
×
78
                }
×
79
        }
80

81
        uintVal, err := strconv.ParseUint(strVal, 10, 32)
×
82
        if err != nil {
×
83
                return 0, errors.New(MsgQueryParmInvalid(name))
×
84
        }
×
85

86
        if uintVal < min || uintVal > max {
×
87
                return 0, errors.New(MsgQueryParmLimit(name))
×
88
        }
×
89

90
        return uintVal, nil
×
91
}
92

93
func ParseQueryParmBool(r *rest.Request, name string, required bool, def *bool) (*bool, error) {
×
94
        strVal := r.URL.Query().Get(name)
×
95

×
96
        if strVal == "" {
×
97
                if required {
×
98
                        return nil, errors.New(MsgQueryParmMissing(name))
×
99
                } else {
×
100
                        return def, nil
×
101
                }
×
102
        }
103

104
        boolVal, err := strconv.ParseBool(strVal)
×
105
        if err != nil {
×
106
                return nil, errors.New(MsgQueryParmInvalid(name))
×
107
        }
×
108

109
        return &boolVal, nil
×
110
}
111

112
func ParseQueryParmStr(
113
        r *rest.Request,
114
        name string,
115
        required bool,
116
        allowed []string,
117
) (string, error) {
3✔
118
        val := r.URL.Query().Get(name)
3✔
119

3✔
120
        if val == "" {
3✔
121
                if required {
×
122
                        return "", errors.New(MsgQueryParmMissing(name))
×
123
                }
×
124
        } else {
3✔
125
                if allowed != nil && !micro_strings.ContainsString(val, allowed) {
3✔
126
                        return "", errors.New(MsgQueryParmOneOf(name, allowed))
×
127
                }
×
128
        }
129

130
        return val, nil
3✔
131
}
132

133
// pagination helpers
134
func ParsePagination(r *rest.Request) (uint64, uint64, error) {
×
135
        page, err := ParseQueryParmUInt(r, PageName, false, PageMin, math.MaxUint64, PageDefault)
×
136
        if err != nil {
×
137
                return 0, 0, err
×
138
        }
×
139

NEW
140
        per_page, err := ParseQueryParmUInt(
×
NEW
141
                r, PerPageName, false, PerPageMin, PerPageMax, PerPageDefault)
×
142
        if err != nil {
×
143
                return 0, 0, err
×
144
        }
×
145

146
        return page, per_page, nil
×
147
}
148

149
func MakePageLinkHdrs(r *rest.Request, page, per_page uint64, has_next bool) []string {
2✔
150
        var links []string
2✔
151
        if page > 1 {
3✔
152
                links = append(links, MakeLink(LinkPrev, r, page-1, per_page))
1✔
153
        }
1✔
154

155
        if has_next {
4✔
156
                links = append(links, MakeLink(LinkNext, r, page+1, per_page))
2✔
157
        }
2✔
158

159
        links = append(links, MakeLink(LinkFirst, r, 1, per_page))
2✔
160
        return links
2✔
161
}
162

163
// MakeLink creates a relative URL for insertion in the link header URL field.
164
func MakeLink(link_type string, r *rest.Request, page, per_page uint64) string {
5✔
165
        q := r.URL.Query()
5✔
166
        q.Set(PageName, strconv.Itoa(int(page)))
5✔
167
        q.Set(PerPageName, strconv.Itoa(int(per_page)))
5✔
168
        url := url.URL{
5✔
169
                Path:     r.URL.Path,
5✔
170
                RawPath:  r.URL.RawPath,
5✔
171
                RawQuery: q.Encode(),
5✔
172
                Fragment: r.URL.Fragment,
5✔
173
        }
5✔
174

5✔
175
        return fmt.Sprintf(LinkTmpl, url.String(), link_type)
5✔
176
}
5✔
177

178
// build URL using request 'r' and template, replace path params with
179
// elements from 'params' using lexical match as in strings.Replace()
UNCOV
180
func BuildURL(r *rest.Request, template string, params map[string]string) *url.URL {
×
UNCOV
181
        url := r.BaseUrl()
×
UNCOV
182

×
UNCOV
183
        path := template
×
UNCOV
184
        for k, v := range params {
×
UNCOV
185
                path = strings.Replace(path, k, v, -1)
×
186
        }
×
187
        url.Path = path
×
188

×
189
        return url
×
190
}
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