spf13--viper/internal/encoding/cue/codec.go
Adrian Maurer 29eaef3bf7
feat: add cue encoder (#1)
* feat: add cue encoder

* wip: cleanup and comments

* fix: extensions

fix: remove print statement (#3)

fix: marshal val to byte

fix: straight decode
2021-07-22 11:29:09 -04:00

28 lines
626 B
Go

package cue
import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/format"
)
// Codec implements the encoding.Encoder and encoding.Decoder interfaces for CUE encoding.
type Codec struct{}
func (Codec) Encode(v interface{}) ([]byte, error) {
context := cuecontext.New()
val := context.Encode(v)
if val.Err() != nil {
return nil, val.Err()
}
return format.Node(val.Syntax(cue.ResolveReferences(true)))
}
func (Codec) Decode(b []byte, v interface{}) error {
context := cuecontext.New()
val := context.CompileBytes(b)
if val.Err() != nil {
return val.Err()
}
return val.Decode(v)
}