Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement PyMap.get with the optional default.
  • Loading branch information
ccutrer committed Feb 5, 2025
commit db55adc791c238761f574c1f41503575b671a2fc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ protected Map<String, Object> delegate() {
return map;
}

public Object get(String key, Object defaultValue) {
if (!map.containsKey(key)) {
return defaultValue;
}
return map.get(key);
Comment thread
ccutrer marked this conversation as resolved.
Outdated
}

@Override
public Object put(String s, Object o) {
if (o == this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,18 @@ public void itGetsKeysWithVariableName() {
.isEqualTo("value1");
}

@Test
public void itSupportsGetWithOptionalDefault() {
assertThat(
jinjava.render(
"{% set test = {\"key1\": \"value1\"} %}" +
"{{ test.get(\"key2\", \"default\") }}",
Collections.emptyMap()
)
)
.isEqualTo("default");
}

@Test
public void itFallsBackUnknownVariableNameToString() {
assertThat(
Expand Down