yanic/output/all/output.go

71 lines
1.8 KiB
Go
Raw Normal View History

2018-01-03 15:41:40 +01:00
package all
import (
"fmt"
"log"
"github.com/FreifunkBremen/yanic/output"
2018-01-13 19:08:46 +01:00
"github.com/FreifunkBremen/yanic/output/filter"
2018-01-03 15:41:40 +01:00
"github.com/FreifunkBremen/yanic/runtime"
)
type Output struct {
output.Output
2018-01-13 19:08:46 +01:00
list map[int]output.Output
outputFilter map[int]filter.Set
2018-01-03 15:41:40 +01:00
}
func Register(configuration map[string]interface{}) (output.Output, error) {
list := make(map[int]output.Output)
2018-01-13 19:08:46 +01:00
outputFilter := make(map[int]filter.Set)
2018-01-03 15:41:40 +01:00
i := 1
allOutputs := configuration
for outputType, outputRegister := range output.Adapters {
configForOutput := allOutputs[outputType]
if configForOutput == nil {
log.Printf("the output type '%s' has no configuration\n", outputType)
continue
}
2018-01-15 01:01:32 +01:00
outputConfigs, ok := configForOutput.([]interface{})
2018-01-03 15:41:40 +01:00
if !ok {
return nil, fmt.Errorf("the output type '%s' has the wrong format", outputType)
}
2018-01-15 01:01:32 +01:00
for _, outputConfig := range outputConfigs {
config, ok := outputConfig.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("the output type '%s' has the wrong format", outputType)
}
2018-01-03 15:41:40 +01:00
if c, ok := config["enable"].(bool); ok && !c {
continue
}
output, err := outputRegister(config)
if err != nil {
return nil, err
}
if output == nil {
continue
}
2018-01-13 19:08:46 +01:00
var errs []error
var filterSet filter.Set
2018-01-03 15:41:40 +01:00
if c := config["filter"]; c != nil {
2018-01-13 19:08:46 +01:00
if filterConf, ok := c.(map[string]interface{}); ok {
filterSet, errs = filter.New(filterConf)
}
if len(errs) > 0 {
return nil, fmt.Errorf("filter configuration errors: %v", errs)
}
outputFilter[i] = filterSet
2018-01-03 15:41:40 +01:00
}
2018-01-13 19:08:46 +01:00
list[i] = output
2018-01-03 15:41:40 +01:00
i++
}
}
2018-01-13 19:08:46 +01:00
return &Output{list: list, outputFilter: outputFilter}, nil
2018-01-03 15:41:40 +01:00
}
func (o *Output) Save(nodes *runtime.Nodes) {
for i, item := range o.list {
2018-01-13 19:08:46 +01:00
item.Save(o.outputFilter[i].Apply(nodes))
2018-01-03 15:41:40 +01:00
}
}