google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.34k stars 212 forks source link

Support for type-specific unpack logic #267

Closed landism closed 4 years ago

landism commented 4 years ago

The project I work on defines a number of starlark builtins with parameter types not quite supported by starlark.UnpackArgs, e.g., we want an argument of type Union[None|str], or Union[str|List[str]].

Right now, we're unpacking these args into a variable of type starlark.Value and then doing post-unpack validation / casting, which seems like a lot of distracting noise in the builtin definitions.

A solution that comes to mind is to add a new interface starlark.Unpackable with the method Unpack(starlark.Value, ptr interface{}), and have unpackOneArg's switch statement call that if it exists.

This would allow us to have UnpackArgs handle arg validation for custom types as it already does for default types.

I'm happy to write this, but wanted to check on interest or alternatives before doing so.

Thanks!

alandonovan commented 4 years ago

Sounds reasonable. Care to send a sketch of your idea so we can discuss it in more detail?