inveniosoftware / dojson

Simple pythonic JSON to JSON converter.
https://dojson.readthedocs.io
Other
10 stars 29 forks source link

RFC Passing original blob to rules #171

Closed kaplun closed 8 years ago

kaplun commented 8 years ago

When a rule based on a key A depends on the outcome of the execution of a rule based on a key B, then A depends on B. Currently there is no way to implement this.

I'd propose that the original object is passed inside self in a special key that is subsequently deleted.

jirikuncar commented 8 years ago

@kaplun have you tried something like:

def execute_after_A_and_B(self, key, value):
    if self.get('A') and self.get('B')
        self['C'] = self.pop('A'), self.pop('B')

overdo = dojson.Overdo()

@overdo.over('a', 'A')
def match(self, key, value):
    self['A'] = True
    return execute_after_A_and_B(self, key, value)

@overdo.over('b', 'B')
def match(self, key, value):
    self['B'] = True
    return execute_after_A_and_B(self, key, value)
kaplun commented 8 years ago

@jirikuncar I thought about something similar, but isn't it making writing rules more complex?

E.g. compare with:

@overdo.over('a', 'A')
def match(self, key, value):
    if 'B' in self['__original_blob__']:
        # B is there then...
    else:
        # B is not there then ...
jirikuncar commented 8 years ago

@kaplun the complexity seems similar. One has to understand that there is a dependency between fields and execute_after_A_and_B makes it IMHO more explicit.

tiborsimko commented 8 years ago

@kaplun @jacquerie Shall I deduce from your thumbs-up that @jirikuncar's proposal works well for you? If so, let's close this issue.

P.S. Personally to me it looks much cleaner to use a execute_after_... technique rather than introducing blobs and using conditions for Bs that weren't explicitly specified in overdo...

jacquerie commented 8 years ago

We worked around this issue using the approach outlined in https://github.com/inveniosoftware/dojson/issues/173#issuecomment-247542655.