Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion emrichen/tags/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,31 @@
from .base import BaseTag


class EnrichingProxy:
"""
Eager JSONPath lookups and Emrichen's lazy evaluation don't always mix well.
Deep nesting with !Var etc. may cause a situation where we try to !Lookup or
!Format on a structure that is not yet enriched.

This tries to fix that by enriching property access.

https://github.com/con2/emrichen/issues/15
"""
def __init__(self, obj, context):
self.obj = obj
self.context = context

def __getitem__(self, index):
return self.context.enrich(self.obj[index])


@lru_cache()
def parse_jsonpath(expr: str):
return jsonpath_rw.parse(expr)


def find_jsonpath_in_context(jsonpath_str: str, context: Context) -> List[jsonpath_rw.DatumInContext]:
return parse_jsonpath(jsonpath_str).find(context)
return parse_jsonpath(jsonpath_str).find(EnrichingProxy(context, context))


class Lookup(BaseTag):
Expand Down
15 changes: 14 additions & 1 deletion tests/test_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def test_lookup_no_match():
assert 'no matches for' in str(ei.value)


@pytest.mark.xfail
def test_late_enrich():
template = Template.parse('''
!Defaults
Expand Down Expand Up @@ -98,3 +97,17 @@ def test_lookup_enrich():
template: !Var item
''')
assert template.enrich({}) == [{'should_contain_5': [5]}]


@pytest.mark.xfail
def test_recursive_data_structure():
template = Template.parse('''
!Defaults
x:
y: 5
x: !Var x
---
five: !Lookup x.y
also_five: !Lookup x.x.x.x.x.y
''')
assert template.enrich({}) == [{'five': 5, 'also_five': 5}]