-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to support real bool values
Signed-off-by: Mathieu Frenette <[email protected]>
- Loading branch information
Showing
17 changed files
with
148 additions
and
50 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 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package conversion | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
// ToString converts an abstract value (can be either string or bool) to its string representation | ||
func ToString(value interface{}) (string, error) { | ||
strValue, ok := value.(string) | ||
if ok { | ||
return strValue, nil | ||
} | ||
|
||
boolValue, ok := value.(bool) | ||
if ok { | ||
return strconv.FormatBool(boolValue), nil | ||
} | ||
|
||
return "", fmt.Errorf("failed to convert type into string: %t", value) | ||
} | ||
|
||
// ToBool converts an abstract value (can be either string or bool) to its string representation | ||
func ToBool(value interface{}) (bool, error) { | ||
boolValue, ok := value.(bool) | ||
if ok { | ||
return boolValue, nil | ||
} | ||
|
||
strValue, ok := value.(string) | ||
if ok { | ||
return strconv.ParseBool(strValue) | ||
} | ||
|
||
return false, fmt.Errorf("failed to convert type into bool: %t", value) | ||
} |
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,33 @@ | ||
package variables | ||
|
||
import "github.com/Samasource/jen/src/internal/helpers/conversion" | ||
|
||
// TryGetString tries to fetch given variable from map and return it as a string, | ||
// also returning whether the variable was successfully found and converted. | ||
func TryGetString(vars map[string]interface{}, name string) (string, bool) { | ||
value, ok := vars[name] | ||
if !ok { | ||
return "", false | ||
} | ||
|
||
str, err := conversion.ToString(value) | ||
if err != nil { | ||
return "", false | ||
} | ||
return str, true | ||
} | ||
|
||
// TryGetBool tries to fetch given variable from map and return it as a bool, | ||
// also returning whether the variable was successfully found and converted. | ||
func TryGetBool(vars map[string]interface{}, name string) (bool, bool) { | ||
value, ok := vars[name] | ||
if !ok { | ||
return false, false | ||
} | ||
|
||
b, err := conversion.ToBool(value) | ||
if err != nil { | ||
return false, false | ||
} | ||
return b, true | ||
} |
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.