-
Notifications
You must be signed in to change notification settings - Fork 36
/
urlparams.go
48 lines (40 loc) · 1.12 KB
/
urlparams.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package gomaasapi
import (
"fmt"
"net/url"
)
// URLParams wraps url.Values to easily add values, but skipping empty ones.
type URLParams struct {
Values url.Values
}
// NewURLParams allocates a new URLParams type.
func NewURLParams() *URLParams {
return &URLParams{Values: make(url.Values)}
}
// MaybeAdd adds the (name, value) pair iff value is not empty.
func (p *URLParams) MaybeAdd(name, value string) {
if value != "" {
p.Values.Add(name, value)
}
}
// MaybeAddInt adds the (name, value) pair iff value is not zero.
func (p *URLParams) MaybeAddInt(name string, value int) {
if value != 0 {
p.Values.Add(name, fmt.Sprint(value))
}
}
// MaybeAddBool adds the (name, value) pair iff value is true.
func (p *URLParams) MaybeAddBool(name string, value bool) {
if value {
p.Values.Add(name, fmt.Sprint(value))
}
}
// MaybeAddMany adds the (name, value) for each value in values iff
// value is not empty.
func (p *URLParams) MaybeAddMany(name string, values []string) {
for _, value := range values {
p.MaybeAdd(name, value)
}
}