Skip to content

Commit

Permalink
re-wrote i18n corcing asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
bbakerman committed Mar 7, 2024
1 parent 254009c commit bae150f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/main/java/graphql/scalar/GraphqlBooleanCoercing.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.math.BigDecimal;
import java.util.Locale;

import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertShouldNeverHappen;
import static graphql.scalar.CoercingUtil.i18nMsg;
import static graphql.scalar.CoercingUtil.isNumberIsh;
Expand Down Expand Up @@ -88,7 +87,10 @@ private static boolean parseLiteralImpl(@NotNull Object input, @NotNull Locale l

@NotNull
private BooleanValue valueToLiteralImpl(@NotNull Object input, @NotNull Locale locale) {
Boolean result = assertNotNull(convertImpl(input), () -> i18nMsg(locale, "Boolean.notBoolean", typeName(input)));
Boolean result = convertImpl(input);
if (result == null) {
assertShouldNeverHappen(i18nMsg(locale, "Boolean.notBoolean", typeName(input)));
}
return BooleanValue.newBooleanValue(result).build();
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/graphql/scalar/GraphqlFloatCoercing.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.math.BigDecimal;
import java.util.Locale;

import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertShouldNeverHappen;
import static graphql.scalar.CoercingUtil.i18nMsg;
import static graphql.scalar.CoercingUtil.isNumberIsh;
import static graphql.scalar.CoercingUtil.typeName;
Expand Down Expand Up @@ -89,7 +89,10 @@ private static double parseLiteralImpl(@NotNull Object input, @NotNull Locale lo

@NotNull
private FloatValue valueToLiteralImpl(Object input, @NotNull Locale locale) {
Double result = assertNotNull(convertImpl(input), () -> i18nMsg(locale, "Float.notFloat", typeName(input)));
Double result = convertImpl(input);
if (result == null) {
assertShouldNeverHappen(i18nMsg(locale, "Float.notFloat", typeName(input)));
}
return FloatValue.newFloatValue(BigDecimal.valueOf(result)).build();
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/graphql/scalar/GraphqlIDCoercing.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.UUID;

import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertShouldNeverHappen;
import static graphql.scalar.CoercingUtil.i18nMsg;
import static graphql.scalar.CoercingUtil.typeName;

Expand Down Expand Up @@ -84,7 +85,10 @@ private String parseLiteralImpl(Object input, @NotNull Locale locale) {

@NotNull
private StringValue valueToLiteralImpl(Object input, @NotNull Locale locale) {
String result = assertNotNull(convertImpl(input), () -> i18nMsg(locale, "ID.notId", typeName(input)));
String result = convertImpl(input);
if (result == null) {
assertShouldNeverHappen(i18nMsg(locale, "ID.notId", typeName(input)));
}
return StringValue.newStringValue(result).build();
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/graphql/scalar/GraphqlIntCoercing.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.math.BigInteger;
import java.util.Locale;

import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertShouldNeverHappen;
import static graphql.scalar.CoercingUtil.i18nMsg;
import static graphql.scalar.CoercingUtil.isNumberIsh;
import static graphql.scalar.CoercingUtil.typeName;
Expand Down Expand Up @@ -91,7 +91,10 @@ private static int parseLiteralImpl(Object input, @NotNull Locale locale) {
}

private IntValue valueToLiteralImpl(Object input, @NotNull Locale locale) {
Integer result = assertNotNull(convertImpl(input),() -> i18nMsg(locale, "Int.notInt", typeName(input)));
Integer result = convertImpl(input);
if (result == null) {
assertShouldNeverHappen(i18nMsg(locale, "Int.notInt", typeName(input)));
}
return IntValue.newIntValue(BigInteger.valueOf(result)).build();
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/graphql/schema/GraphQLEnumType.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ public Value valueToLiteral(Object input) {
@Internal
public Value<?> valueToLiteral(Object input, GraphQLContext graphQLContext, Locale locale) {
GraphQLEnumValueDefinition enumValueDefinition = valueDefinitionMap.get(input.toString());
assertNotNull(enumValueDefinition, () -> i18nMsg(locale, "Enum.badName", name, input.toString()));
if (enumValueDefinition == null) {
assertShouldNeverHappen(i18nMsg(locale, "Enum.badName", name, input.toString()));
};
return EnumValue.newEnumValue(enumValueDefinition.getName()).build();

}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/benchmark/AssertBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class AssertBenchmark {

private static final int LOOPS = 100;
private static final Random random = new Random();
private static final boolean BOOL = new Random().nextBoolean();

@Benchmark
@BenchmarkMode(Mode.Throughput)
Expand Down Expand Up @@ -62,9 +62,9 @@ public void benchMarkAssertWithStringFormatted() {
}

private boolean jitTrue() {
int i = random.nextInt();
// can you jit this away, Mr JIT??
return i % 10 < -1 || i * 2 + 1 < -2;
//noinspection ConstantValue,SimplifiableConditionalExpression
return BOOL ? BOOL : !BOOL;
}

public static void main(String[] args) throws RunnerException {
Expand Down

0 comments on commit bae150f

Please sign in to comment.