Skip to content

Commit ec69a56

Browse files
authored
Merge pull request #18 from jmespath-community/fix/root-reference
Fix #17 - root node reference
2 parents 4dcb2bf + 04805b3 commit ec69a56

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

jmespath/visitor.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def visit_current(self, node, value):
211211

212212
def visit_root(self, *args, **kwargs):
213213
if 'scopes' in kwargs:
214-
return kwargs['scopes'].getValue('$')
214+
return kwargs['scopes'].getRoot()
215215
return None
216216

217217
def visit_expref(self, node, value):
@@ -397,8 +397,9 @@ def _visit(self, node, current):
397397

398398
@with_str_method
399399
class Scopes:
400-
def __init__(self):
400+
def __init__(self, root):
401401
self._scopes = []
402+
self._root = root
402403

403404
def pushScope(self, scope):
404405
self._scopes.append(scope)
@@ -413,17 +414,19 @@ def getValue(self, identifier):
413414
return scope[identifier]
414415
return None
415416

417+
def getRoot(self):
418+
return self._root
419+
416420
def __str__(self):
417421
return '{}'.format(self._scopes)
418422

419423

420424
class ScopedInterpreter(TreeInterpreter):
421425
def __init__(self, options = None):
422426
super().__init__(options)
423-
self._scopes = Scopes()
424427

425428
def evaluate(self, ast, root_scope):
426-
self._scopes.pushScope({'$': root_scope})
429+
self._scopes = Scopes(root_scope)
427430
return self.visit(ast, root_scope)
428431

429432
def visit(self, node, *args, **kwargs):

tests/test_scopes.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66

77
class TestScope(unittest.TestCase):
88
def setUp(self):
9-
self._scopes = Scopes()
9+
self._scopes = Scopes('root')
1010

1111
def test_Scope_missing(self):
1212
value = self._scopes.getValue('foo')
1313
self.assertEqual(None, value)
1414

1515
def test_Scope_root(self):
16-
self._scopes.pushScope({'foo': 'bar'})
17-
value = self._scopes.getValue('foo')
18-
self.assertEqual('bar', value)
16+
value = self._scopes.getRoot()
17+
self.assertEqual('root', value)
1918

2019
def test_Scope_nested(self):
2120
self._scopes.pushScope({'foo': 'bar'})

0 commit comments

Comments
 (0)