def split_leftmost(jsonpath_expr): if isinstance(jsonpath_expr, jsonpath.Child): further_leftmost, rest = split_leftmost(jsonpath_expr.left) return further_leftmost, rest.child(jsonpath_expr.right) elif isinstance(jsonpath_expr, jsonpath.Descendants): further_leftmost, rest = split_leftmost(jsonpath_expr.left) return further_leftmost, jsonpath.Descendants(rest, jsonpath_expr.right) else: return (jsonpath_expr, jsonpath.This())
def test_split_leftmost(self): assert split_leftmost( parse_jsonpath('foo')) == (jsonpath.Fields('foo'), jsonpath.This()) assert split_leftmost( parse_jsonpath('foo.baz')) == (jsonpath.Fields('foo'), jsonpath.Fields('baz')) assert split_leftmost( parse_jsonpath('foo.baz.bar')) == (jsonpath.Fields('foo'), jsonpath.Fields('baz').child( jsonpath.Fields('bar'))) assert split_leftmost( parse_jsonpath('[*].baz')) == (jsonpath.Slice(), jsonpath.Fields('baz')) assert split_leftmost( parse_jsonpath('foo[*].baz')) == (jsonpath.Fields('foo'), jsonpath.Slice().child( jsonpath.Fields('baz')))