gobuffalo / plush

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

Fix panic when accessing a map with a key type that's not comparable with map index #177

Closed Mido-sys closed 1 year ago

Mido-sys commented 1 year ago

What is being done in this PR?

Fixes issue#176

What are the main choices made to get to this solution?

If the map key is not of interface{} type then the index and map key type should be the same. If not then an error will be returned "line 1: cannot use first (string constant) as int value in map index"

List the manual test cases you've covered before sending this PR:

Created two new tests under hashes_test.go.

  1. Test_Render_Hash_Key_Interface : This test should pass as the map key type is of a type interface{}
  2. Test_Render_Hash_Key_Int_With_String_Index: This test should return an error because the map key type is int and accessing it with a string.
paganotoni commented 1 year ago

@Mido-sys, this is great! Could we add a test case that checks when the map has a key type of a non-empty interface?

Mido-sys commented 1 year ago

@paganotoni, I think I covered it in this test:

func Test_Render_Hash_Key_Interface(t *testing.T) {
    r := require.New(t)

    input := `<%= m["first"]%>`
    s, err := Render(input, NewContextWith(map[string]interface{}{

        "m": map[interface{}]bool{"first": true},
    }))
    r.NoError(err)
    r.Equal("true", s)
}
paganotoni commented 1 year ago

Yes. you're right @Mido-sys! Thanks for pointing it out.