chia7712 commented on code in PR #19871:
URL: https://github.com/apache/kafka/pull/19871#discussion_r2214031048
##########
connect/transforms/src/main/java/org/apache/kafka/connect/transforms/MaskField.java:
##########
@@ -65,32 +65,30 @@ public abstract class MaskField<R extends ConnectRecord<R>>
implements Transform
private static final String PURPOSE = "mask fields";
- private static final Map<Class<?>, Function<String, ?>>
REPLACEMENT_MAPPING_FUNC = new HashMap<>();
- private static final Map<Class<?>, Object> PRIMITIVE_VALUE_MAPPING = new
HashMap<>();
-
- static {
- PRIMITIVE_VALUE_MAPPING.put(Boolean.class, Boolean.FALSE);
- PRIMITIVE_VALUE_MAPPING.put(Byte.class, (byte) 0);
- PRIMITIVE_VALUE_MAPPING.put(Short.class, (short) 0);
- PRIMITIVE_VALUE_MAPPING.put(Integer.class, 0);
- PRIMITIVE_VALUE_MAPPING.put(Long.class, 0L);
- PRIMITIVE_VALUE_MAPPING.put(Float.class, 0f);
- PRIMITIVE_VALUE_MAPPING.put(Double.class, 0d);
- PRIMITIVE_VALUE_MAPPING.put(BigInteger.class, BigInteger.ZERO);
- PRIMITIVE_VALUE_MAPPING.put(BigDecimal.class, BigDecimal.ZERO);
- PRIMITIVE_VALUE_MAPPING.put(Date.class, new Date(0));
- PRIMITIVE_VALUE_MAPPING.put(String.class, "");
-
- REPLACEMENT_MAPPING_FUNC.put(Byte.class, v ->
Values.convertToByte(null, v));
- REPLACEMENT_MAPPING_FUNC.put(Short.class, v ->
Values.convertToShort(null, v));
- REPLACEMENT_MAPPING_FUNC.put(Integer.class, v ->
Values.convertToInteger(null, v));
- REPLACEMENT_MAPPING_FUNC.put(Long.class, v ->
Values.convertToLong(null, v));
- REPLACEMENT_MAPPING_FUNC.put(Float.class, v ->
Values.convertToFloat(null, v));
- REPLACEMENT_MAPPING_FUNC.put(Double.class, v ->
Values.convertToDouble(null, v));
- REPLACEMENT_MAPPING_FUNC.put(String.class, Function.identity());
- REPLACEMENT_MAPPING_FUNC.put(BigDecimal.class, BigDecimal::new);
- REPLACEMENT_MAPPING_FUNC.put(BigInteger.class, BigInteger::new);
- }
+ private static final Map<Class<?>, Function<String, ?>>
REPLACEMENT_MAPPING_FUNC = Map.ofEntries(
Review Comment:
```java
private static final Map<Class<?>, Function<String, ?>>
REPLACEMENT_MAPPING_FUNC = Map.of(
Byte.class, v -> Values.convertToByte(null, v),
Short.class, v -> Values.convertToShort(null, v),
Integer.class, v -> Values.convertToInteger(null, v),
Long.class, v -> Values.convertToLong(null, v),
Float.class, v -> Values.convertToFloat(null, v),
Double.class, v -> Values.convertToDouble(null, v),
String.class, Function.identity(),
BigDecimal.class, BigDecimal::new,
BigInteger.class, BigInteger::new
);
```
##########
connect/transforms/src/test/java/org/apache/kafka/connect/transforms/MaskFieldTest.java:
##########
@@ -64,25 +61,25 @@ public class MaskFieldTest {
.field("map", SchemaBuilder.map(Schema.STRING_SCHEMA,
Schema.STRING_SCHEMA))
.field("withDefault",
SchemaBuilder.string().optional().defaultValue("default").build())
.build();
- private static final Map<String, Object> VALUES = new HashMap<>();
+ private static final Map<String, Object> VALUES = Map.ofEntries(
+ Map.entry("magic", 42),
+ Map.entry("bool", true),
+ Map.entry("byte", (byte) 42),
+ Map.entry("short", (short) 42),
+ Map.entry("int", 42),
+ Map.entry("long", 42L),
+ Map.entry("float", 42f),
+ Map.entry("double", 42d),
+ Map.entry("string", "55.121.20.20"),
+ Map.entry("date", new Date()),
+ Map.entry("bigint", new BigInteger("42")),
+ Map.entry("bigdec", new BigDecimal("42.0")),
+ Map.entry("list", List.of(42)),
+ Map.entry("map", Map.of("key", "value"))
+ );
private static final Struct VALUES_WITH_SCHEMA = new Struct(SCHEMA);
Review Comment:
Could you please rewrite it too?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]