11package graphql .scalar ;
22
3+ import graphql .GraphQLContext ;
34import graphql .Internal ;
5+ import graphql .execution .CoercedVariables ;
46import graphql .language .FloatValue ;
57import graphql .language .IntValue ;
68import graphql .language .Value ;
79import graphql .schema .Coercing ;
810import graphql .schema .CoercingParseLiteralException ;
911import graphql .schema .CoercingParseValueException ;
1012import graphql .schema .CoercingSerializeException ;
13+ import org .jetbrains .annotations .NotNull ;
14+ import org .jetbrains .annotations .Nullable ;
1115
1216import java .math .BigDecimal ;
17+ import java .util .Locale ;
1318
1419import static graphql .Assert .assertNotNull ;
20+ import static graphql .scalar .CoercingUtil .i18nMsg ;
1521import static graphql .scalar .CoercingUtil .isNumberIsh ;
1622import 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
1929public 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