86 lines
2.0 KiB
Go
86 lines
2.0 KiB
Go
package report
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"gitea.maximumdirect.net/eric/weatherreporter/internal/module"
|
|
)
|
|
|
|
type Registry struct {
|
|
definitions map[ID]Definition
|
|
}
|
|
|
|
func DefaultRegistry() Registry {
|
|
definitions := []Definition{
|
|
dailyTodayDefinition(),
|
|
tomorrowDefinition(),
|
|
hourlyDefinition(),
|
|
threeDayDefinition(),
|
|
weekendDefinition(),
|
|
stormDefinition(),
|
|
}
|
|
registry := Registry{definitions: map[ID]Definition{}}
|
|
for _, definition := range definitions {
|
|
registry.definitions[definition.ID] = definition
|
|
}
|
|
return registry
|
|
}
|
|
|
|
func (r Registry) WithModuleOverrides(overrides map[ID][]module.ConfigItem) (Registry, error) {
|
|
next := Registry{definitions: map[ID]Definition{}}
|
|
for id, definition := range r.definitions {
|
|
definition.Modules = append([]module.ConfigItem(nil), definition.Modules...)
|
|
next.definitions[id] = definition
|
|
}
|
|
for id, items := range overrides {
|
|
definition, ok := next.definitions[id]
|
|
if !ok {
|
|
return Registry{}, fmt.Errorf("unknown report %q", id)
|
|
}
|
|
definition.Modules = cloneModuleItems(items)
|
|
next.definitions[id] = definition
|
|
}
|
|
return next, nil
|
|
}
|
|
|
|
func moduleItems(ids ...module.ID) []module.ConfigItem {
|
|
items := make([]module.ConfigItem, 0, len(ids))
|
|
for _, id := range ids {
|
|
items = append(items, module.ConfigItem{ID: id})
|
|
}
|
|
return items
|
|
}
|
|
|
|
func cloneModuleItems(items []module.ConfigItem) []module.ConfigItem {
|
|
cloned := make([]module.ConfigItem, len(items))
|
|
copy(cloned, items)
|
|
return cloned
|
|
}
|
|
|
|
func (r Registry) Lookup(id ID) (Definition, error) {
|
|
definition, ok := r.definitions[id]
|
|
if !ok {
|
|
return Definition{}, fmt.Errorf("unknown report %q", id)
|
|
}
|
|
return definition, nil
|
|
}
|
|
|
|
func (r Registry) MustLookup(id ID) Definition {
|
|
definition, err := r.Lookup(id)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return definition
|
|
}
|
|
|
|
func (r Registry) All() []Definition {
|
|
ids := []ID{DailyToday, Tomorrow, Hourly, ThreeDay, Weekend, Storm}
|
|
out := make([]Definition, 0, len(ids))
|
|
for _, id := range ids {
|
|
if definition, ok := r.definitions[id]; ok {
|
|
out = append(out, definition)
|
|
}
|
|
}
|
|
return out
|
|
}
|