Skip to content

Commit

Permalink
Java 0.2.5 - Make switch function support lazy evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
elisherer committed Aug 22, 2024
1 parent 767a239 commit 258903d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 14 deletions.
2 changes: 1 addition & 1 deletion java/json-transform/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group 'co.nlighten'
version = '0.2.4'
version = '0.2.5'

ext {
gsonVersion = "2.10.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Object apply(FunctionContext<JE, JA, JO> context) {
var cje = jArray.get(arr, 0);
if (adapter.isNull(cje)) {
condition = false;
} else if (context.isJsonBoolean(cje)) {
} else if (adapter.isJsonBoolean(cje)) {
condition = adapter.getBoolean(cje);
} else {
condition = adapter.isTruthy(context.transform(cje));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Object apply(FunctionContext<JE, JA, JO> context) {
JE that = null;
// if operator is not in/nin then prepare the "that" argument which is a JsonElement
if (!Objects.equals(op, "IN") && !Objects.equals(op, "NIN")) {
that = context.isJsonNumber(value)
that = adapter.isJsonNumber(value)
? nullableBigDecimalJsonPrimitive(context, context.getBigDecimal("that"))
: context.getJsonElement("that");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ public TransformerFunctionSwitch(JsonAdapter<JE, JA, JO> adapter) {
public Object apply(FunctionContext<JE, JA, JO> context) {
var alias = context.getAlias();
var value = context.getString(null);
var caseMapEl = context.getJsonElement("cases");
var transformed = false;
var caseMapEl = context.getJsonElement("cases", false);
if (adapter.isJsonString(caseMapEl)) {
caseMapEl = context.transform(caseMapEl);
transformed = true;
}
if (!jObject.is(caseMapEl)) {
logger.warn("{}.cases was not specified with an object as case map", alias);
return null;
}
var caseMap = jObject.convert(caseMapEl);
return jObject.has(caseMap, value)
? jObject.get(caseMap, value)
: context.getJsonElement("default");
if (!jObject.has(caseMap, value)) {
return context.getJsonElement("default");
}
var caseValue = jObject.get(caseMap, value);
return transformed ? caseValue : context.transform(caseValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,6 @@ public Object get(String name) {
public boolean isNull(JE value) {
return adapter.isNull(value);
}
public boolean isJsonNumber(JE value) {
return adapter.isJsonNumber(value);
}
public boolean isJsonBoolean(JE value) {
return adapter.isJsonBoolean(value);
}
public JE wrap(Object value) {
return adapter.wrap(value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package co.nlighten.jsontransform.functions;

import co.nlighten.jsontransform.BaseTest;
import co.nlighten.jsontransform.adapters.gson.GsonJsonTransformer;
import co.nlighten.jsontransform.functions.common.FunctionContext;
import co.nlighten.jsontransform.functions.common.TransformerFunction;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class TransformerFunctionSwitchTest extends BaseTest {
@Test
void testObjectFunctionSwitch() {
void object() {
assertTransformation("B",fromJson("""
{
"$$switch": "$",
Expand Down Expand Up @@ -46,4 +56,38 @@ void inline() {
assertTransformation(fromJson("{ \"a\": 1, \"B\": 2, \"c\": 3 }"),
"$$switch($,'$$decimal:4'):C", fromJson("4"));
}


@Test
void lazyEval() {
var transformer = fromJson("""
{
"$$switch": "$",
"cases": {
"a": "$$switchLazyEval:1",
"B": "$$switchLazyEval:2",
"c": "$$switchLazyEval:3"
}
}
""");

var callCount = new AtomicInteger();
GsonJsonTransformer.FUNCTIONS.registerFunctions(Map.entry("switchLazyEval", new TransformerFunction<>(GsonJsonTransformer.ADAPTER) {
@Override
public Object apply(FunctionContext<JsonElement, JsonArray, JsonObject> context) {
callCount.incrementAndGet();
return context.get(null);
}
}));

callCount.set(0);
assertTransformation("B", transformer, "2");
// assert that "c" was called twice (check A, check B, break)
Assertions.assertEquals(1, callCount.get());

callCount.set(0);
assertTransformation("c", transformer, "3");
// assert that "c" was called once (check A, break)
Assertions.assertEquals(1, callCount.get());
}
}

0 comments on commit 258903d

Please sign in to comment.