-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoption.go
45 lines (39 loc) · 1.1 KB
/
option.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
package osm
// Opts holds configuration options for requests.
type Opts struct {
Locale string // Locale specifies the language for the response.
UserAgent string // UserAgent specifies the User-Agent header for the request.
}
type optFunc func(*Opts)
// DefaultOpts provides the default options for requests.
var DefaultOpts = Opts{
Locale: "en", // Default locale is English.
UserAgent: "Chrome", // Default User-Agent is Chrome.
}
// WithLocale returns an optFunc that sets the locale option.
// locale specifies the language for the response.
func WithLocale(locale string) optFunc {
return func(o *Opts) {
o.Locale = locale
}
}
// WithUserAgent returns an optFunc that sets the User-Agent option.
// userAgent specifies the User-Agent header for the request.
func WithUserAgent(userAgent string) optFunc {
return func(o *Opts) {
o.UserAgent = userAgent
}
}
func mergeOpts(optsList []optFunc) *Opts {
o := &Opts{}
for _, opt := range optsList {
opt(o)
}
if o.Locale == "" {
o.Locale = DefaultOpts.Locale
}
if o.UserAgent == "" {
o.UserAgent = DefaultOpts.UserAgent
}
return o
}