Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
elisherer committed Jul 27, 2024
1 parent 99f5308 commit 8e2edb3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public TransformerFunctionAnd(JsonAdapter<JE, JA, JO> adapter) {
@Override
public Object apply(FunctionContext<JE, JA, JO> context) {
var value = context.getJsonElementStreamer(null);
if (value == null) {
return false;
}
return value.stream().allMatch(adapter::isTruthy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import co.nlighten.jsontransform.functions.annotations.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

Expand All @@ -35,21 +34,17 @@ public Object apply(FunctionContext<JE, JA, JO> context) {
if (streamer == null || streamer.knownAsEmpty())
return null;
var by = context.getJsonElement( "by", false);
var def = Objects.requireNonNullElse(context.getBigDecimal("default"), BigDecimal.ZERO);
var _default = Objects.requireNonNullElse(context.getBigDecimal("default"), BigDecimal.ZERO);
var size = new AtomicInteger(0);
var identity = BigDecimal.valueOf(0, FunctionHelpers.MAX_SCALE);
var result = streamer.stream()
.map(t -> {
size.getAndIncrement();
var res = !adapter.isNull(by) ? context.transformItem(by, t) : t;
return adapter.isNull(res) ? def : adapter.getNumberAsBigDecimal(res);
return adapter.isNull(res) ? _default : adapter.getNumberAsBigDecimal(res);
})
.reduce(BigDecimal.ZERO, BigDecimal::add)
.divide(BigDecimal.valueOf(size.get()), RoundingMode.HALF_UP);

// cap scale at max
if (result.scale() > FunctionHelpers.MAX_SCALE) {
result = result.setScale(FunctionHelpers.MAX_SCALE, FunctionHelpers.MAX_SCALE_ROUNDING);
}
.reduce(identity, BigDecimal::add)
.divide(BigDecimal.valueOf(size.get()), FunctionHelpers.MAX_SCALE_ROUNDING);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@ public class TransformerFunctionAvgTest extends BaseTest {
@Test
void inline() {
var arr = new Object[] {4,2,13.45, JsonNull.INSTANCE};
assertTransformation(arr, "$$avg():$", fromJson("4.86"));
assertTransformation(arr, "$$avg(1):$", fromJson("5.11"));
assertTransformation(arr, "$$avg():$", fromJson("4.8625"));
assertTransformation(arr, "$$avg(1):$", fromJson("5.1125"));
}

static class holder{
static class Holder {
final BigDecimal value;

holder(BigDecimal value) {
Holder(BigDecimal value) {
this.value = value;
}
}
@Test
void object() {
var arr = new holder[] {new holder(new BigDecimal(4)),new holder( new BigDecimal(2)),new holder( new BigDecimal("13.45")), new holder(null)};
var arr = new Holder[] {new Holder(new BigDecimal(4)),new Holder( new BigDecimal(2)),new Holder( new BigDecimal("13.45")), new Holder(null)};


assertTransformation(arr, fromJson("""
{
"$$avg":"$","by":"##current.value"
}
"""), fromJson("4.86"));
"""), fromJson("4.8625"));

assertTransformation(arr, fromJson("""
{
"$$avg":"$","by":"##current.value","default":1
}
"""), fromJson("5.11"));
"""), fromJson("5.1125"));

}

Expand Down

0 comments on commit 8e2edb3

Please sign in to comment.