-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_builder.go
More file actions
58 lines (47 loc) · 1.65 KB
/
config_builder.go
File metadata and controls
58 lines (47 loc) · 1.65 KB
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
54
55
56
57
58
package httpcache
import "time"
// If adding new fields to [Config], then add the corresponding field and
// methods to [configBuilder]. Configuration values in [configBuilder] always
// use pointer types. This allows the [Config] decide which fields are
// required.
// configBuilder is an internal structure to create configs with required
// parameters.
type configBuilder struct {
// allowedStatusCodes only persists HTTP responses that have an appropriate
// status code (i.e. 200).
//
// This is a required field.
allowedStatusCodes *[]int
// allowedMethods only persists HTTP responses that use an appropriate HTTP
// method (i.e. "GET").
//
// This is a required field.
allowedMethods *[]string
// expiryTime invalidates HTTP responses after a duration has elapsed from
// [time.Now]. Set to nil for no expiry.
expiryTime *time.Duration
}
func NewConfigBuilder() *configBuilder {
return &configBuilder{}
}
func (c *configBuilder) WithAllowedStatusCodes(allowedStatusCodes []int) *configBuilder {
c.allowedStatusCodes = &allowedStatusCodes
return c
}
func (c *configBuilder) WithAllowedMethods(allowedMethods []string) *configBuilder {
c.allowedMethods = &allowedMethods
return c
}
func (c *configBuilder) WithExpiryTime(expiryDuration time.Duration) *configBuilder {
c.expiryTime = &expiryDuration
return c
}
// Build constructs a [Config] that is ready to be consumed by [Transport]. If
// the configuration passed by [configBuilder] is invalid, it will panic.
func (c *configBuilder) Build() *Config {
return &Config{
AllowedStatusCodes: *c.allowedStatusCodes,
AllowedMethods: *c.allowedMethods,
ExpiryTime: c.expiryTime,
}
}