Skip to content

Commit

Permalink
Extract bool conversion to method
Browse files Browse the repository at this point in the history
  • Loading branch information
dadgar committed Nov 25, 2015
1 parent 3b45305 commit 3049507
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions jobspec/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,9 @@ func parseConstraints(result *[]*structs.Constraint, list *ast.ObjectList) error
}

if value, ok := m[structs.ConstraintDistinctHosts]; ok {
var enabled bool
var err error
switch value.(type) {
case string:
if enabled, err = strconv.ParseBool(value.(string)); err != nil {
return err
}
case bool:
enabled = value.(bool)
default:
return fmt.Errorf("distinct_hosts should be set to true or false; got %v", value)
enabled, err := parseBool(value)
if err != nil {
return fmt.Errorf("distinct_hosts should be set to true or false; %v", err)
}

// If it is not enabled, skip the constraint.
Expand All @@ -354,6 +346,23 @@ func parseConstraints(result *[]*structs.Constraint, list *ast.ObjectList) error
return nil
}

// parseBool takes an interface value and tries to convert it to a boolean and
// returns an error if the type can't be converted.
func parseBool(value interface{}) (bool, error) {
var enabled bool
var err error
switch value.(type) {
case string:
enabled, err = strconv.ParseBool(value.(string))
case bool:
enabled = value.(bool)
default:
err = fmt.Errorf("%v couldn't be converted to boolean value", value)
}

return enabled, err
}

func parseTasks(jobName string, taskGroupName string, result *[]*structs.Task, list *ast.ObjectList) error {
list = list.Children()
if len(list.Items) == 0 {
Expand Down

0 comments on commit 3049507

Please sign in to comment.