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"
)
/* -------------------- 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
type Position struct {
Height int
@ -22,28 +51,30 @@ type Position struct {
// NewPositionFromYAML creates and returns a new instance of 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
top, err := moduleConfig.Int(positionPath + ".top")
errs["top"] = err
val, err = moduleConfig.Int(positionPath + ".top")
validations["top"] = newPositionValidation("top", val, err)
left, err := moduleConfig.Int(positionPath + ".left")
errs["left"] = err
val, err = moduleConfig.Int(positionPath + ".left")
validations["left"] = newPositionValidation("left", val, err)
width, err := moduleConfig.Int(positionPath + ".width")
errs["width"] = err
val, err = moduleConfig.Int(positionPath + ".width")
validations["width"] = newPositionValidation("width", val, err)
height, err := moduleConfig.Int(positionPath + ".height")
errs["height"] = err
val, err = moduleConfig.Int(positionPath + ".height")
validations["height"] = newPositionValidation("height", val, err)
validatePositions(moduleName, errs)
validatePositions(moduleName, validations)
pos := Position{
Top: top,
Left: left,
Width: width,
Height: height,
Top: validations["top"].val,
Left: validations["left"].val,
Width: validations["width"].val,
Height: validations["height"].val,
}
return pos
@ -71,18 +102,18 @@ func NewPositionFromYAML(moduleName string, moduleConfig *config.Config) Positio
// width: 2
// height: 1
//
func validatePositions(moduleName string, errs map[string]error) {
func validatePositions(moduleName string, validations map[string]*positionValidation) {
var errStr string
for pos, err := range errs {
if err != nil {
errStr += fmt.Sprintf(" - Invalid value for %s\n", pos)
for _, posVal := range validations {
if posVal.hasError() {
errStr += fmt.Sprintf(" - %s.\t\033[0;31mError\033[0m %v\n", posVal, posVal.err)
}
}
if errStr != "" {
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("Please check your config.yml file.")
fmt.Println()