|
7 | 7 | import com.fasterxml.jackson.databind.ObjectMapper; |
8 | 8 | import com.google.common.collect.ImmutableCollection; |
9 | 9 | import com.google.common.collect.ImmutableSet; |
| 10 | +import com.google.protobuf.NullValue; |
10 | 11 | import dev.cel.common.CelAbstractSyntaxTree; |
11 | 12 | import dev.cel.common.CelOptions; |
12 | 13 | import dev.cel.common.CelValidationException; |
|
26 | 27 | import io.kafbat.ui.exception.CelException; |
27 | 28 | import io.kafbat.ui.model.TopicMessageDTO; |
28 | 29 | import java.util.HashMap; |
| 30 | +import java.util.LinkedHashMap; |
29 | 31 | import java.util.Map; |
30 | 32 | import java.util.Objects; |
31 | 33 | import java.util.Optional; |
|
38 | 40 | @Slf4j |
39 | 41 | @UtilityClass |
40 | 42 | public class MessageFilters { |
| 43 | + |
41 | 44 | private static final String CEL_RECORD_VAR_NAME = "record"; |
42 | 45 | private static final String CEL_RECORD_TYPE_NAME = TopicMessageDTO.class.getSimpleName(); |
43 | 46 |
|
44 | 47 | private static final CelCompiler CEL_COMPILER = createCompiler(); |
45 | 48 | private static final CelRuntime CEL_RUNTIME = createRuntime(); |
| 49 | + private static final Object CELL_NULL_VALUE = NullValue.NULL_VALUE; |
46 | 50 |
|
47 | 51 | private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
48 | 52 |
|
@@ -188,10 +192,34 @@ private static Object parseToJsonOrReturnAsIs(@Nullable String str) { |
188 | 192 | } |
189 | 193 |
|
190 | 194 | try { |
191 | | - return OBJECT_MAPPER.readValue(str, new TypeReference<Map<String, Object>>() { |
192 | | - }); |
| 195 | + //@formatter:off |
| 196 | + var map = OBJECT_MAPPER.readValue(str, new TypeReference<Map<String, Object>>() {}); |
| 197 | + //@formatter:on |
| 198 | + return replaceCelNulls(map); |
193 | 199 | } catch (JsonProcessingException e) { |
194 | 200 | return str; |
195 | 201 | } |
196 | 202 | } |
| 203 | + |
| 204 | + @SuppressWarnings("unchecked") |
| 205 | + private static Map<String, Object> replaceCelNulls(Map<String, Object> map) { |
| 206 | + var result = new LinkedHashMap<String, Object>(); |
| 207 | + |
| 208 | + for (var entry : map.entrySet()) { |
| 209 | + String key = entry.getKey(); |
| 210 | + Object value = entry.getValue(); |
| 211 | + |
| 212 | + if (value == null) { |
| 213 | + result.put(key, CELL_NULL_VALUE); |
| 214 | + } else if (value instanceof Map<?, ?>) { |
| 215 | + var inner = (Map<String, Object>) value; |
| 216 | + result.put(key, replaceCelNulls(inner)); |
| 217 | + } else { |
| 218 | + result.put(key, value); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + return result; |
| 223 | + } |
| 224 | + |
197 | 225 | } |
0 commit comments