Skip to content

Commit

Permalink
Support arbritrary static bindings object
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Kurait <[email protected]>
  • Loading branch information
AndreKurait committed Jan 14, 2025
1 parent 9a48175 commit 5b6fc2a
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import java.util.Map;
import java.util.function.Function;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;

public class JsonJSTransformerProvider implements IJsonTransformerProvider {

public static final String INITIALIZATION_SCRIPT = "initializationScript";
public static final String BINDINGS_PROVIDER = "bindingsProvider";
public static final String BINDINGS_OBJECT = "bindingsObject";

/**
* Validates and retrieves a configuration map, ensuring it contains required keys.
Expand Down Expand Up @@ -51,10 +53,19 @@ protected String getConfigUsageStr(String... requiredKeys) {
@SneakyThrows
@Override
public IJsonTransformer createTransformer(Object jsonConfig) {
var config = validateAndExtractConfig(jsonConfig, new String[]{INITIALIZATION_SCRIPT}, new String[]{BINDINGS_PROVIDER});
var config = validateAndExtractConfig(jsonConfig, new String[]{INITIALIZATION_SCRIPT}, new String[]{BINDINGS_OBJECT});

String script = (String) config.get(INITIALIZATION_SCRIPT);
Function<Object, Object> bindingsProvider = (Function<Object, Object>) config.get(BINDINGS_PROVIDER);
Function<Object, Object> bindingsProvider;
Object bindingsOutput;
ObjectMapper objectMapper = new ObjectMapper();
try {
String bindingsMapString = (String) config.get(BINDINGS_OBJECT);
bindingsOutput = objectMapper.readValue(bindingsMapString, new TypeReference<>() {});
bindingsProvider = (ignored) -> bindingsOutput;
} catch (Exception e) {
throw new RuntimeException("Failed to parse the bindings map", e);
}

if (script == null) {
throw new IllegalArgumentException("'script' must be provided.");
Expand Down

0 comments on commit 5b6fc2a

Please sign in to comment.