gobuffalo / plush

The powerful template system that Go needs
MIT License
901 stars 56 forks source link

Add map/slice index access #137

Closed Mido-sys closed 3 years ago

Mido-sys commented 3 years ago

Give access to nested array/map structs. For example, people[0].Name or people[0].Children[0].Final. The nested array/map access depth is unlimited.

The pull request builds on the existing code that worked with nested structs up to an array. The existing code didn’t give access to the underlying array fields people.Name.Chilldren[0] issue#101

Background:

The existing code processed the nested struct access by building a list of identifiers from back to front. So if the user wanted to access robot.Avatar.Name it will return an ast.Identifier with a nested Callee:

ast.Identifier.Value: "Final"  Callee (ast.Identifier.Value = "Avatar" Callee ( ast.Identifier.Value  = robot , Callee = Nil)). 

When the compiler receives the ast.Identifier, evalIdentifier checks first if Callee is not nil. If not, then it evaluates the expression ast.Callee. The compiler will keep unwrapping the ast.Identifier till it reaches the condition of ast.Callee is nil. If ast.Callee is nil then it will check if the ast.Identifier.Value stored in the ctx. As a result, ast.Identifier.Value = robot value will return the object that was passed in the ctx. Since the key robot is stored in ctx it will then return that object to the upper chain which now will access the object returned by the key ast.Identifier.Value = Avatar using reflection thus will return the nested object till it bubbles up to the first Callee which again, will have access to the object returned by ast.Identifier.Value: "Final".

Restirctions of evalIdentifier:

The function only processes objects returned by the Callee that are structs. It can’t process objects that return a slice. So if the process of robot returns a slice then Avatar won’t be able to access it. As a result, the existing code wasn't able to process struct fields that are of type map/array.

paganotoni commented 3 years ago

This is great. I think i've had this issue as well. I don't know if its related but I had a similar issue recently. I think is with the result of a function call:

...
<p class="mr-2">Employees will be reminded on <date class="font-semibold"><%= period.EmployeeReviewReminderDate().Format("Jan 2, 2006") %></date>.</p>
...

I get

no prefix parse function for DOT found

Do you think is related ?

Mido-sys commented 3 years ago

@paganotoni , yes we will need to add this feature to allow function chain calls. Shouldn't be too hard. I will send a new pull request for this feature.

paganotoni commented 3 years ago

Added #139 to keep track of this one. Thank you @Mido-sys