Skip to content

Commit

Permalink
feat: add data map on TransformContext (eclipse-edc#4120)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt authored Apr 17, 2024
1 parent 49993d5 commit 4fd16b8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

public class TransformerContextImpl implements TransformerContext {
private final List<String> problems = new ArrayList<>();
private final TypeTransformerRegistry registry;
private final Map<Class<?>, Map<String, AtomicReference<?>>> data = new HashMap<>();

public TransformerContextImpl(TypeTransformerRegistry registry) {
this.registry = registry;
Expand Down Expand Up @@ -70,4 +75,18 @@ public Class<?> typeAlias(String type, Class<?> defaultType) {
return registry.typeAlias(type, defaultType);
}

@Override
public void setData(Class<?> type, String key, Object value) {
data.computeIfAbsent(type, t -> new HashMap<>()).put(key, new AtomicReference<>(value));
}

@Override
public Object consumeData(Class<?> type, String key) {
return Optional.of(type)
.map(data::get)
.map(typeMap -> typeMap.get(key))
.map(reference -> reference.getAndSet(null))
.orElse(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

package org.eclipse.edc.transform;

import org.eclipse.edc.transform.spi.TransformerContext;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -26,8 +28,8 @@

class TransformerContextImplTest {

private final TypeTransformerRegistry registry = mock(TypeTransformerRegistry.class);
private final TransformerContextImpl context = new TransformerContextImpl(registry);
private final TypeTransformerRegistry registry = mock();
private final TransformerContext context = new TransformerContextImpl(registry);

@Test
void shouldReturnTransformedInput() {
Expand Down Expand Up @@ -55,4 +57,43 @@ void shouldNotTransform_whenInputIsNull() {
assertThat(result).isNull();
verifyNoInteractions(registry);
}

@Nested
class Data {
@Test
void shouldClearData_whenConsumed() {
context.setData(Integer.class, "key", "value");

var data = context.consumeData(Integer.class, "key");

assertThat(data).isEqualTo("value");
assertThat(context.consumeData(Integer.class, "key")).isNull();
}

@Test
void shouldReturnNull_whenKeyDoesNotExist() {
context.setData(Integer.class, "key", "value");

var data = context.consumeData(Integer.class, "unexistent-key");

assertThat(data).isNull();
}

@Test
void shouldReturnNull_whenTypeDoesNotExist() {
var data = context.consumeData(Double.class, "any");

assertThat(data).isNull();
}

@Test
void shouldPermitMultipleDataPerType() {
context.setData(Integer.class, "key", "value");
context.setData(Integer.class, "another-key", "another-value");

var data = context.consumeData(Integer.class, "key");

assertThat(data).isEqualTo("value");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,22 @@ default Class<?> typeAlias(String type) {
default Class<?> typeAlias(String type, Class<?> defaultType) {
return defaultType;
}

/**
* Set context data to be consumed by the transformer operating on type.
*
* @param type the type of the transformer that will consume the data.
* @param key the key.
* @param value the value.
*/
void setData(Class<?> type, String key, Object value);

/**
* Consume context data. After consuming the data will be cleared.
*
* @param type the type of the transformer that consumes the data.
* @param key the key.
* @return the value.
*/
Object consumeData(Class<?> type, String key);
}

0 comments on commit 4fd16b8

Please sign in to comment.