-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathFormat.java
More file actions
371 lines (345 loc) · 11.7 KB
/
Format.java
File metadata and controls
371 lines (345 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2023-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package build.buf.protovalidate;
import static java.time.format.DateTimeFormatter.ISO_INSTANT;
import com.google.protobuf.Duration;
import com.google.protobuf.Timestamp;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.time.Instant;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.stream.Collectors;
import org.projectnessie.cel.common.types.Err.ErrException;
import org.projectnessie.cel.common.types.IntT;
import org.projectnessie.cel.common.types.IteratorT;
import org.projectnessie.cel.common.types.ListT;
import org.projectnessie.cel.common.types.MapT;
import org.projectnessie.cel.common.types.ref.TypeEnum;
import org.projectnessie.cel.common.types.ref.Val;
/** String formatter for CEL evaluation. */
final class Format {
/**
* Format the string with a {@link ListT}.
*
* @param fmtString the string to format.
* @param list the arguments.
* @return the formatted string.
* @throws ErrException If an error occurs formatting the string.
*/
static String format(String fmtString, ListT list) {
// StringBuilder to accumulate the formatted string
StringBuilder builder = new StringBuilder();
int index = 0;
int argIndex = 0;
while (index < fmtString.length()) {
char c = fmtString.charAt(index++);
if (c != '%') {
// Append non-format characters directly
builder.append(c);
// Add the entire character if it's not a UTF-8 character.
if ((c & 0x80) != 0) {
// Add the rest of the UTF-8 character.
while (index < fmtString.length() && (fmtString.charAt(index) & 0xc0) == 0x80) {
builder.append(fmtString.charAt(index++));
}
}
continue;
}
if (index >= fmtString.length()) {
throw new ErrException("format: expected format specifier");
}
if (fmtString.charAt(index) == '%') {
// Escaped '%', append '%' and move to the next character
builder.append('%');
index++;
continue;
}
if (argIndex >= list.size().intValue()) {
throw new ErrException("index " + argIndex + " out of range");
}
Val arg = list.get(IntT.intOf(argIndex++));
c = fmtString.charAt(index++);
int precision = 6;
if (c == '.') {
// parse the precision
precision = 0;
while (index < fmtString.length()
&& '0' <= fmtString.charAt(index)
&& fmtString.charAt(index) <= '9') {
precision = precision * 10 + (fmtString.charAt(index++) - '0');
}
if (index >= fmtString.length()) {
throw new ErrException("format: expected format specifier");
}
c = fmtString.charAt(index++);
}
switch (c) {
case 'd':
builder.append(formatDecimal(arg));
break;
case 'x':
builder.append(formatHex(arg));
break;
case 'X':
// We can use a root locale, because the only characters are hex (A-F).
builder.append(formatHex(arg).toUpperCase(Locale.ROOT));
break;
case 's':
builder.append(formatString(arg));
break;
case 'e':
builder.append(formatExponential(arg, precision));
break;
case 'f':
builder.append(formatFloat(arg, precision));
break;
case 'b':
builder.append(formatBinary(arg));
break;
case 'o':
builder.append(formatOctal(arg));
break;
default:
throw new ErrException(
"could not parse formatting clause: unrecognized formatting clause \"" + c + "\"");
}
}
return builder.toString();
}
/**
* Formats a string value.
*
* @param val the value to format.
*/
private static String formatString(Val val) {
TypeEnum type = val.type().typeEnum();
switch (type) {
case Type:
case String:
return val.value().toString();
case Bool:
return Boolean.toString(val.booleanValue());
case Int:
case Uint:
Optional<String> str = validateNumber(val);
if (str.isPresent()) {
return str.get();
}
return val.value().toString();
case Bytes:
String byteStr = new String((byte[]) val.value(), StandardCharsets.UTF_8);
// Collapse any contiguous placeholders into one
return byteStr.replaceAll("\\ufffd+", "\ufffd");
case Double:
Optional<String> result = validateNumber(val);
if (result.isPresent()) {
return result.get();
}
return formatDecimal(val);
case Duration:
return formatDuration(val);
case Timestamp:
return formatTimestamp(val);
case List:
return formatList((ListT) val);
case Map:
return formatMap((MapT) val);
case Null:
return "null";
default:
throw new ErrException(
"error during formatting: string clause can only be used on strings, bools, bytes, ints, doubles, maps, lists, types, durations, and timestamps, was given "
+ val.type());
}
}
/**
* Formats a list value.
*
* @param val the value to format.
*/
private static String formatList(ListT val) {
StringBuilder builder = new StringBuilder();
builder.append('[');
IteratorT iter = val.iterator();
while (iter.hasNext().booleanValue()) {
Val v = iter.next();
builder.append(formatString(v));
if (iter.hasNext().booleanValue()) {
builder.append(", ");
}
}
builder.append(']');
return builder.toString();
}
private static String formatMap(MapT val) {
StringBuilder builder = new StringBuilder();
builder.append('{');
SortedMap<String, String> sorted = new TreeMap<>();
IteratorT iter = val.iterator();
while (iter.hasNext().booleanValue()) {
Val key = iter.next();
String mapKey = formatString(key);
String mapVal = formatString(val.find(key));
sorted.put(mapKey, mapVal);
}
String result =
sorted.entrySet().stream()
.map(entry -> entry.getKey() + ": " + entry.getValue())
.collect(Collectors.joining(", "));
builder.append(result).append('}');
return builder.toString();
}
/**
* Formats a timestamp value.
*
* @param val the value to format.
*/
private static String formatTimestamp(Val val) {
Timestamp timestamp = val.convertToNative(Timestamp.class);
Instant instant = Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
return ISO_INSTANT.format(instant);
}
/**
* Formats a duration value.
*
* @param val the value to format.
*/
private static String formatDuration(Val val) {
StringBuilder builder = new StringBuilder();
Duration duration = val.convertToNative(Duration.class);
double totalSeconds = duration.getSeconds() + (duration.getNanos() / 1_000_000_000.0);
DecimalFormat formatter = new DecimalFormat("0.#########");
builder.append(formatter.format(totalSeconds));
builder.append("s");
return builder.toString();
}
/**
* Formats a hexadecimal value.
*
* @param val the value to format.
*/
private static String formatHex(Val val) {
TypeEnum type = val.type().typeEnum();
if (type == TypeEnum.Int || type == TypeEnum.Uint) {
return Long.toHexString(val.intValue());
} else if (type == TypeEnum.Bytes) {
StringBuilder hexString = new StringBuilder();
for (byte b : (byte[]) val.value()) {
hexString.append(String.format("%02x", b));
}
return hexString.toString();
} else if (type == TypeEnum.String) {
String arg = val.value().toString();
return String.format("%x", new BigInteger(1, arg.getBytes(StandardCharsets.UTF_8)));
} else {
throw new ErrException(
"error during formatting: only integers, byte buffers, and strings can be formatted as hex, was given "
+ val.type());
}
}
/**
* Formats a decimal value.
*
* @param val the value to format.
*/
private static String formatDecimal(Val val) {
TypeEnum type = val.type().typeEnum();
if (type == TypeEnum.Int || type == TypeEnum.Uint || type == TypeEnum.Double) {
Optional<String> str = validateNumber(val);
if (str.isPresent()) {
return str.get();
}
DecimalFormat formatter = new DecimalFormat("0.#########");
return formatter.format(val.value());
} else {
throw new ErrException(
"error during formatting: decimal clause can only be used on integers, was given "
+ val.type());
}
}
private static String formatOctal(Val val) {
TypeEnum type = val.type().typeEnum();
if (type == TypeEnum.Int || type == TypeEnum.Uint) {
return Long.toOctalString(Long.valueOf(val.intValue()));
} else {
throw new ErrException(
"error during formatting: octal clause can only be used on integers, was given "
+ val.type());
}
}
private static String formatBinary(Val val) {
TypeEnum type = val.type().typeEnum();
if (type == TypeEnum.Int || type == TypeEnum.Uint) {
return Long.toBinaryString(Long.valueOf(val.intValue()));
} else if (type == TypeEnum.Bool) {
return val.booleanValue() ? "1" : "0";
} else {
throw new ErrException(
"error during formatting: only integers and bools can be formatted as binary, was given "
+ val.type());
}
}
private static String formatExponential(Val val, int precision) {
TypeEnum type = val.type().typeEnum();
if (type == TypeEnum.Double) {
Optional<String> str = validateNumber(val);
if (str.isPresent()) {
return str.get();
}
String pattern = "%." + precision + "e";
return String.format(pattern, val.doubleValue());
} else {
throw new ErrException(
"error during formatting: scientific clause can only be used on doubles, was given "
+ val.type());
}
}
private static String formatFloat(Val val, int precision) {
TypeEnum type = val.type().typeEnum();
if (type == TypeEnum.Double) {
Optional<String> str = validateNumber(val);
if (str.isPresent()) {
return str.get();
}
StringBuilder pattern = new StringBuilder("0.");
if (precision > 0) {
for (int i = 0; i < precision; i++) {
pattern.append("0");
}
} else {
pattern.append("########");
}
DecimalFormat formatter = new DecimalFormat(pattern.toString());
return formatter.format(val.value());
} else {
throw new ErrException(
"error during formatting: fixed-point clause can only be used on doubles, was given "
+ val.type());
}
}
private static Optional<String> validateNumber(Val val) {
if (val.doubleValue() == Double.POSITIVE_INFINITY) {
return Optional.of("Infinity");
} else if (val.doubleValue() == Double.NEGATIVE_INFINITY) {
return Optional.of("-Infinity");
}
return Optional.empty();
}
}