basic config validation added

This commit is contained in:
sqshq 2019-06-09 15:00:16 -04:00
parent e7aa9691bc
commit a148f4250d
2 changed files with 170 additions and 124 deletions

View File

@ -1,10 +1,55 @@
package config
/*
TODO validation
- title uniquness and mandatory within a single type of widget
- label uniqueness and mandatory (if > 1 data bullets)
*/
import (
"fmt"
"github.com/sqshq/sampler/console"
)
func (c *Config) validate() {
var components []ComponentConfig
for _, c := range c.RunCharts {
components = append(components, c.ComponentConfig)
validateLabelsUniqueness(c.Title, c.Items)
}
for _, c := range c.BarCharts {
components = append(components, c.ComponentConfig)
validateLabelsUniqueness(c.Title, c.Items)
}
for _, c := range c.SparkLines {
components = append(components, c.ComponentConfig)
}
for _, c := range c.Gauges {
components = append(components, c.ComponentConfig)
}
for _, c := range c.AsciiBoxes {
components = append(components, c.ComponentConfig)
}
for _, c := range c.TextBoxes {
components = append(components, c.ComponentConfig)
}
validateTitlesUniqueness(components)
}
func validateLabelsUniqueness(title string, items []Item) {
labels := make(map[string]bool)
for _, c := range items {
label := *c.Label
if _, contains := labels[label]; contains {
console.Exit(fmt.Sprintf("Config validation error: item labels should be unique. Please rename '%s' in '%s'", label, title))
}
labels[label] = true
}
}
func validateTitlesUniqueness(components []ComponentConfig) {
titles := make(map[string]bool)
for _, c := range components {
if _, contains := titles[c.Title]; contains {
console.Exit(fmt.Sprintf("Config validation error: component titles should be unique. Please rename '%s'", c.Title))
}
titles[c.Title] = true
}
}

View File

@ -112,6 +112,7 @@ textboxes:
rate-ms: 10000
sample: curl wttr.in/newyork?0ATQF
border: false
transform: echo 1
- title: San Francisco weather
position: [[17, 30], [13, 7]]
rate-ms: 10000