Skip to content

Commit da0bd0e

Browse files
committed
now using Locale in the standard scalars
1 parent 89c8574 commit da0bd0e

12 files changed

Lines changed: 359 additions & 93 deletions
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
package graphql.scalar;
22

33
import graphql.Internal;
4+
import graphql.i18n.I18n;
5+
6+
import java.util.Locale;
47

58
@Internal
6-
class CoercingUtil {
7-
static boolean isNumberIsh(Object input) {
9+
public class CoercingUtil {
10+
public static boolean isNumberIsh(Object input) {
811
return input instanceof Number || input instanceof String;
912
}
1013

11-
static String typeName(Object input) {
14+
public static String typeName(Object input) {
1215
if (input == null) {
1316
return "null";
1417
}
1518

1619
return input.getClass().getSimpleName();
1720
}
21+
22+
public static String i18nMsg(Locale locale, String msgKey, Object... args) {
23+
return I18n.i18n(I18n.BundleType.Scalars, locale).msg(msgKey, args);
24+
}
1825
}

‎src/main/java/graphql/scalar/GraphqlBooleanCoercing.java‎

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
package graphql.scalar;
22

3+
import graphql.GraphQLContext;
34
import graphql.Internal;
5+
import graphql.execution.CoercedVariables;
46
import graphql.language.BooleanValue;
57
import graphql.language.Value;
68
import graphql.schema.Coercing;
79
import graphql.schema.CoercingParseLiteralException;
810
import graphql.schema.CoercingParseValueException;
911
import graphql.schema.CoercingSerializeException;
12+
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Nullable;
1014

1115
import java.math.BigDecimal;
16+
import java.util.Locale;
1217

1318
import static graphql.Assert.assertNotNull;
1419
import static graphql.Assert.assertShouldNeverHappen;
20+
import static graphql.scalar.CoercingUtil.i18nMsg;
1521
import static graphql.scalar.CoercingUtil.isNumberIsh;
1622
import static graphql.scalar.CoercingUtil.typeName;
1723

24+
/**
25+
* The deprecated methods still have implementations in case code outside graphql-java is calling them
26+
* but internally the call paths have been replaced.
27+
*/
1828
@Internal
1929
public class GraphqlBooleanCoercing implements Coercing<Boolean, Boolean> {
2030

@@ -45,41 +55,84 @@ private Boolean convertImpl(Object input) {
4555

4656
}
4757

48-
@Override
49-
public Boolean serialize(Object input) {
58+
@NotNull
59+
private Boolean serializeImpl(@NotNull Object input, @NotNull Locale locale) {
5060
Boolean result = convertImpl(input);
5161
if (result == null) {
5262
throw new CoercingSerializeException(
53-
"Expected type 'Boolean' but was '" + typeName(input) + "'."
63+
i18nMsg(locale, "Boolean.notBoolean", typeName(input))
5464
);
5565
}
5666
return result;
5767
}
5868

59-
@Override
60-
public Boolean parseValue(Object input) {
69+
@NotNull
70+
private Boolean parseValueImpl(@NotNull Object input, @NotNull Locale locale) {
6171
Boolean result = convertImpl(input);
6272
if (result == null) {
6373
throw new CoercingParseValueException(
64-
"Expected type 'Boolean' but was '" + typeName(input) + "'."
74+
i18nMsg(locale, "Boolean.notBoolean", typeName(input))
6575
);
6676
}
6777
return result;
6878
}
6979

70-
@Override
71-
public Boolean parseLiteral(Object input) {
80+
private static boolean parseLiteralImpl(@NotNull Object input, @NotNull Locale locale) {
7281
if (!(input instanceof BooleanValue)) {
7382
throw new CoercingParseLiteralException(
74-
"Expected AST type 'BooleanValue' but was '" + typeName(input) + "'."
83+
i18nMsg(locale, "Boolean.unexpectedAstType", typeName(input))
7584
);
7685
}
7786
return ((BooleanValue) input).isValue();
7887
}
7988

80-
@Override
81-
public Value valueToLiteral(Object input) {
82-
Boolean result = assertNotNull(convertImpl(input));
89+
@NotNull
90+
private BooleanValue valueToLiteralImpl(@NotNull Object input, @NotNull Locale locale) {
91+
Boolean result = assertNotNull(convertImpl(input), () -> i18nMsg(locale, "Boolean.notBoolean", typeName(input)));
8392
return BooleanValue.newBooleanValue(result).build();
8493
}
94+
95+
@Override
96+
@Deprecated
97+
public Boolean serialize(@NotNull Object dataFetcherResult) {
98+
return serializeImpl(dataFetcherResult, Locale.getDefault());
99+
}
100+
101+
@Override
102+
public @Nullable Boolean serialize(@NotNull Object dataFetcherResult, @NotNull GraphQLContext graphQLContext, @NotNull Locale locale) throws CoercingSerializeException {
103+
return serializeImpl(dataFetcherResult, locale);
104+
}
105+
106+
@Override
107+
@Deprecated
108+
public Boolean parseValue(@NotNull Object input) {
109+
return parseValueImpl(input, Locale.getDefault());
110+
}
111+
112+
@Override
113+
public Boolean parseValue(@NotNull Object input, @NotNull GraphQLContext graphQLContext, @NotNull Locale locale) throws CoercingParseValueException {
114+
return parseValueImpl(input, locale);
115+
}
116+
117+
@Override
118+
@Deprecated
119+
public Boolean parseLiteral(@NotNull Object input) {
120+
return parseLiteralImpl(input, Locale.getDefault());
121+
}
122+
123+
@Override
124+
public @Nullable Boolean parseLiteral(@NotNull Value<?> input, @NotNull CoercedVariables variables, @NotNull GraphQLContext graphQLContext, @NotNull Locale locale) throws CoercingParseLiteralException {
125+
return parseLiteralImpl(input, locale);
126+
}
127+
128+
@Override
129+
@Deprecated
130+
public Value valueToLiteral(@NotNull Object input) {
131+
return valueToLiteralImpl(input, Locale.getDefault());
132+
}
133+
134+
@Override
135+
public @NotNull Value<?> valueToLiteral(@NotNull Object input, @NotNull GraphQLContext graphQLContext, @NotNull Locale locale) {
136+
return valueToLiteralImpl(input, locale);
137+
}
85138
}
Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
package graphql.scalar;
22

3+
import graphql.GraphQLContext;
34
import graphql.Internal;
5+
import graphql.execution.CoercedVariables;
46
import graphql.language.FloatValue;
57
import graphql.language.IntValue;
68
import graphql.language.Value;
79
import graphql.schema.Coercing;
810
import graphql.schema.CoercingParseLiteralException;
911
import graphql.schema.CoercingParseValueException;
1012
import graphql.schema.CoercingSerializeException;
13+
import org.jetbrains.annotations.NotNull;
14+
import org.jetbrains.annotations.Nullable;
1115

1216
import java.math.BigDecimal;
17+
import java.util.Locale;
1318

1419
import static graphql.Assert.assertNotNull;
20+
import static graphql.scalar.CoercingUtil.i18nMsg;
1521
import static graphql.scalar.CoercingUtil.isNumberIsh;
1622
import static graphql.scalar.CoercingUtil.typeName;
1723

24+
/**
25+
* The deprecated methods still have implementations in case code outside graphql-java is calling them
26+
* but internally the call paths have been replaced.
27+
*/
1828
@Internal
1929
public class GraphqlFloatCoercing implements Coercing<Double, Double> {
2030

@@ -33,46 +43,88 @@ private Double convertImpl(Object input) {
3343

3444
}
3545

36-
@Override
37-
public Double serialize(Object input) {
46+
@NotNull
47+
private Double serialiseImpl(Object input, @NotNull Locale locale) {
3848
Double result = convertImpl(input);
3949
if (result == null) {
4050
throw new CoercingSerializeException(
41-
"Expected type 'Float' but was '" + typeName(input) + "'."
51+
i18nMsg(locale, "Float.notFloat", typeName(input))
4252
);
4353
}
4454
return result;
45-
4655
}
4756

48-
@Override
49-
public Double parseValue(Object input) {
57+
@NotNull
58+
private Double parseValueImpl(Object input, @NotNull Locale locale) {
5059
Double result = convertImpl(input);
5160
if (result == null) {
5261
throw new CoercingParseValueException(
53-
"Expected type 'Float' but was '" + typeName(input) + "'."
62+
i18nMsg(locale, "Float.notFloat", typeName(input))
5463
);
5564
}
5665
return result;
5766
}
5867

59-
@Override
60-
public Double parseLiteral(Object input) {
68+
private static double parseLiteralImpl(@NotNull Object input, @NotNull Locale locale) {
6169
if (input instanceof IntValue) {
6270
return ((IntValue) input).getValue().doubleValue();
6371
} else if (input instanceof FloatValue) {
6472
return ((FloatValue) input).getValue().doubleValue();
6573
} else {
6674
throw new CoercingParseLiteralException(
67-
"Expected AST type 'IntValue' or 'FloatValue' but was '" + typeName(input) + "'."
75+
i18nMsg(locale, "Float.unexpectedAstType", typeName(input))
6876
);
6977
}
7078
}
7179

72-
@Override
73-
public Value valueToLiteral(Object input) {
74-
Double result = assertNotNull(convertImpl(input));
80+
@NotNull
81+
private FloatValue valueToLiteralImpl(Object input, @NotNull Locale locale) {
82+
Double result = assertNotNull(convertImpl(input), () -> i18nMsg(locale, "Float.notFloat", typeName(input)));
7583
return FloatValue.newFloatValue(BigDecimal.valueOf(result)).build();
7684
}
85+
86+
@Override
87+
@Deprecated
88+
public Double serialize(@NotNull Object dataFetcherResult) {
89+
return serialiseImpl(dataFetcherResult, Locale.getDefault());
90+
}
91+
92+
@Override
93+
public @Nullable Double serialize(@NotNull Object dataFetcherResult, @NotNull GraphQLContext graphQLContext, @NotNull Locale locale) throws CoercingSerializeException {
94+
return serialiseImpl(dataFetcherResult, locale);
95+
}
96+
97+
@Override
98+
@Deprecated
99+
public @NotNull Double parseValue(@NotNull Object input) {
100+
return parseValueImpl(input, Locale.getDefault());
101+
}
102+
103+
@Override
104+
public Double parseValue(@NotNull Object input, @NotNull GraphQLContext graphQLContext, @NotNull Locale locale) throws CoercingParseValueException {
105+
return parseValueImpl(input, locale);
106+
}
107+
108+
@Override
109+
@Deprecated
110+
public Double parseLiteral(@NotNull Object input) {
111+
return parseLiteralImpl(input, Locale.getDefault());
112+
}
113+
114+
@Override
115+
public @Nullable Double parseLiteral(@NotNull Value<?> input, @NotNull CoercedVariables variables, @NotNull GraphQLContext graphQLContext, @NotNull Locale locale) throws CoercingParseLiteralException {
116+
return parseLiteralImpl(input, locale);
117+
}
118+
119+
@Override
120+
@Deprecated
121+
public Value valueToLiteral(@NotNull Object input) {
122+
return valueToLiteralImpl(input, Locale.getDefault());
123+
}
124+
125+
@Override
126+
public @NotNull Value<?> valueToLiteral(@NotNull Object input, @NotNull GraphQLContext graphQLContext, @NotNull Locale locale) {
127+
return valueToLiteralImpl(input, locale);
128+
}
77129
}
78130

0 commit comments

Comments
 (0)