qgis / QGIS

QGIS is a free, open source, cross platform (lin/win/mac) geographical information system (GIS)
https://qgis.org
GNU General Public License v2.0
10.67k stars 3.02k forks source link

[expression] Add a to_bool() function to convert various value types into a boolean #59488

Closed nirvn closed 1 week ago

nirvn commented 1 week ago

Description

Title says it all. The PR adds a useful function to convert various value types into a boolean.

Required for a follow up PR to add support for mapbox gl's to-boolean expression when importing vector tile styles (https://docs.mapbox.com/style-spec/reference/expressions/#types-to-boolean)

github-actions[bot] commented 1 week ago

🪟 Windows builds

Download Windows builds of this PR for testing. Debug symbols for this build are available here. (Built from commit f6302256d441546be90b6643d8100545d1ab1d35)

Gustry commented 1 week ago

Nice addition. Indeed it was indeed missing ;-)

According to QVariant::toBool(), it takes care of false. Maybe it would nice for users to have the example of to_bool('false') ?

Question, would it be acceptable to move this logic into QgsStringUtils ? (or similar class) So it's available in Python. I see some plugins having a to_bool function as well.

nirvn commented 1 week ago

@Gustry , ATM, I'm matching the behavior to that of the mapboxgl spec. Although it does not explicitly say 'false' is true, it says a non-empty string is true, which 'false' would be.

We also don't follow the QVariant::toBool for '0' (which returns false, but we want to return true here).