Skip to content

Commit fa7c047

Browse files
committed
do some refactoring
1 parent 84a26aa commit fa7c047

File tree

1 file changed

+37
-46
lines changed

1 file changed

+37
-46
lines changed

fluss-client/src/main/java/org/apache/fluss/client/utils/PojoConverterUtils.java

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@
5555
* #getConverter(Class, RowType)} creates a new instance.
5656
*
5757
* <p>Notes: - Nested POJOs are not supported. - If a row contains null for a field that maps to a
58-
* primitive type in the POJO, that field will not be set (it keeps the Java default). Prefer boxed
59-
* types if nulls are expected.
60-
*
61-
* <p>This class is intended for internal use only.
58+
* primitive type in the POJO, that field will not be set (it keeps the Java default)
6259
*
6360
* @param <T> The POJO type to convert
6461
*/
@@ -208,18 +205,7 @@ private FieldToRowConverter[] createFieldToRowConverters() {
208205
}
209206

210207
// Validate Java field type compatibility with Fluss type
211-
Set<Class<?>> supported = SUPPORTED_TYPES.get(fieldType.getTypeRoot());
212-
Class<?> actual = field.getType();
213-
if (supported == null || !supported.contains(actual)) {
214-
throw new IllegalArgumentException(
215-
String.format(
216-
"Field '%s' in POJO %s has Java type %s which is incompatible with Fluss type %s. Supported Java types: %s",
217-
field.getName(),
218-
pojoClass.getName(),
219-
actual.getName(),
220-
fieldType.getTypeRoot(),
221-
supported));
222-
}
208+
validateCompatibility(fieldType, field, pojoClass);
223209
// Create the appropriate converter for this field
224210
converters[i] = createFieldToRowConverter(fieldType, field);
225211
} else {
@@ -243,18 +229,7 @@ private RowToFieldConverter[] createRowToFieldConverters() {
243229

244230
if (field != null) {
245231
// Validate Java field type compatibility with Fluss type
246-
Set<Class<?>> supported = SUPPORTED_TYPES.get(fieldType.getTypeRoot());
247-
Class<?> actual = field.getType();
248-
if (supported == null || !supported.contains(actual)) {
249-
throw new IllegalArgumentException(
250-
String.format(
251-
"Field '%s' in POJO %s has Java type %s which is incompatible with Fluss type %s. Supported Java types: %s",
252-
field.getName(),
253-
pojoClass.getName(),
254-
actual.getName(),
255-
fieldType.getTypeRoot(),
256-
supported));
257-
}
232+
validateCompatibility(fieldType, field, pojoClass);
258233
// Create the appropriate converter for this field
259234
converters[i] = createRowToFieldConverter(fieldType, field);
260235
} else {
@@ -292,6 +267,39 @@ private Field findField(Class<?> clazz, String fieldName) {
292267
return null;
293268
}
294269

270+
private static void validateCompatibility(DataType fieldType, Field field, Class<?> pojoClass) {
271+
Set<Class<?>> supported = SUPPORTED_TYPES.get(fieldType.getTypeRoot());
272+
Class<?> actual = field.getType();
273+
if (supported == null || !supported.contains(actual)) {
274+
throw new IllegalArgumentException(
275+
String.format(
276+
"Field '%s' in POJO %s has Java type %s which is incompatible with Fluss type %s. Supported Java types: %s",
277+
field.getName(),
278+
pojoClass.getName(),
279+
actual.getName(),
280+
fieldType.getTypeRoot(),
281+
supported));
282+
}
283+
}
284+
285+
private static BinaryString toBinaryStringForText(Object v, Field field, DataTypeRoot root) {
286+
final String s;
287+
if (v instanceof Character) {
288+
s = String.valueOf((Character) v);
289+
} else if (v instanceof String) {
290+
s = (String) v;
291+
} else {
292+
s = v.toString();
293+
}
294+
if (root == DataTypeRoot.CHAR && s.length() != 1) {
295+
throw new IllegalArgumentException(
296+
String.format(
297+
"Field %s expects exactly one character for CHAR type, got length %d.",
298+
field.getName(), s.length()));
299+
}
300+
return BinaryString.fromString(s);
301+
}
302+
295303
/**
296304
* Creates a field converter for converting from POJO to Row for a specific field based on its
297305
* data type.
@@ -317,24 +325,7 @@ private FieldToRowConverter createFieldToRowConverter(DataType fieldType, Field
317325
case CHAR:
318326
case STRING:
319327
return nullableFieldConverter(
320-
field,
321-
v -> {
322-
String s;
323-
if (v instanceof Character) {
324-
s = String.valueOf((Character) v);
325-
} else if (v instanceof String) {
326-
s = (String) v;
327-
} else {
328-
s = v.toString();
329-
}
330-
if (fieldType.getTypeRoot() == DataTypeRoot.CHAR && s.length() != 1) {
331-
throw new IllegalArgumentException(
332-
String.format(
333-
"Field %s expects exactly one character for CHAR type, got length %d.",
334-
field.getName(), s.length()));
335-
}
336-
return BinaryString.fromString(s);
337-
});
328+
field, v -> toBinaryStringForText(v, field, fieldType.getTypeRoot()));
338329
case DECIMAL:
339330
return nullableFieldConverter(
340331
field,

0 commit comments

Comments
 (0)