Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 10, 2023

Conversation

Mido-sys
Copy link
Contributor

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
Copy link
Member

@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?

@paganotoni paganotoni self-requested a review March 10, 2023 12:52
@Mido-sys
Copy link
Contributor Author

@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
Copy link
Member

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

Copy link
Member

@paganotoni paganotoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for covering this one @Mido-sys

@paganotoni paganotoni merged commit 082e553 into gobuffalo:main Mar 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Plush Panics when accessing a map with a key that's not comparable with the map index
2 participants