Skip to content
New issue

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

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

Already on GitHub? # to your account

Length filter does not work directly on strings #273

Open
injeniero opened this issue Oct 28, 2024 · 1 comment
Open

Length filter does not work directly on strings #273

injeniero opened this issue Oct 28, 2024 · 1 comment

Comments

@injeniero
Copy link

injeniero commented Oct 28, 2024

In jinja2, you can apply the length filter directly on strings like: {{ my_string | length }}, this will produce an empty value in jinja2Cpp.

The fix seems to be as simple as changing filters.cpp:457

diff --git a/src/filters.cpp b/src/filters.cpp
--- a/src/filters.cpp
+++ b/src/filters.cpp
@@ -454,7 +454,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
     InternalValue result;
 
     bool isConverted = false;
-    ListAdapter list = ConvertToList(baseVal, isConverted);
+    ListAdapter list = ConvertToList(baseVal, isConverted, false);
 
     if (!isConverted)
         return result;

And adding a test:

diff --git a/test/filters_test.cpp b/test/filters_test.cpp
--- a/test/filters_test.cpp
+++ b/test/filters_test.cpp
@@ -161,6 +161,7 @@ INSTANTIATE_TEST_SUITE_P(Length, FilterGenericTest, ::testing::Values(
                             InputOutputPair{"intValue | length", ""},
                             InputOutputPair{"intList | length", "10"},
                             InputOutputPair{"stringValue | list | length", "4"},
+                            InputOutputPair{"stringValue | length", "4"},
                             InputOutputPair{"reflectedIntVector | length", "10"}
                             ));
@injeniero
Copy link
Author

I run the test suit with these changes and it worked.

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

No branches or pull requests

1 participant