validateExpressionDepth function

bool validateExpressionDepth(
  1. Object? expr, {
  2. int maxDepth = 5,
  3. int currentDepth = 1,
})

Giới hạn độ sâu lồng nhau của MapLibre expression (§9.8 protocol, mặc định 5). Chặn expression bệnh hoạn gây tốn CPU khi render.

Implementation

bool validateExpressionDepth(
  Object? expr, {
  int maxDepth = 5,
  int currentDepth = 1,
}) {
  if (currentDepth > maxDepth) return false;
  if (expr is! List) return true;
  for (final item in expr) {
    if (item is List &&
        !validateExpressionDepth(
          item,
          maxDepth: maxDepth,
          currentDepth: currentDepth + 1,
        )) {
      return false;
    }
  }
  return true;
}