Normalize briefing module options and weather stories
This commit is contained in:
@@ -100,7 +100,8 @@ func (r ModuleRegistry) BuildModule(ctx ModuleContext, item module.ConfigItem) (
|
||||
if !definition.SupportsReport(ctx.Resolved.Definition.ID) {
|
||||
return nil, fmt.Errorf("module %q is not compatible with report %q", item.ID, ctx.Resolved.Definition.ID)
|
||||
}
|
||||
if err := definition.ValidateOptions(item.Options); err != nil {
|
||||
options, err := definition.CanonicalOptions(item.Options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if definition.Builder == nil {
|
||||
@@ -120,7 +121,6 @@ func (r ModuleRegistry) BuildModule(ctx ModuleContext, item module.ConfigItem) (
|
||||
return nil, fmt.Errorf("module %q has unknown missing data behavior %q", item.ID, definition.MissingData)
|
||||
}
|
||||
}
|
||||
options := item.Options
|
||||
if options == nil {
|
||||
options = definition.DefaultOptions
|
||||
}
|
||||
@@ -264,6 +264,24 @@ func (d ModuleDefinition) ValidateOptions(options any) error {
|
||||
return fmt.Errorf("module %q options have type %s, want %s", d.ID, got, want)
|
||||
}
|
||||
|
||||
// CanonicalOptions validates options and converts an accepted typed pointer to its value form.
|
||||
func (d ModuleDefinition) CanonicalOptions(options any) (any, error) {
|
||||
if err := d.ValidateOptions(options); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if options == nil {
|
||||
return nil, nil
|
||||
}
|
||||
value := reflect.ValueOf(options)
|
||||
if value.Kind() != reflect.Pointer {
|
||||
return options, nil
|
||||
}
|
||||
if value.IsNil() {
|
||||
return nil, fmt.Errorf("module %q options must not be a nil pointer", d.ID)
|
||||
}
|
||||
return value.Elem().Interface(), nil
|
||||
}
|
||||
|
||||
func defaultModuleDefinitions() []ModuleDefinition {
|
||||
allReports := []report.ID{report.Daily, report.Today, report.Tomorrow, report.Hourly}
|
||||
daypartReports := []report.ID{report.Daily, report.Today, report.Tomorrow}
|
||||
|
||||
Reference in New Issue
Block a user