-
-
Notifications
You must be signed in to change notification settings - Fork 805
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimpliments the module configuration validation
Now supports displaying errors from multiple widgets.
- Loading branch information
1 parent
c308d1b
commit b07f3c5
Showing
65 changed files
with
371 additions
and
363 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package cfg | ||
|
||
import ( | ||
"github.com/olebedev/config" | ||
) | ||
|
||
const ( | ||
positionPath = "position" | ||
) | ||
|
||
// PositionSettings represents the onscreen location of a widget | ||
type PositionSettings struct { | ||
Validations *Validations | ||
|
||
Height int | ||
Left int | ||
Top int | ||
Width int | ||
} | ||
|
||
// NewPositionSettingsFromYAML creates and returns a new instance of cfg.Position | ||
func NewPositionSettingsFromYAML(moduleName string, moduleConfig *config.Config) PositionSettings { | ||
var currVal int | ||
var err error | ||
|
||
validations := NewValidations() | ||
|
||
// Parse the positional data from the config data | ||
currVal, err = moduleConfig.Int(positionPath + ".top") | ||
validations.append("top", newPositionValidation("top", currVal, err)) | ||
|
||
currVal, err = moduleConfig.Int(positionPath + ".left") | ||
validations.append("left", newPositionValidation("left", currVal, err)) | ||
|
||
currVal, err = moduleConfig.Int(positionPath + ".width") | ||
validations.append("width", newPositionValidation("width", currVal, err)) | ||
|
||
currVal, err = moduleConfig.Int(positionPath + ".height") | ||
validations.append("height", newPositionValidation("height", currVal, err)) | ||
|
||
pos := PositionSettings{ | ||
Validations: validations, | ||
|
||
Top: validations.valueFor("top"), | ||
Left: validations.valueFor("left"), | ||
Width: validations.valueFor("width"), | ||
Height: validations.valueFor("height"), | ||
} | ||
|
||
return pos | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package cfg | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/logrusorgru/aurora" | ||
) | ||
|
||
// Common examples of invalid position configuration are: | ||
// | ||
// position: | ||
// top: -3 | ||
// left: 2 | ||
// width: 0 | ||
// height: 1 | ||
// | ||
// position: | ||
// top: 3 | ||
// width: 2 | ||
// height: 1 | ||
// | ||
// position: | ||
// top: 3 | ||
// # left: 2 | ||
// width: 2 | ||
// height: 1 | ||
// | ||
// position: | ||
// top: 3 | ||
// left: 2 | ||
// width: 2 | ||
// height: 1 | ||
// | ||
type positionValidation struct { | ||
err error | ||
name string | ||
intVal int | ||
} | ||
|
||
func (posVal *positionValidation) Error() error { | ||
return posVal.err | ||
} | ||
|
||
func (posVal *positionValidation) HasError() bool { | ||
return posVal.err != nil | ||
} | ||
|
||
func (posVal *positionValidation) IntValue() int { | ||
return posVal.intVal | ||
} | ||
|
||
// String returns the Stringer representation of the positionValidation | ||
func (posVal *positionValidation) String() string { | ||
return fmt.Sprintf("Invalid value for %s:\t%d", aurora.Yellow(posVal.name), posVal.intVal) | ||
} | ||
|
||
func newPositionValidation(name string, intVal int, err error) *positionValidation { | ||
posVal := &positionValidation{ | ||
err: err, | ||
name: name, | ||
intVal: intVal, | ||
} | ||
|
||
return posVal | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package cfg | ||
|
||
// Validatable is implemented by any value that validates a configuration setting | ||
type Validatable interface { | ||
Error() error | ||
HasError() bool | ||
String() string | ||
IntValue() int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cfg | ||
|
||
// Validations represent a collection of config setting validations | ||
type Validations struct { | ||
validations map[string]Validatable | ||
} | ||
|
||
// NewValidations creates and returns an instance of Validations | ||
func NewValidations() *Validations { | ||
vals := &Validations{ | ||
validations: make(map[string]Validatable), | ||
} | ||
|
||
return vals | ||
} | ||
|
||
func (vals *Validations) append(key string, posVal Validatable) { | ||
vals.validations[key] = posVal | ||
} | ||
|
||
func (vals *Validations) valueFor(key string) int { | ||
val := vals.validations[key] | ||
if val != nil { | ||
return val.IntValue() | ||
} | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.