Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Oct 4, 2024
1 parent 30c10fe commit 18df0a3
Showing 1 changed file with 23 additions and 82 deletions.
105 changes: 23 additions & 82 deletions provider/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,162 +44,103 @@ type Config struct {
Other map[string]any `mapstructure:",remain" yaml:",inline"`
}

// NewIntGetterFromConfig creates a IntGetter from config
func NewIntGetterFromConfig(config Config) (func() (int64, error), error) {
func fromFactory[T any](typ string, config Config) (T, error) {
var zero T

factory, err := registry.Get(config.Source)
if err != nil {
return nil, err
return zero, err
}

provider, err := factory(context.TODO(), config.Other)
if err != nil {
return nil, err
return zero, err
}

prov, ok := provider.(IntProvider)
prov, ok := provider.(T)
if !ok {
return nil, fmt.Errorf("invalid plugin source for type int: %s", config.Source)
return zero, fmt.Errorf("invalid plugin source for type %s: %s", typ, config.Source)
}

return prov.IntGetter()
return prov, nil
}

// NewFloatGetterFromConfig creates a FloatGetter from config
func NewFloatGetterFromConfig(config Config) (func() (float64, error), error) {
factory, err := registry.Get(config.Source)
// NewIntGetterFromConfig creates a IntGetter from config
func NewIntGetterFromConfig(config Config) (func() (int64, error), error) {
prov, err := fromFactory[IntProvider]("int", config)
if err != nil {
return nil, err
}

provider, err := factory(context.TODO(), config.Other)
return prov.IntGetter()
}

// NewFloatGetterFromConfig creates a FloatGetter from config
func NewFloatGetterFromConfig(config Config) (func() (float64, error), error) {
prov, err := fromFactory[FloatProvider]("float", config)
if err != nil {
return nil, err
}

prov, ok := provider.(FloatProvider)
if !ok {
return nil, fmt.Errorf("invalid plugin source for type float: %s", config.Source)
}

return prov.FloatGetter()
}

// NewStringGetterFromConfig creates a StringGetter from config
func NewStringGetterFromConfig(config Config) (func() (string, error), error) {
factory, err := registry.Get(config.Source)
prov, err := fromFactory[StringProvider]("string", config)
if err != nil {
return nil, err
}

provider, err := factory(context.TODO(), config.Other)
if err != nil {
return nil, err
}

prov, ok := provider.(StringProvider)
if !ok {
return nil, fmt.Errorf("invalid plugin source for type string: %s", config.Source)
}

return prov.StringGetter()
}

// NewBoolGetterFromConfig creates a BoolGetter from config
func NewBoolGetterFromConfig(config Config) (func() (bool, error), error) {
factory, err := registry.Get(config.Source)
prov, err := fromFactory[BoolProvider]("bool", config)
if err != nil {
return nil, err
}

provider, err := factory(context.TODO(), config.Other)
if err != nil {
return nil, err
}

prov, ok := provider.(BoolProvider)
if !ok {
return nil, fmt.Errorf("invalid plugin source for type bool: %s", config.Source)
}

return prov.BoolGetter()
}

// NewIntSetterFromConfig creates a IntSetter from config
func NewIntSetterFromConfig(param string, config Config) (func(int64) error, error) {
factory, err := registry.Get(config.Source)
if err != nil {
return nil, err
}

provider, err := factory(context.TODO(), config.Other)
prov, err := fromFactory[SetIntProvider]("int", config)
if err != nil {
return nil, err
}

prov, ok := provider.(SetIntProvider)
if !ok {
return nil, fmt.Errorf("invalid plugin source for type int: %s", config.Source)
}

return prov.IntSetter(param)
}

// NewFloatSetterFromConfig creates a FloatSetter from config
func NewFloatSetterFromConfig(param string, config Config) (func(float642 float64) error, error) {
factory, err := registry.Get(config.Source)
prov, err := fromFactory[SetFloatProvider]("float", config)
if err != nil {
return nil, err
}

provider, err := factory(context.TODO(), config.Other)
if err != nil {
return nil, err
}

prov, ok := provider.(SetFloatProvider)
if !ok {
return nil, fmt.Errorf("invalid plugin source for type float: %s", config.Source)
}

return prov.FloatSetter(param)
}

// NewStringSetterFromConfig creates a StringSetter from config
func NewStringSetterFromConfig(param string, config Config) (func(string) error, error) {
factory, err := registry.Get(config.Source)
prov, err := fromFactory[SetStringProvider]("string", config)
if err != nil {
return nil, err
}

provider, err := factory(context.TODO(), config.Other)
if err != nil {
return nil, err
}

prov, ok := provider.(SetStringProvider)
if !ok {
return nil, fmt.Errorf("invalid plugin source for type string: %s", config.Source)
}

return prov.StringSetter(param)
}

// NewBoolSetterFromConfig creates a BoolSetter from config
func NewBoolSetterFromConfig(param string, config Config) (func(bool) error, error) {
factory, err := registry.Get(config.Source)
prov, err := fromFactory[SetBoolProvider]("bool", config)
if err != nil {
return nil, err
}

provider, err := factory(context.TODO(), config.Other)
if err != nil {
return nil, err
}

prov, ok := provider.(SetBoolProvider)
if !ok {
return nil, fmt.Errorf("invalid plugin source for type bool: %s", config.Source)
}

return prov.BoolSetter(param)
}

0 comments on commit 18df0a3

Please sign in to comment.