-
Notifications
You must be signed in to change notification settings - Fork 5
/
option.go
49 lines (42 loc) · 921 Bytes
/
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
46
47
48
49
package rwriter
import (
"fmt"
)
type config struct {
cidPathType string
mhPathType string
preferJson bool
}
// Option is a function that sets a value in a config.
type Option func(*config) error
// getOpts creates a config and applies Options to it.
func getOpts(opts []Option) (config, error) {
cfg := config{
mhPathType: "multihash",
cidPathType: "cid",
}
for i, opt := range opts {
if err := opt(&cfg); err != nil {
return config{}, fmt.Errorf("option %d failed: %s", i, err)
}
}
return cfg, nil
}
func WithCidPathType(pathElem string) Option {
return func(cfg *config) error {
cfg.cidPathType = pathElem
return nil
}
}
func WithMultihashPathType(pathElem string) Option {
return func(cfg *config) error {
cfg.mhPathType = pathElem
return nil
}
}
func WithPreferJson(preferJson bool) Option {
return func(cfg *config) error {
cfg.preferJson = preferJson
return nil
}
}