Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Accept bool strings for ArgBool #895

Merged
merged 3 commits into from
Apr 30, 2018
Merged

Conversation

shanson7
Copy link
Collaborator

Fixes #867 (for string case)

expr/parse.go Outdated

if strings.HasPrefix(e, "False") || strings.HasPrefix(e, "false") {
return &expr{bool: false, str: e[:5], etype: etBool}, e[5:], nil
if val, wasBool := strToBool(e); wasBool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm confused about how we support strings supplied by the user that have quotes.
why are we not comparing e to "\"False\"" anywhere for example?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose not to interpret all strings like 'false' and "True" etc. as bools automatically, because things like alias could very well be passed 'false' as a string value and intend for it to be a string.

Instead, I changed the ArgBool cases in expr.go to also accept strings that look like a bool

expr/expr.go Outdated
break
}
if got.etype == etString {
if val, wasBool := strToBool(got.str); wasBool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

golang idiom to use ok, not wasBool

},
namedArgs: map[string]*expr{
"key2": {etype: etString, str: "true"},
"key1": {etype: etString, str: "false"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we want these to be etBool ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At parse time, we don't know what type we want. We only know what type they are. Only when they are converted to expressions do we know what signature to apply and how to match them up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense

if val {
size = 4
}
return &expr{bool: val, str: e[:size], etype: etBool}, e[size:], nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is etype etBool here and not etString?

Copy link
Collaborator Author

@shanson7 shanson7 Apr 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is an unquoted boolean (e.g. false not 'false' or "false"). This makes it unambiguously an etBool

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah you got me. You know this code better now than I do 😄

expr/expr.go Outdated
break
}
if got.etype == etString {
if val, wasBool := strToBool(got.str); wasBool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about we make strToBool a method on expr ? we just comment that it's only defined for etString types.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the same logic in parse.go on the string e.

Copy link
Contributor

@Dieterbe Dieterbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks solid Sean. thank you very much :)

@Dieterbe Dieterbe merged commit 0428430 into grafana:master Apr 30, 2018
@shanson7 shanson7 deleted the feature_strBool branch August 9, 2018 15:28
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants