#426·goja

Public interface for ToValue customization ?

Author: vanackereCreated Aug 20, 2022Updated Sep 30, 2024

Hi,

It would be really nice if the (r *Runtime) ToValue method could support an interface to allow custom conversion to goja values, working with reflect-based objects.

My use case would be to be able to declare custom generics types in go (like Option[T], Result[T,E]) and have them automatically handled anywhere in go values (either as field of structs or values in mappings for example).

There is already an internal interface with the right signature:

go
type valueContainer interface {
	toValue(*Runtime) Value
}

However there are two problems with this interface:

  1. It is currently private
  2. AFAICT for my solution to work, this interface should also be checked in (r *Runtime) reflectValueToValue

What do you think about this ?