Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

luci-base: add uci.get_bool to allow cleanup of app code #7612

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

efahl
Copy link
Contributor

@efahl efahl commented Feb 8, 2025

Any number of apps read boolean values from configuration files, then use various inconsistent means for checking truth values. The get_bool function allows app authors to fetch the value without regard for how it is represented in the config file.

For example, this

    let enabled = uci.get('system', 'ntp', 'enable_server');
    if (enabled == '1') ...

could become the more natural

    let enabled = uci.get_bool('system', 'ntp', 'enable_server');
    if (enabled) ...

Any number of apps read boolean values from configuration files, then use
various inconsistent means for checking truth values.  The get_bool function
allows app authors to fetch the value without regard for how it is represented
in the config file.

For example, this

    let enabled = uci.get('system', 'ntp', 'enable_server');
    if (enabled == '1') ...

could become the more natural

    let enabled = uci.get_bool('system', 'ntp', 'enable_server');
    if (enabled) ...

Signed-off-by: Eric Fahlgren <ericfahlgren@gmail.com>
get_bool(conf, type, opt) {
let value = this.get(conf, type, opt);
if (typeof(value) == 'string')
return ['1', 'on', 'true', 'yes'].includes(value.toLowerCase());
Copy link
Contributor

Choose a reason for hiding this comment

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

missing 'enabled'

Copy link
Contributor

Choose a reason for hiding this comment

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

enabled is a parameter name. I've not seen any instances that use = "enabled", and I don't think its use should be encouraged.

Copy link
Contributor

Choose a reason for hiding this comment

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

it may be some feature = 'enabled'. I agree that this should be avoided, just want to have it consistent with UCI. Still not a problem, we may merge the PR.

let value = this.get(conf, type, opt);
if (typeof(value) == 'string')
return ['1', 'on', 'true', 'yes'].includes(value.toLowerCase());
return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

a Number 1 will be converted to false

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But get guarantees null, string or stuff we don't care about, so should never see an int.

Copy link
Contributor

Choose a reason for hiding this comment

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

great

@stokito
Copy link
Contributor

stokito commented Feb 16, 2025

There is config_get_bool function that also accepts enabled as true value.
https://openwrt.org/docs/guide-developer/config-scripting#reading_booleans

Additionally it also would be nice to check the existing JS code to use the function. Interesting in how many places it would be useful.

Just a bikeshedding though. From a performance standpoint:

  • ['1', 'on', 'true', 'yes'] allocation of an array
  • value.toLowerCase() allocates another chunk of memory. Maybe the lower case is not needed.
  • includes() linear search

Maybe the regexp will be faster, it may use KMP search, and maybe even easier to understand.
In fact something simpler than regexp may be used like a substring search '1|on|true|enabled|'.includes(opt + '|').

The JavaScript itself has Boolan().
It converts any non 0 to true, not just 1. But it will also convert any non-empty string to true, including '0'.

@efahl
Copy link
Contributor Author

efahl commented Feb 22, 2025

Just a bikeshedding though. From a performance standpoint:

Yeah, I completely ignored any performance considerations. I figure this is always running on a "big" client with CPU and RAM to spare, so my only real thought was to keep the JS footprint small so it takes up less device space...

Copy link
Contributor

@stokito stokito left a comment

Choose a reason for hiding this comment

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

let's merge it

@stangri
Copy link
Member

stangri commented Feb 23, 2025

There is config_get_bool function that also accepts enabled as true value.
https://openwrt.org/docs/guide-developer/config-scripting#reading_booleans

Thanks for pointing that out @stokito! Would be ideal if the js function was consistent with the shell function in accepting enabled as true value. Otherwise it's a bug and why knowingly merge code which introduces the bug? My 2c.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants