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 5 commits
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
21 changes: 18 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,35 @@
public class LazyExpression implements Supplier {
private final Supplier supplier;
private final String image;
private final Memoization memoization;
private Object jsonValue = null;

private LazyExpression(Supplier supplier, String image) {
public enum Memoization {
ON,
OFF
}

private LazyExpression(Supplier supplier, String image, Memoization memoization) {
this.supplier = supplier;
this.image = image;
this.memoization = memoization;
}

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

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

@Override
public Object get() {
if (jsonValue == null) {
if (jsonValue == null || memoization == Memoization.OFF) {
jsonValue = supplier.get();
}
return jsonValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
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 com.hubspot.jinjava.interpret.LazyExpression.Memoization;
import java.util.List;
import org.junit.Test;

public class LazyExpressionTest {
Expand All @@ -29,4 +32,31 @@ 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, "", Memoization.OFF);
expression.get();
expression.get();
verify(mock, times(2)).isEmpty();
}

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