Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Make LazyExpression memoization disable-able #673

Merged
merged 6 commits into from
May 27, 2021
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
12 changes: 9 additions & 3 deletions src/main/java/com/hubspot/jinjava/interpret/LazyExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@
public class LazyExpression implements Supplier {
private final Supplier supplier;
private final String image;
private final boolean memoize;
private Object jsonValue = null;

private LazyExpression(Supplier supplier, String image) {
private LazyExpression(Supplier supplier, String image, boolean memoize) {
jmaroeder marked this conversation as resolved.
Show resolved Hide resolved
this.supplier = supplier;
this.image = image;
this.memoize = memoize;
}

public static LazyExpression of(Supplier supplier, String image) {
return new LazyExpression(supplier, image);
return new LazyExpression(supplier, image, true);
}

public static LazyExpression of(Supplier supplier, String image, boolean memoize) {
return new LazyExpression(supplier, image, false);
jmaroeder marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public Object get() {
if (jsonValue == null) {
if (jsonValue == null || !memoize) {
jsonValue = supplier.get();
}
return jsonValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.hubspot.jinjava.interpret;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import java.util.List;
import org.junit.Test;

public class LazyExpressionTest {
Expand All @@ -29,4 +31,22 @@ public void itSerializesNonEvaluatedValueToEmpty() throws JsonProcessingExceptio
);
assertThat(new ObjectMapper().writeValueAsString(expression)).isEqualTo("\"\"");
}

@Test
public void itMemoizesByDefault() {
List mock = mock(List.class);
LazyExpression expression = LazyExpression.of(mock::isEmpty, "");
expression.get();
expression.get();
verify(mock, times(1)).isEmpty();
jmaroeder marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void itAllowsMemoizationToBeDisabled() {
List mock = mock(List.class);
LazyExpression expression = LazyExpression.of(mock::isEmpty, "", false);
expression.get();
expression.get();
verify(mock, times(2)).isEmpty();
}
}