Skip to content

Commit

Permalink
Change algorithm for ToBool function.
Browse files Browse the repository at this point in the history
Signed-off-by: aliwoto <[email protected]>
  • Loading branch information
aliwoto committed May 28, 2022
1 parent 0cd5fd4 commit fa1c331
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
7 changes: 1 addition & 6 deletions ssg/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,7 @@ func RunCommandAsyncWithChan(command string, finishedChan chan bool) *ExecuteCom
}

func ToBool(str string) bool {
str = strings.ToLower(strings.TrimSpace(str))
if str == LowerYes || str == LowerTrueStr || str == LowerOnStr {
return true
}

return false
return strongParser.BoolMapping[strings.ToLower(strings.TrimSpace(str))]
}

func ToBase10(value int64) string {
Expand Down
10 changes: 5 additions & 5 deletions ssg/strongParser/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func parseFinalConfigBySection(v any, section string, configValue *ConfigParser)
for _, envTry := range envTries {
envValue := os.Getenv(envTry)
if envValue != "" {
theValue, found = boolMapping[envValue]
theValue, found = BoolMapping[envValue]
if found {
// second try: read it from env.
currentField.SetBool(theValue)
Expand All @@ -453,7 +453,7 @@ func parseFinalConfigBySection(v any, section string, configValue *ConfigParser)
continue
}

theValue, found = boolMapping[fByName.Tag.Get("default")]
theValue, found = BoolMapping[fByName.Tag.Get("default")]
if found {
// third try: from default value.
currentField.SetBool(theValue)
Expand Down Expand Up @@ -851,7 +851,7 @@ func parseFinalConfig(v any, configValue *ConfigParser) error {
for _, envTry := range envTries {
envValue := os.Getenv(envTry)
if envValue != "" {
theValue, found = boolMapping[envValue]
theValue, found = BoolMapping[envValue]
if found {
// second try: read it from env.
currentField.SetBool(theValue)
Expand All @@ -864,7 +864,7 @@ func parseFinalConfig(v any, configValue *ConfigParser) error {
continue
}

theValue, found = boolMapping[fByName.Tag.Get("default")]
theValue, found = BoolMapping[fByName.Tag.Get("default")]
if found {
// third try: from default value.
currentField.SetBool(theValue)
Expand Down Expand Up @@ -1530,7 +1530,7 @@ func parseToBoolArray(value string) []bool {

for i := 0; i < len(arr); i++ {
arr[i] = strings.TrimSpace(arr[i])
theValue, found := boolMapping[strings.ToLower(arr[i])]
theValue, found := BoolMapping[strings.ToLower(arr[i])]
if !found {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion ssg/strongParser/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (p *ConfigParser) GetBool(section, option string) (bool, error) {

result = strings.ToLower(result)

booleanValue, present := boolMapping[result]
booleanValue, present := BoolMapping[result]
if !present {
return false, fmt.Errorf("not a boolean: '%s'", result)
}
Expand Down
28 changes: 18 additions & 10 deletions ssg/strongParser/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,26 @@ var (
sectionHeader = regexp.MustCompile(`\[([^]]+)\]`)
keyValue = regexp.MustCompile(`([^:=\s][^:=]*)\s*(?P<vi>[:=])\s*(.*)$`)
//continuationLine = regexp.MustCompile(`\w+(.*)$`)
//interpolater = regexp.MustCompile(`%\(([^)]*)\)s`)
//interpolator = regexp.MustCompile(`%\(([^)]*)\)s`)
)

var boolMapping = map[string]bool{
"1": true,
"true": true,
"on": true,
"yes": true,
"0": false,
"false": false,
"off": false,
"no": false,
// BoolMapping is a map of strings to bool.
// WARNING: This is not a safe map, it should remain read-only.
var BoolMapping = map[string]bool{
"1": true,
"true": true,
"on": true,
"yes": true,
"y": true,
"enable": true,
"enabled": true,
"0": false,
"false": false,
"off": false,
"no": false,
"n": false,
"disable": false,
"disabled": false,
}

var invalidReflectValue = reflect.ValueOf(nil)

0 comments on commit fa1c331

Please sign in to comment.