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

Go structs with pointers to maps return unexpected type errors #533

Open
rorydbain opened this issue Oct 21, 2024 · 2 comments
Open

Go structs with pointers to maps return unexpected type errors #533

rorydbain opened this issue Oct 21, 2024 · 2 comments

Comments

@rorydbain
Copy link

If I have a Go struct containing a pointer to a map, I'm unable to use that structure in Otto.

Here's an example test case:

func TestGoStructMapPointerField(t *testing.T) {
	type S struct {
		A *map[string]any `json:"a"`
	}

	tt(t, func() {
		test, vm := test()
		vm.Set("s", S{A: &map[string]any{
			"one": 1,
		}})

		test(`'a' in s`, true)
		test(`s.a.one`, 1)
	})
}

And the resulting error:

--- FAIL: TestGoStructMapPointerField (0.00s)
panic: TypeError: invalid value (map): missing runtime: map[one:1] (map[string]interface {}) [recovered]
	panic: TypeError: invalid value (map): missing runtime: map[one:1] (map[string]interface {}) [recovered]
	panic: TypeError: invalid value (map): missing runtime: map[one:1] (map[string]interface {}) [recovered]
	panic: TypeError: invalid value (map): missing runtime: map[one:1] (map[string]interface {})
@stevenh
Copy link
Collaborator

stevenh commented Oct 21, 2024

While this should work, is there a specific reason why you're using a pointer to a map as its a reference type anyway?

@rorydbain
Copy link
Author

While this should work, is there a specific reason why you're using a pointer to a map as its a reference type anyway?

I had a usecase where *map[string]any was being used to indicate that the field was optional and might not be set - which I realise is niche but felt like a bit of a footgun.

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

No branches or pull requests

2 participants