meyermw / rson

Automatically exported from code.google.com/p/rson
MIT License
1 stars 0 forks source link

Macros are not dereferenced in lists #2

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It looks like if you define a macro in a list item in an rson file, rsonmac 
never deferences the macro and you are left with a macroproxy object in the 
returned data structure.  Dereferencing seems to work fine for dicts.

Steps will reproduce the problem:

<code>

from rsonmac import *

text = """
var:
    key0 : val0

#this works
myFirstSection: $(var[key0])

mySecondSection: []
#   the macro never gets dereferenced here
    $(var[key0])
    A different thing

myThirdSection:
    Subsection: []
#       the macro never gets dereferenced here
        $(var[key0])
        Another different thing
"""

d3 = loads(test3)
print(d3)

</code>

When I run this I should see:
d3 = loads(test3)
    print(d3)
Instead I see:
{u'myFirstSection': u'val0',
 u'mySecondSection': [u'val0',
                      u'A different thing'],
 u'myThirdSection': {u'Subsection': [u'val0',
                                     u'Another different thing']},
 u'var': {u'key0': u'val0'}}

Instead it looks like:
{u'myFirstSection': u'val0',
 u'mySecondSection': [<__main__.MacroProxy object at 0x060A6BB0>,
                      u'A different thing'],
 u'myThirdSection': {u'Subsection': [<__main__.MacroProxy object at 0x060A6B90>,
                                     u'Another different thing']},
 u'var': {u'key0': u'val0'}}

This is using the rson and rsonmac pulled from trunk on 12 April 2012.
I'm running Python 2.7.2 on Windows 7

Thanks for this project.  I had rolled my own scheme for doing this sort of 
thing to data read in from a JSON file, but it was not as flexible as rsonmac.  
I was using the string.Template class when walking through the tree.  My 
variables needed to be in a dict named 'var'.  I'm hoping I can get rsonmac 
working for lists so I don't need to resort to my other approach.

Thanks!
Brad

Original issue reported on code.google.com by bradlcam...@gmail.com on 12 Apr 2012 at 6:53

GoogleCodeExporter commented 9 years ago
I've modifed the post_parse method in rsonmac.py.  My changes to work for my 
case, but I'm not sure if there are cases where it would fail.  Here is the new 
function:

    @staticmethod
    def post_parse(tokens, value):

        if isinstance(value,list):
            iterant = range(0,len(value))
        elif isinstance(value,dict):
            iterant = value.keys()

        for i in iterant:
            if isinstance(value[i],MacroProxy):
                value[i] = value[i].dereference()
            elif (isinstance(value[i],dict) or isinstance(value[i],list)):
                value[i] = RsonMacros.post_parse(None,value[i])            

        return value

Thanks,
Brad

Original comment by bradlcam...@gmail.com on 12 Apr 2012 at 7:45

GoogleCodeExporter commented 9 years ago
I think the problem was just with the first element of the array.

I have checked in a fix to rsonmac.py.  Please try this and close the issue if 
it works for you.

Thanks,
Pat

Original comment by pmaupin on 12 Apr 2012 at 7:52

GoogleCodeExporter commented 9 years ago
Beautiful.  Thanks!

Original comment by bradlcam...@gmail.com on 12 Apr 2012 at 8:18

GoogleCodeExporter commented 9 years ago
Glad I could help.  Thanks for using RSON.  Since you seem happy, I'll mark the 
fix as verified.

Original comment by pmaupin on 12 Apr 2012 at 8:27