Skip to content

Commit

Permalink
Add generic registry (hides golang/go#44895)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Mar 10, 2021
1 parent e07b412 commit f53c09b
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 94 deletions.
7 changes: 3 additions & 4 deletions charger/charger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"github.com/andig/evcc/api"
"github.com/andig/evcc/provider"
"github.com/andig/evcc/util"
"github.com/andig/evcc/util/registry"
reg "github.com/andig/evcc/util/registry"
)

var registry = reg.New[api.Charger]()

// Charger is an api.Charger implementation with configurable getters and setters.
type Charger struct {
statusG func() (string, error)
Expand All @@ -17,10 +19,7 @@ type Charger struct {
maxCurrentS func(int64) error
}

var registry registry.Registry[api.Charger]

func init() {
registry=registry.New()
registry.Add("default", NewConfigurableFromConfig)
}

Expand Down
41 changes: 0 additions & 41 deletions meter/config.go

This file was deleted.

3 changes: 3 additions & 0 deletions meter/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"github.com/andig/evcc/api"
"github.com/andig/evcc/provider"
"github.com/andig/evcc/util"
reg "github.com/andig/evcc/util/registry"
)

var registry = reg.New[api.Meter]()

func init() {
registry.Add("default", NewConfigurableFromConfig)
}
Expand Down
6 changes: 1 addition & 5 deletions util/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import (

type Registry[T any] map[string]func(map[string]interface{}) (T, error)

// var registry [T]Registry = make(map[string]func(map[string]interface{}) (T, error))

func New[T any]() Registry[T] {
reg := make(map[string]func(map[string]interface{}) (T, error))
return reg
return make(map[string]func(map[string]interface{}) (T, error))
}

func (r Registry[T]) Add(name string, factory func(map[string]interface{}) (T, error)) {
Expand All @@ -29,7 +26,6 @@ func (r Registry[T]) Get(name string) (func(map[string]interface{}) (T, error),
return factory, nil
}


// NewFromConfig creates charger from configuration
func (r Registry[T]) NewFromConfig(typ string, other map[string]interface{}) (v T, err error) {
factory, err := r.Get(strings.ToLower(typ))
Expand Down
44 changes: 0 additions & 44 deletions vehicle/config.go

This file was deleted.

5 changes: 5 additions & 0 deletions vehicle/vehicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import (
"github.com/andig/evcc/api"
"github.com/andig/evcc/provider"
"github.com/andig/evcc/util"
reg "github.com/andig/evcc/util/registry"
)

const interval = 15 * time.Minute

var registry = reg.New[api.Vehicle]()

type embed struct {
title string
capacity int64
Expand Down

0 comments on commit f53c09b

Please sign in to comment.