2020-12-26 15:32:45 +00:00
|
|
|
package cue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cuelang.org/go/cue"
|
|
|
|
"github.com/ghodss/yaml"
|
2021-01-20 21:01:10 +00:00
|
|
|
"github.com/asim/go-micro/v3/config/encoder"
|
2020-12-26 15:32:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type cueEncoder struct{}
|
|
|
|
|
|
|
|
func (c cueEncoder) Encode(v interface{}) ([]byte, error) {
|
|
|
|
return yaml.Marshal(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c cueEncoder) Decode(d []byte, v interface{}) error {
|
|
|
|
var r cue.Runtime
|
|
|
|
instance, err := r.Compile("config", d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
j, err := instance.Value().MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return yaml.Unmarshal(j, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c cueEncoder) String() string {
|
|
|
|
return "cue"
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEncoder : create new cueEncoder
|
|
|
|
func NewEncoder() encoder.Encoder {
|
|
|
|
return cueEncoder{}
|
|
|
|
}
|