StubbleOrg / Stubble

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

Anonymous object property is not retrieved #140

Closed Baccanno closed 1 year ago

Baccanno commented 1 year ago

Hi there, not sure it's a bug or I'm missing something, I'm in a deeply nested context and the following happens :

(I simplified the representation)

 var myData = new { 
  Name = "Name",
  Resources = new Dictionnary<string, int>{ ... }
 }

It gets up to a Section Renderer which try to parse a {{#Resources}} tag.

While debugging, I see that it recognize the anonymous object as either a IList, (in which case it tries to parse the tag as an int and fails) or as a standard object. In which case it ends up finding an already existing Getter in the cache of type :

"(System.Collections.Generic.Dictionary2[System.String,System.Lazy1[System.Func2[System.Object,System.Object]]], System.Collections.Generic.Dictionary2[System.String,System.Lazy1[System.Func2[System.Object,System.Object]]])"

In which these values are present and of course none match the tag.

  Nom Valeur Type
[0] "[get_Capacity, Value is not created.]" System.Collections.Generic.KeyValuePair<string, System.Lazy<System.Func<object, object>>>
[1] "[get_Count, Value is not created.]" System.Collections.Generic.KeyValuePair<string, System.Lazy<System.Func<object, object>>>
[2] "[AsReadOnly, Value is not created.]" System.Collections.Generic.KeyValuePair<string, System.Lazy<System.Func<object, object>>>
[3] "[GetEnumerator, Value is not created.]" System.Collections.Generic.KeyValuePair<string, System.Lazy<System.Func<object, object>>>
[4] "[ToArray, Value is not created.]" System.Collections.Generic.KeyValuePair<string, System.Lazy<System.Func<object, object>>>
[5] "[GetHashCode, Value is not created.]" System.Collections.Generic.KeyValuePair<string, System.Lazy<System.Func<object, object>>>
[6] "[GetType, Value is not created.]" System.Collections.Generic.KeyValuePair<string, System.Lazy<System.Func<object, object>>>
[7] "[ToString, Value is not created.]" System.Collections.Generic.KeyValuePair<string, System.Lazy<System.Func<object, object>>>
[8] "[Capacity, Value is not created.]" System.Collections.Generic.KeyValuePair<string, System.Lazy<System.Func<object, object>>>
[9] "[Count, Value is not created.]" System.Collections.Generic.KeyValuePair<string, System.Lazy<System.Func<object, object>>>
Baccanno commented 1 year ago

I'll use a Dictionary as it might fix the issue momentarily. But I'm using anonymous type most of the time in the application as I came from Nustache.

Is it unadvised to use anonymous types ? I'm starting to think that some unexpected behaviours might be coming from this, but I'd like to be sure as It's a refactoring the goes through a lot of files.

Baccanno commented 1 year ago

Wierdly enough, a dictionary does not help. https://github.com/StubbleOrg/Stubble/blob/18d8e8aef39907d940f245aa6a8a8ed6a598b28d/src/Stubble.Core/Renderers/StringRenderer/TokenRenderers/SectionTokenRenderer.cs#L38 I still get a null value here when looking up Resources

https://github.com/StubbleOrg/Stubble/blob/18d8e8aef39907d940f245aa6a8a8ed6a598b28d/src/Stubble.Core/Contexts/Context.cs#L271

I clearly instanciate a dictionnary<string, object> but the type check fails.

 lang = new Dictionary<string, object>()
                        {
                            {"CardLang", cardLang},
                            {"Shift", ... },
                            {"Resources", ... )}
                        };

And the type is registered in the Getters

  Nom Valeur Type
[0] {Newtonsoft.Json.Linq.JObject} System.Type
[1] {Newtonsoft.Json.Linq.JProperty} System.Type
[2] {System.Collections.IList} System.Type
[3] {System.Collections.Generic.IDictionary`2[System.String,System.Object]} System.Type
[4] {System.Collections.IDictionary} System.Type
[5] {System.Dynamic.IDynamicMetaObjectProvider} System.Type
[6] {System.Object} System.Type
Baccanno commented 1 year ago

Okay, so the Value gets retrieved as a List instead of a Dictionary. Is Stubble converting data at some point? Or is it my fault :D ?

image

Baccanno commented 1 year ago

Definitelly my fault. I missed a SelectMany and had instead a simple Select so the list was not flatten.