Skip to content

Commit

Permalink
Update finalise to return the payload
Browse files Browse the repository at this point in the history
  • Loading branch information
njoyce committed Jan 25, 2015
1 parent 889b01a commit 9bf8d63
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cpyamf/codec.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ cdef class Decoder(Codec):
cdef object readConcreteElement(self, char t)

cpdef int send(self, data) except -1
cdef int finalise(self, object payload) except? -1
cdef object finalise(self, object payload)


cdef class Encoder(Codec):
Expand Down
8 changes: 4 additions & 4 deletions cpyamf/codec.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ cdef class Decoder(Codec):
self.depth -= 1

if self.depth == 0:
self.finalise(element)
element = self.finalise(element)

return element

Expand Down Expand Up @@ -442,17 +442,17 @@ cdef class Decoder(Codec):
def __iter__(self):
return self

cdef int finalise(self, object payload) except? -1:
cdef object finalise(self, object payload):
"""
Finalise the payload.
This provides a useful hook to adapters to modify the payload that was
decoded.
"""
for c in pyamf.POST_DECODE_PROCESSORS:
c(payload, self.context.extra)
payload = c(payload, self.context.extra)

return 0
return payload


cdef class Encoder(Codec):
Expand Down
3 changes: 3 additions & 0 deletions pyamf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,9 @@ def add_post_decode_processor(func):
This is useful for adapter as the last chance to modify the Python graph
before it enters user land.
The function takes two arguments, the decoded payload and the context's
`extra` dict. It MUST return the payload that will be finally returned.
@see: L{pyamf.codec.Decoder.finalise}
@since: 0.7.0
"""
Expand Down
6 changes: 4 additions & 2 deletions pyamf/codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,9 @@ def finalise(self, payload):
decoder.
"""
for c in pyamf.POST_DECODE_PROCESSORS:
c(payload, self.context.extra)
payload = c(payload, self.context.extra)

return payload

def _readElement(self):
"""
Expand Down Expand Up @@ -399,7 +401,7 @@ def readElement(self):
self.__depth -= 1

if self.__depth == 0:
self.finalise(element)
element = self.finalise(element)

return element

Expand Down
5 changes: 4 additions & 1 deletion pyamf/tests/test_amf0.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,15 +925,18 @@ def postprocess(payload, context):

self.executed = True

return payload

pyamf.add_post_decode_processor(postprocess)

# setup complete
bytes = pyamf.encode(u'foo', encoding=pyamf.AMF0).getvalue()

self.decoder.send(bytes)
self.decoder.next()
ret = self.decoder.next()

self.assertTrue(self.executed)
self.assertEqual(ret, u'foo')


class RecordSetTestCase(unittest.TestCase, EncoderMixIn, DecoderMixIn):
Expand Down
5 changes: 4 additions & 1 deletion pyamf/tests/test_amf3.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,15 +941,18 @@ def postprocess(payload, context):

self.executed = True

return payload

pyamf.add_post_decode_processor(postprocess)

# setup complete
bytes = pyamf.encode(u'foo', encoding=pyamf.AMF3).getvalue()

self.decoder.send(bytes)
self.decoder.next()
ret = self.decoder.next()

self.assertTrue(self.executed)
self.assertEqual(ret, u'foo')


class ObjectEncodingTestCase(ClassCacheClearingTestCase, EncoderMixIn):
Expand Down

0 comments on commit 9bf8d63

Please sign in to comment.