2020-01-12 02:49:55 +00:00
|
|
|
package jsonnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/google/go-jsonnet"
|
|
|
|
)
|
|
|
|
|
2021-07-07 13:29:20 +00:00
|
|
|
// Decoder type can decode a jsonnet stream into the given output.
|
2020-01-12 02:49:55 +00:00
|
|
|
type Decoder struct {
|
|
|
|
r io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDecoder creates a new decoder.
|
|
|
|
func NewDecoder(r io.Reader) *Decoder {
|
|
|
|
return &Decoder{r: r}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode decodes the stream into the provide value.
|
|
|
|
func (d *Decoder) Decode(v interface{}) error {
|
2022-04-13 20:24:27 +00:00
|
|
|
b, err := io.ReadAll(d.r)
|
2020-01-12 02:49:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vm := jsonnet.MakeVM()
|
2021-07-07 13:29:20 +00:00
|
|
|
jsonStr, err := vm.EvaluateAnonymousSnippet("memory", string(b))
|
2020-01-12 02:49:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.Unmarshal([]byte(jsonStr), &v)
|
|
|
|
}
|