Trampoline for real world proposals

Days ago I had to consume a Rest API and save the data into my MongoDB gateway data store. I used simply because it’s simple to use.

My problem was that inside the JSON response there were few BigDecimal attributes (converted automatically using HttpResponseDecorator) and BigDecimal is not supported in MongoDB (check here / and here).

My first approach was hardcode those attributes and save them as Double – but then I discovered the structure could be completely different based on business rules (each request acts differently).

So I decided to create a Closure which goes “recursively” into the Map searching for BigDecimal attributes. Just mentioned “recursively” ’cause the idea is to use the same recursive approach (I don’t know the tree structure) but avoiding StackOverflow using trampoline, considering the JSON response is quite big.

Recursive example
Recursive example

Here is my piece of code:

I have not found any other solution so far, let me know whether you have any trick one 😉

[]’s Cassio.


3 thoughts on “Trampoline for real world proposals

  1. Hi @Cassio, nice work. Groovy is awesome! Also memoize is a useful to cache previous method calls that can be cached for example Factorial algorithm…
    The trampoline can be achieved using stacks.. See:
    Also a test case:

    1. Hey Guil! Thanks for your message. Memoize wouldn’t fit in my case because it caches when there is the same method call – in my case the recursion is much bigger than the number of BigDecimal occurrence (I tried).

      There is another solution with @TailRecursive (groovy >= 2.3) which does pretty much the same as trampoline.

      I really like your solution… btw I will try it using the real response Map and benchmark against trampoline.

      Keep in touch brow!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s