StubbleOrg / Stubble

Trimmed down {{mustache}} templates in .NET
Other
405 stars 58 forks source link

Dynamic support is incomplete #91

Closed 0x15e closed 4 years ago

0x15e commented 4 years ago

I just got done troubleshooting a problem with Stubble not resolving properties on a custom object derived from DynamicObject and after seriously starting to doubt my sanity, finally found the culprit in the default value getters that were being set in RendererSettingsDefaults.cs. The default getters only work on dynamic objects that implement IDictionary<string, object>.

That works fine for ExpandoObject but doesn't work for anything that derives from DynamicObject or just implements IDynamicMetaObjectProvider without implementing IDictionary<string, object>.

I've put together a value getter that works using IDynamicMetaObjectProvider and the CallSite APIs and while it should cover more cases, I haven't tested performance and was hoping we could discuss it a bit before I submit a PR for it.

The Gist for it is here: https://gist.github.com/0x15e/eb220c9c639b148fd3a26ae5edc49f09

Romanx commented 4 years ago

Hi there,

Thanks very much for this very well put together issue. I'd be happy to accept a pull request and work with you to get this merged. I thought there may be some issues around dynamics but didn't have the knowledge to improve it so thank you very much!

0x15e commented 4 years ago

@Romanx sounds good. This has been a learning experience for me too. :)

I was thinking of doing an implementation where it tries the existing approach first because it's bound to be faster, then using the IDynamicMetaObjectProvider approach next if that doesn't work.

I should have some time today to polish up that gist and will try to have a PR for you in the next few days.

Romanx commented 4 years ago

That sounds like a great idea, will look forward to it!

0x15e commented 4 years ago

@Romanx any chance you've had an opportunity to review that PR?

Romanx commented 4 years ago

@0x15e Thanks for your contribution. I got it merged and released today in version 1.8.4. Please let me know if you have any issues.