@@ -139,52 +139,25 @@ private static String bytesToHex(byte[] bytes, char[] digits) {
139139 * @param val the value to format.
140140 */
141141 private static void formatString (StringBuilder builder , Val val ) {
142- if (val .type ().typeEnum () == TypeEnum .String ) {
143- builder .append (val .value ());
144- } else if (val .type ().typeEnum () == TypeEnum .Bytes ) {
145- builder .append (new String ((byte []) val .value (), StandardCharsets .UTF_8 ));
146- } else if (val .type ().typeEnum () == TypeEnum .Double ) {
147- DecimalFormat format = new DecimalFormat ();
148- builder .append (format .format (val .value ()));
149- } else {
150- formatStringSafe (builder , val , false );
151- }
152- }
153-
154- /**
155- * Formats a string value safely for other value types.
156- *
157- * @param builder the StringBuilder to append the formatted string to.
158- * @param val the value to format.
159- * @param listType indicates if the value type is a list.
160- */
161- private static void formatStringSafe (StringBuilder builder , Val val , boolean listType ) {
162142 TypeEnum type = val .type ().typeEnum ();
163143 if (type == TypeEnum .Bool ) {
164144 builder .append (val .booleanValue ());
165- } else if (type == TypeEnum .Int || type == TypeEnum .Uint ) {
166- formatDecimal (builder , val );
167- } else if (type == TypeEnum .Double ) {
168- // When a double is nested in another type (e.g. a list) it will have a minimum of 6 decimal
169- // digits. This is to maintain consistency with the Go CEL runtime.
170- DecimalFormat format = new DecimalFormat ();
171- format .setMaximumFractionDigits (Integer .MAX_VALUE );
172- format .setMinimumFractionDigits (6 );
173- builder .append (format .format (val .value ()));
174- } else if (type == TypeEnum .String ) {
175- builder .append ("\" " ).append (val .value ().toString ()).append ("\" " );
145+ } else if (type == TypeEnum .String || type == TypeEnum .Int || type == TypeEnum .Uint ) {
146+ builder .append (val .value ());
176147 } else if (type == TypeEnum .Bytes ) {
177- formatBytes (builder , val );
148+ builder .append (new String ((byte []) val .value (), StandardCharsets .UTF_8 ));
149+ } else if (type == TypeEnum .Double ) {
150+ formatDecimal (builder , val );
178151 } else if (type == TypeEnum .Duration ) {
179- formatDuration (builder , val , listType );
152+ formatDuration (builder , val );
180153 } else if (type == TypeEnum .Timestamp ) {
181154 formatTimestamp (builder , val );
182155 } else if (type == TypeEnum .List ) {
183156 formatList (builder , val );
184157 } else if (type == TypeEnum .Map ) {
185- throw new ErrException ("unimplemented stringSafe map type" );
158+ throw new ErrException ("unimplemented string map type" );
186159 } else if (type == TypeEnum .Null ) {
187- throw new ErrException ("unimplemented stringSafe null type" );
160+ throw new ErrException ("unimplemented string null type" );
188161 }
189162 }
190163
@@ -200,7 +173,7 @@ private static void formatList(StringBuilder builder, Val val) {
200173 List list = val .convertToNative (List .class );
201174 for (int i = 0 ; i < list .size (); i ++) {
202175 Object obj = list .get (i );
203- formatStringSafe (builder , DefaultTypeAdapter .nativeToValue (Db .newDb (), null , obj ), true );
176+ formatString (builder , DefaultTypeAdapter .nativeToValue (Db .newDb (), null , obj ));
204177 if (i != list .size () - 1 ) {
205178 builder .append (", " );
206179 }
@@ -225,35 +198,15 @@ private static void formatTimestamp(StringBuilder builder, Val val) {
225198 *
226199 * @param builder the StringBuilder to append the formatted duration value to.
227200 * @param val the value to format.
228- * @param listType indicates if the value type is a list.
229201 */
230- private static void formatDuration (StringBuilder builder , Val val , boolean listType ) {
231- if (listType ) {
232- builder .append ("duration(\" " );
233- }
202+ private static void formatDuration (StringBuilder builder , Val val ) {
234203 Duration duration = val .convertToNative (Duration .class );
235204
236205 double totalSeconds = duration .getSeconds () + (duration .getNanos () / 1_000_000_000.0 );
237206
238- DecimalFormat format = new DecimalFormat ("0.#########" );
239- builder .append (format .format (totalSeconds ));
207+ DecimalFormat formatter = new DecimalFormat ("0.#########" );
208+ builder .append (formatter .format (totalSeconds ));
240209 builder .append ("s" );
241- if (listType ) {
242- builder .append ("\" )" );
243- }
244- }
245-
246- /**
247- * Formats a byte array value.
248- *
249- * @param builder the StringBuilder to append the formatted byte array value to.
250- * @param val the value to format.
251- */
252- private static void formatBytes (StringBuilder builder , Val val ) {
253- builder
254- .append ("\" " )
255- .append (new String ((byte []) val .value (), StandardCharsets .UTF_8 ))
256- .append ("\" " );
257210 }
258211
259212 /**
@@ -283,9 +236,10 @@ private static void formatHex(StringBuilder builder, Val val, char[] digits) {
283236 * Formats a decimal value.
284237 *
285238 * @param builder the StringBuilder to append the formatted decimal value to.
286- * @param arg the value to format.
239+ * @param val the value to format.
287240 */
288- private static void formatDecimal (StringBuilder builder , Val arg ) {
289- builder .append (arg .value ());
241+ private static void formatDecimal (StringBuilder builder , Val val ) {
242+ DecimalFormat formatter = new DecimalFormat ("0.#########" );
243+ builder .append (formatter .format (val .value ()));
290244 }
291245}
0 commit comments