8000 Implement 'attr' filter · jinja2cpp/Jinja2Cpp@27230f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 27230f9

Browse files
committed
Implement 'attr' filter
1 parent 893e8b4 commit 27230f9

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

include/jinja2cpp/reflected_value.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ struct ContainerReflector
153153
{
154154
return GenericList([accessor = PtrItemAccessor<T>(cont)]() {return &accessor;});
155155
}
156+
157+
template<typename T>
158+
static Value CreateFromPtr(std::shared_ptr<T> cont)
159+
{
160+
return GenericList([ptr = std::move(cont), accessor = PtrItemAccessor<T>(cont.get())]() {return &accessor;});
161+
}
156162
};
157163

158164
template<typename T>
@@ -197,7 +203,7 @@ struct Reflector<T, IsReflectedType<T>>
197203

198204
static auto CreateFromPtr(std::shared_ptr<T> val)
199205
{
200-
return GenericMap([accessor = ReflectedMapImpl<T>(val.get())]() {return &accessor;});
206+
return GenericMap([ptr = val, accessor = ReflectedMapImpl<T>(val.get())]() {return &accessor;});
201207
}
202208
};
203209

src/filters.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@ InternalValue Sort::Filter(const InternalValue& baseVal, RenderContext& context)
9292

9393
Attribute::Attribute(FilterParams params)
9494
{
95-
95+
ParseParams({{"name", true}}, params);
9696
}
9797

9898
InternalValue Attribute::Filter(const InternalValue& baseVal, RenderContext& context)
9999
{
100-
return InternalValue();
100+
InternalValue attrNameVal = GetArgumentValue("name", context);
101+
return Subscript(baseVal, attrNameVal);
101102
}
102103

103104
Default::Default(FilterParams params)

test/filters_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,14 @@ INSTANTIATE_TEST_CASE_P(Unique, ListIteratorTest, ::testing::Values(
149149
InputOutputPair{"[3.0, 3, 1] | unique", "3, 1"}
150150
// InputOutputPair{"reflectedList | unique(attribute='strValue')", "test string 0test string 1test string 2test string 3test string 4test string 5test string 6test string 7test string 8test string 9"}
151151
));
152+
153+
INSTANTIATE_TEST_CASE_P(Attr, FilterGenericTest, ::testing::Values(
154+
InputOutputPair{"{'key'='itemName', 'value'='itemValue'} | attr('key')", "itemName"},
155+
InputOutputPair{"mapValue | attr('intVal')", "10"},
156+
InputOutputPair{"mapValue | attr(name='dblVal')", "100.5"},
157+
InputOutputPair{"mapValue | attr('stringVal')", "string100.5"},
158+
InputOutputPair{"mapValue | attr('boolValue')", "true"},
159+
InputOutputPair{"reflectedVal | attr('intValue')", "0"},
160+
InputOutputPair{"filledReflectedPtrVal | attr('strValue')", "test string 0"}
161+
));
162+

0 commit comments

Comments
 (0)
0