Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
encoding/gocodec: Validate helper function
Browse files Browse the repository at this point in the history
Change-Id: I2365180fbe4d7742a2bd392e5b03d06e11218cb7
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6342
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jun 16, 2020
1 parent 019060d commit cb21170
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions encoding/gocode/gocodec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ func (c *Codec) Encode(v cue.Value, x interface{}) error {
return v.Decode(x)
}

var defaultCodec = New(&cue.Runtime{}, nil)

// Validate calls Validate on a default Codec for the type of x.
func Validate(x interface{}) error {
c := defaultCodec
c.mutex.RLock()
defer c.mutex.RUnlock()

r := defaultCodec.runtime
v, err := fromGoType(r, x)
if err != nil {
return err
}
w, err := fromGoValue(r, x, false)
if err != nil {
return err
}
v = v.Unify(w)
if err := v.Validate(); err != nil {
return err
}
return nil
}

// Validate checks whether x satisfies the constraints defined by v.
//
// The given value must be created using the same Runtime with which c was
Expand Down

0 comments on commit cb21170

Please sign in to comment.