dmulyalin / ttp

Template Text Parser
MIT License
350 stars 34 forks source link

Bug: Dynamic Paths - Matches cast as integers are formatted as strings in resulting data structure #67

Closed stanswi closed 2 years ago

stanswi commented 2 years ago

Hey There,

I think this is a small bug. I tried to look through the code but I can't seem to find the section that formulates the data structures.

Assuming the following input data

foo domain 100
  bar 123
  biz
  baz
foo domain 101
  bar 234
  biz
foo domain 102
  bar 345

And assuming this template

<group name="foo**.{{ domain_id }}**">
foo domain {{ domain_id | DIGIT | to_int | _start_ }}
  bar {{ bar | DIGIT | to_int }}
  biz {{ biz | default(false) | set(true) }}
  baz {{ baz | default(false) | set(true) }}
</group>

I am casting the domain_id match to an integer type. When TTP returns the data structure, it appears to be cast back into a string rather than its native type.

Result:

"foo": {
        "100": {
                "bar": 123,
                "baz": true,
                "biz": true
        },
        "101": {
                "bar": 234,
                "baz": false,
                "biz": true
        },
        "102": {
                "bar": 345,
                "baz": false,
                "biz": false
        }
}

If this is in fact a bug, looking forward to seeing the PR. Hopeful that maybe someday I could contribute a PR for something like this but at the moment not familiar enough with the code base.

Thanks again!!

dmulyalin commented 2 years ago

@stanswi this might be considered a bug.

Added support for dynamic_path_key_to_int TTP object argument in commit b5922f4b3c791d04c2706e33365537378fa591ee, that allows to switch the behaviour to convert dynamic path keys to integers. For backward compatibility dynamic_path_key_to_int default value is False.

You can refer to test case as example demonstrating how to use dynamic_path_key_to_int

Hope that helps.

dmulyalin commented 2 years ago

FYI - released TTP 0.8.4 that includes fix for tis issue.

dmulyalin commented 2 years ago

closing this one as workaround integrated in ttp, feel free to re-open if need any further help.