File: opts.go

package info (click to toggle)
golang-github-mesos-mesos-go 0.0.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 11,724 kB
  • sloc: makefile: 163
file content (53 lines) | stat: -rw-r--r-- 1,178 bytes parent folder | download
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
49
50
51
52
53
package httpcli

type (
	// Opt defines a functional option for the HTTP client type. A functional option
	// must return an Opt that acts as an "undo" if applied to the same Client.
	Opt func(*Client) Opt
	// Opts represents a series of functional options
	Opts []Opt
)

// Apply is a nil-safe application of an Opt: if the receiver is nil then this func
// simply returns nil, otherwise it returns the result invoking the receiving Opt
// with the given Client.
func (o Opt) Apply(c *Client) (result Opt) {
	if o != nil {
		result = o(c)
	}
	return
}

// Merged generates a single Opt that applies all the functional options, in-order
func (opts Opts) Merged() Opt {
	if len(opts) == 0 {
		return nil
	}
	return func(c *Client) Opt {
		var (
			size = len(opts)
			undo = make(Opts, size)
		)
		size-- // make this a zero-based offset
		for i, opt := range opts {
			if opt != nil {
				undo[size-i] = opt(c)
			}
		}
		return undo.Merged()
	}
}

// And combines two functional options into a single Opt
func (o Opt) And(other Opt) Opt {
	if o == nil {
		if other == nil {
			return nil
		}
		return other
	}
	if other == nil {
		return o
	}
	return Opts{o, other}.Merged()
}