1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Use a map to return the failed position value and error message

This commit is contained in:
Chris Cummer 2019-07-05 22:21:50 -07:00
parent 774c465f57
commit 0104f97805

View File

@ -12,6 +12,35 @@ const (
positionPath = "position" positionPath = "position"
) )
/* -------------------- Position Validation -------------------- */
type positionValidation struct {
err error
name string
val int
}
func (posVal *positionValidation) hasError() bool {
return posVal.err != nil
}
// String returns the Stringer representation of the positionValidation
func (posVal *positionValidation) String() string {
return fmt.Sprintf("Invalid value for %s:\t%d", posVal.name, posVal.val)
}
func newPositionValidation(name string, val int, err error) *positionValidation {
posVal := &positionValidation{
err: err,
name: name,
val: val,
}
return posVal
}
/* -------------------- Position -------------------- */
// Position represents the onscreen location of a widget // Position represents the onscreen location of a widget
type Position struct { type Position struct {
Height int Height int
@ -22,28 +51,30 @@ type Position struct {
// NewPositionFromYAML creates and returns a new instance of Position // NewPositionFromYAML creates and returns a new instance of Position
func NewPositionFromYAML(moduleName string, moduleConfig *config.Config) Position { func NewPositionFromYAML(moduleName string, moduleConfig *config.Config) Position {
errs := make(map[string]error) var val int
var err error
validations := make(map[string]*positionValidation)
// Parse the positional data from the config data // Parse the positional data from the config data
top, err := moduleConfig.Int(positionPath + ".top") val, err = moduleConfig.Int(positionPath + ".top")
errs["top"] = err validations["top"] = newPositionValidation("top", val, err)
left, err := moduleConfig.Int(positionPath + ".left") val, err = moduleConfig.Int(positionPath + ".left")
errs["left"] = err validations["left"] = newPositionValidation("left", val, err)
width, err := moduleConfig.Int(positionPath + ".width") val, err = moduleConfig.Int(positionPath + ".width")
errs["width"] = err validations["width"] = newPositionValidation("width", val, err)
height, err := moduleConfig.Int(positionPath + ".height") val, err = moduleConfig.Int(positionPath + ".height")
errs["height"] = err validations["height"] = newPositionValidation("height", val, err)
validatePositions(moduleName, errs) validatePositions(moduleName, validations)
pos := Position{ pos := Position{
Top: top, Top: validations["top"].val,
Left: left, Left: validations["left"].val,
Width: width, Width: validations["width"].val,
Height: height, Height: validations["height"].val,
} }
return pos return pos
@ -71,18 +102,18 @@ func NewPositionFromYAML(moduleName string, moduleConfig *config.Config) Positio
// width: 2 // width: 2
// height: 1 // height: 1
// //
func validatePositions(moduleName string, errs map[string]error) { func validatePositions(moduleName string, validations map[string]*positionValidation) {
var errStr string var errStr string
for pos, err := range errs { for _, posVal := range validations {
if err != nil { if posVal.hasError() {
errStr += fmt.Sprintf(" - Invalid value for %s\n", pos) errStr += fmt.Sprintf(" - %s.\t\033[0;31mError\033[0m %v\n", posVal, posVal.err)
} }
} }
if errStr != "" { if errStr != "" {
fmt.Println() fmt.Println()
fmt.Printf("\033[0;31mErrors in %s configuration\033[0m\n", strings.Title(moduleName)) fmt.Printf("\033[0;1mErrors in %s position configuration\033[0m\n", strings.Title(moduleName))
fmt.Println(errStr) fmt.Println(errStr)
fmt.Println("Please check your config.yml file.") fmt.Println("Please check your config.yml file.")
fmt.Println() fmt.Println()