Closed edigaryev closed 3 years ago
I am wary of defining a standard function for converting Starlark values to Go values, or the reverse, because there are so many different ways to do it. For example, which Go integer type should a Starlark int value be mapped to? The smallest one capable of representing the value? int
? *big.Int
? How do you represent in Go a Starlark dict whose keys are (string, int) pairs? There are many sensible answers to such questions, and that suggests that no particular one belongs in the standard package. (A secondary concern is that such conversions invite very inefficient deep copying.)
Fortunately, there is no need for these functions to live in this repository. You can define your own conversion functions that do exactly what you want. If many users want the same function, it can be shared in a utility package. If the passage of time reveals a particularly good implementation, then let's talk about making it standard.
(By the way, see https://github.com/google/starlark-go/commit/ac23acb182e187e6835e4c0cc42205fe37a05624 for a standard Starlark JSON package that encoding and decoding operations for all the data types of JSON.)
Thanks for the quick reply 😀
Just wanted to vent the idea before actually making a PR and I think you have a point. In order to provide a one-size-fits-all implementation a bunch of options would probably need to be introduced that would control the conversion behavior.
Will close this for now.
Similarly to how google.golang.org/protobuf/types/known/structpb does the conversion:
interface{}
: https://pkg.go.dev/google.golang.org/protobuf/types/known/structpb#Value.AsInterfaceinterface{}
: https://pkg.go.dev/google.golang.org/protobuf/types/known/structpb#NewValueFor user-defined
Value
's an interface can be provided, similar to Golang's JSON Marshaler and Unmarshaler interfaces:The
error
allows handling the case when an attempt to callToInterface()
is made on an outerValue
which contains user-definedValue
inside withoutToInterface
implemented.These methods would simplify passing arbitrary YAML/JSON-compatible data structures as ordinary
dict
's/list
's/etc to Starlark functions from Golang (e.g. Cirrus CLI uses Starlark to implement build/task hooks, and this conversion is currently done manually).