YAML Path Expression Evaluator
package com.example.yaml; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class YamlPathEvaluator { private static final Pattern PATH_SEGMENT_PATTERN = Pattern.compile("([a-zA-Z0-9_-]+|\[\d+])"); public Object ev...
This algorithm evaluates a simple dot-notation path expression against a nested Map structure representing YAML data. It allows accessing nested values by key and elements within lists by index. This is useful for config...
The `evaluate` method splits the path string into segments. It then iteratively traverses the nested `Map` and `List` structures. The `navigate` helper method dispatches to `navigateMap` or `navigateList` based on the current data type. `navigateMap` handles key lookups, returning `null` if a key is absent. `navigateList` parses array indices, checking for valid format and bounds, returning `null` for out-of-bounds access. Edge cases include empty or null inputs, missing keys, invalid path segments, and out-of-bounds list indices. The time complexity is O(P) where P is the number of segments in the path, as each segment is processed once. Space complexity is O(1) excluding the input data, as only a few variables are used for traversal.
function evaluate(yamlData, path): segments = split path by '.' current = yamlData for each segment in segments: current = navigate(current, segment) if current is null: return null return current function navigate(curre...