Debian Codegraph @

api/v1/lib/httpcli/opts.go

raw symbols SCIP
1 package httpcli
2
3 type (
4 // Opt defines a functional option for the HTTP client type. A functional option
5 // must return an Opt that acts as an "undo" if applied to the same Client.
6 Opt func(*Client) Opt
7 // Opts represents a series of functional options
8 Opts []Opt
9 )
10
11 // Apply is a nil-safe application of an Opt: if the receiver is nil then this func
12 // simply returns nil, otherwise it returns the result invoking the receiving Opt
13 // with the given Client.
14 func (o Opt) Apply(c *Client) (result Opt) {
15 if o != nil {
16 result = o(c)
17 }
18 return
19 }
20
21 // Merged generates a single Opt that applies all the functional options, in-order
22 func (opts Opts) Merged() Opt {
23 if len(opts) == 0 {
24 return nil
25 }
26 return func(c *Client) Opt {
27 var (
28 size = len(opts)
29 undo = make(Opts, size)
30 )
31 size-- // make this a zero-based offset
32 for i, opt := range opts {
33 if opt != nil {
34 undo[size-i] = opt(c)
35 }
36 }
37 return undo.Merged()
38 }
39 }
40
41 // And combines two functional options into a single Opt
42 func (o Opt) And(other Opt) Opt {
43 if o == nil {
44 if other == nil {
45 return nil
46 }
47 return other
48 }
49 if other == nil {
50 return o
51 }
52 return Opts{o, other}.Merged()
53 }