Fix boolean value parsing to return Python booleans#102
Open
owlas wants to merge 2 commits intojoshtemple:masterfrom
Open
Fix boolean value parsing to return Python booleans#102owlas wants to merge 2 commits intojoshtemple:masterfrom
owlas wants to merge 2 commits intojoshtemple:masterfrom
Conversation
This commit fixes issue joshtemple#15 by implementing proper boolean parsing in the LKML parser. Changes: - Modified DictVisitor.visit_token() to convert unquoted boolean strings (yes/no/true/false) to Python bool objects - Preserved quoted boolean strings (e.g., "yes", "true") as string values - Updated DictParser.parse_any() to handle boolean type during serialization - Added comprehensive test coverage for boolean parsing scenarios The parser now correctly: - Converts `hidden: yes` to Python `True` - Converts `primary_key: no` to Python `False` - Preserves `label: "yes"` as string `"yes"` - Handles case-insensitive boolean values (YES, No, TRUE, False) - Supports round-trip serialization Fixes joshtemple#15
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes #15 by implementing proper boolean parsing in the LKML parser. Boolean values like
yes,no,true, andfalseare now correctly converted to Pythonboolobjects instead of remaining as strings.Changes
Core Implementation
Modified
DictVisitor.visit_token()inlkml/simple.pyto:QuotedSyntaxToken) - if so, preserve as stringyes/no/true/false) to PythonboolobjectsUpdated
DictParser.parse_any()to handle boolean type during serialization:boolto accepted typesyes/nofor LKML outputTest Coverage
Added comprehensive tests in
tests/test_simple.py:test_boolean_parsing_yes_no- Basic yes/no conversiontest_boolean_parsing_true_false- Basic true/false conversiontest_boolean_parsing_case_insensitive- Case insensitive handlingtest_boolean_parsing_non_boolean_strings- Non-boolean strings remain unchangedtest_boolean_round_trip- Round-trip serialization works correctlytest_quoted_boolean_strings_remain_strings- Quoted values remain as stringstest_mixed_quoted_unquoted_booleans- Mixed quoted/unquoted in same structuretest_filter_with_quoted_boolean_values- Filter blocks with quoted booleanstest_case_sensitivity_with_quoted_values- Case preservation for quoted valuestest_edge_cases_quoted_vs_unquoted- Edge cases and empty stringsExamples
Before (incorrect):
After (correct):
Behavior Summary
hidden: yes→True(bool)primary_key: no→False(bool)suggestions: true→True(bool)can_filter: false→False(bool)label: "yes"→"yes"(str)value: "true"→"true"(str)Fixes #15