Skip to content

Commit

Permalink
Parse cache settings manually for friendly errors (#93)
Browse files Browse the repository at this point in the history
Just like it's done for all the other configuration options that
are based off of time durations.

Signed-off-by: Douglas Camata <[email protected]
  • Loading branch information
douglascamata authored Jul 18, 2022
1 parent 25b9abc commit 6a6aac7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
32 changes: 25 additions & 7 deletions pkg/mdformatter/linktransformer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ type Config struct {
}

type CacheConfig struct {
Type string `yaml:"type"`
Validity time.Duration `yaml:"validity"`
Jitter time.Duration `yaml:"jitter"`
Type string `yaml:"type"`
Validity string `yaml:"validity"`
Jitter string `yaml:"jitter"`
validity time.Duration
jitter time.Duration
}

type ValidatorConfig struct {
Expand Down Expand Up @@ -81,16 +83,16 @@ const (
none = "None"
sqlite = "SQLite"

timeDay = 24 * time.Hour
defaultValidity = 5 * timeDay
timeDay = 24 * time.Hour
defaultCacheValidity = 5 * timeDay
)

type GitHubResponse struct {
Number int `json:"number"`
}

func ParseConfig(c []byte) (Config, error) {
cfg := Config{Cache: CacheConfig{Validity: defaultValidity}}
cfg := Config{Cache: CacheConfig{Validity: defaultCacheValidity.String()}}
dec := yaml.NewDecoder(bytes.NewReader(c))
dec.KnownFields(true)
if err := dec.Decode(&cfg); err != nil {
Expand Down Expand Up @@ -118,7 +120,23 @@ func ParseConfig(c []byte) (Config, error) {
}

switch cfg.Cache.Type {
case none, sqlite, "":
case sqlite:
if cfg.Cache.Validity != "" {
var err error
cfg.Cache.validity, err = time.ParseDuration(cfg.Cache.Validity)
if err != nil {
return Config{}, errors.Wrap(err, "parsing cache validity duration")
}
}

if cfg.Cache.Jitter != "" {
var err error
cfg.Cache.jitter, err = time.ParseDuration(cfg.Cache.Jitter)
if err != nil {
return Config{}, errors.Wrap(err, "parsing cache jitter duration")
}
}
case none, "":
default:
return Config{}, errors.New("unsupported cache type")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/mdformatter/linktransformer/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func NewValidator(ctx context.Context, logger log.Logger, linksValidateConfig []

if v.validateConfig.Cache.Type != none && storage != nil {
v.storage = storage
if err = v.storage.Init(v.validateConfig.Cache.Validity, v.validateConfig.Cache.Jitter); err != nil {
if err = v.storage.Init(v.validateConfig.Cache.validity, v.validateConfig.Cache.jitter); err != nil {
return nil, err
}
}
Expand Down

0 comments on commit 6a6aac7

Please sign in to comment.