goja icon indicating copy to clipboard operation
goja copied to clipboard

Exposing the entries of a custom `map` instead of its methods

Open buschtoens opened this issue 1 year ago • 1 comments

Since 4e76d8820401fbc3a2d128e5fcc4674519fe9850 any map type implementing methods will cause those methods to be exposed to JS, but not any of the map entries, to avoid ambiguity.

Now I'm in a scenario where I need the exact opposite behavior: all the map entries should be exposed as properties and none of the methods.

This could be solved with a customizable ToValue: #426

Would you prefer solving this issue via #426 or would you accept a PR with an alternative solution for this case specifically?

For instance, we could use "marker" interfaces:

type ExportEntries interface {
  GojaExportEntries()
}
type Foo map[string]string

// GojaExportEntries is used as a marker for goja to export the map entries instead of its methods.
func (_ Foo) GojaExportEntries() {}

// Without the above, the map would always be exported as an object with this method only.
func (f Foo) SomeMethod() {}

buschtoens avatar Sep 30 '24 15:09 buschtoens

I'd prefer the solution described in #426 as it's more generic and can be allied to 3rd party or built-in types.

dop251 avatar Oct 02 '24 15:10 dop251