Skip to content

Commit cb21170

Browse files
committed
encoding/gocodec: Validate helper function
Change-Id: I2365180fbe4d7742a2bd392e5b03d06e11218cb7 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6342 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 019060d commit cb21170

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

encoding/gocode/gocodec/codec.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,30 @@ func (c *Codec) Encode(v cue.Value, x interface{}) error {
9393
return v.Decode(x)
9494
}
9595

96+
var defaultCodec = New(&cue.Runtime{}, nil)
97+
98+
// Validate calls Validate on a default Codec for the type of x.
99+
func Validate(x interface{}) error {
100+
c := defaultCodec
101+
c.mutex.RLock()
102+
defer c.mutex.RUnlock()
103+
104+
r := defaultCodec.runtime
105+
v, err := fromGoType(r, x)
106+
if err != nil {
107+
return err
108+
}
109+
w, err := fromGoValue(r, x, false)
110+
if err != nil {
111+
return err
112+
}
113+
v = v.Unify(w)
114+
if err := v.Validate(); err != nil {
115+
return err
116+
}
117+
return nil
118+
}
119+
96120
// Validate checks whether x satisfies the constraints defined by v.
97121
//
98122
// The given value must be created using the same Runtime with which c was

0 commit comments

Comments
 (0)