Skip to content

Commit

Permalink
feat(registry): add time.Duration option for registries
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhailswift committed Aug 8, 2023
1 parent 6e4df6c commit 47e1475
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
16 changes: 14 additions & 2 deletions registry/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

package registry

import "fmt"
import (
"fmt"
"time"
)

type Configurer interface {
Description() string
Expand All @@ -23,7 +26,7 @@ type Configurer interface {
}

type Option interface {
int | string | []string | bool
int | string | []string | bool | time.Duration
}

type ConfigOption[T any, TOption Option] struct {
Expand Down Expand Up @@ -93,3 +96,12 @@ func BoolConfigOption[T any](name, description string, defaultVal bool, setter f
setter: setter,
}
}

func DurationConfigOption[T any](name, description string, defaultVal time.Duration, setter func(T, time.Duration) (T, error)) *ConfigOption[T, time.Duration] {
return &ConfigOption[T, time.Duration]{
name: name,
description: description,
defaultVal: defaultVal,
setter: setter,
}
}
7 changes: 6 additions & 1 deletion registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

package registry

import "fmt"
import (
"fmt"
"time"
)

// Registry is a way for the library to expose details about available configuration options
// for a collection of Entities at run time. This is useful to do things such as expose attestor
Expand Down Expand Up @@ -124,6 +127,8 @@ func (r Registry[T]) SetDefaultVals(entity T, opts []Configurer) (T, error) {
entity, err = o.Setter()(entity, o.DefaultVal())
case *ConfigOption[T, bool]:
entity, err = o.Setter()(entity, o.DefaultVal())
case *ConfigOption[T, time.Duration]:
entity, err = o.Setter()(entity, o.DefaultVal())
}

if err != nil {
Expand Down

0 comments on commit 47e1475

Please sign in to comment.