Skip to content

Commit

Permalink
Context builder fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tfij committed Sep 27, 2016
1 parent f29bb51 commit da6f098
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ opel supports:
- registrable functions (i.e. `myFunction('Hello, World!')`)
- registrable constant values (i.e. `'Hello, ' + WORLD_VALUE`)

More can be found in [documentation](https://github.com/allegro/opel/wiki).

## Using with Gradle

Basically, all you have to do is to add a compile dependency:
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/pl/allegro/tech/opel/EvalContextBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ public class EvalContextBuilder {
private Optional<EvalContext> externalEvalContext = Optional.empty();

static EvalContext fromMaps(Map<String, CompletableFuture<Object>> values, Map<String, OpelAsyncFunction<?>> functions) {
Map<String, CompletableFuture<Object>> copiedValues = new HashMap<>(values);
Map<String, OpelAsyncFunction<?>> copiedFunctions = new HashMap<>(functions);
return new EvalContext() {
@Override
public Optional<OpelAsyncFunction<?>> getFunction(String name) {
return Optional.ofNullable(functions.get(name));
return Optional.ofNullable(copiedFunctions.get(name));
}

@Override
public Optional<CompletableFuture<?>> getValue(String name) {
return Optional.ofNullable(values.get(name));
return Optional.ofNullable(copiedValues.get(name));
}
};
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/groovy/pl/allegro/tech/opel/EvalContextBuilderSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,21 @@ class EvalContextBuilderSpec extends Specification {
context.getValue('v').get().get() == 'value1'
context.getFunction('f').get().apply([]).get() == 'from fun 1'
}

def "adding new values should not change built context"() {
given:
def builder = create()
.withCompletedValue('a', 'a')
def context1 = builder.build()

when:
builder.withCompletedValue('b', 'b')
def context2 = builder.build()

then:
context1.getValue('a').isPresent()
!context1.getValue('b').isPresent()
context2.getValue('a').isPresent()
context2.getValue('b').isPresent()
}
}

0 comments on commit da6f098

Please sign in to comment.