mirror of
https://github.com/spf13/viper
synced 2024-12-25 13:07:02 +00:00
29eaef3bf7
* feat: add cue encoder * wip: cleanup and comments * fix: extensions fix: remove print statement (#3) fix: marshal val to byte fix: straight decode
28 lines
626 B
Go
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)
|
|
}
|