private Map removeKeys()

in polaris-synchronizer/api/src/main/java/org/apache/polaris/tools/sync/polaris/planning/ModificationAwarePlanner.java [112:144]


  private Map<String, Object> removeKeys(Map<String, Object> map, List<String> keysToRemove) {
    Map<String, Object> cleaned =
        objectMapper.convertValue(map, new TypeReference<Map<String, Object>>() {});

    for (String key : keysToRemove) {
      // splits key into first part and rest, eg. key1.key2.key3 becomes [key1, key2.key3]
      String[] separateFirst = key.split("\\.", 2);
      String primary = separateFirst[0];

      if (separateFirst.length > 1) {
        // if there are more nested keys, we want to recursively search the sub map if it exists
        Object valueForPrimary = cleaned.get(primary); // get object for primary key if it exists

        if (valueForPrimary == null) {
          continue;
        }

        try {
          Map<String, Object> subMap =
              objectMapper.convertValue(valueForPrimary, new TypeReference<>() {});
          Map<String, Object> cleanedSubMap =
              removeKeys(subMap, List.of(separateFirst[1])); // remove nested keys from submap
          cleaned.put(primary, cleanedSubMap); // replace sub-map with key removed
        } catch (IllegalArgumentException e) {
          // do nothing because that means the key does not exist, no need to remove it
        }
      } else {
        cleaned.remove(primary); // just remove the key if we have no more nesting
      }
    }

    return cleaned;
  }