fix concurrency issues in Compiler.Compile and Value.Source

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-02-02 13:52:52 -08:00
parent cd0f21dbd2
commit 86aa031f92
2 changed files with 6 additions and 0 deletions

View File

@ -48,6 +48,9 @@ func (cc *Compiler) EmptyStruct() (*Value, error) {
} }
func (cc *Compiler) Compile(name string, src interface{}) (*Value, error) { func (cc *Compiler) Compile(name string, src interface{}) (*Value, error) {
cc.Lock()
defer cc.Unlock()
inst, err := cc.Cue().Compile(name, src) inst, err := cc.Cue().Compile(name, src)
if err != nil { if err != nil {
// FIXME: cleaner way to unwrap cue error details? // FIXME: cleaner way to unwrap cue error details?

View File

@ -255,6 +255,9 @@ func (v *Value) Validate(defs ...string) error {
// Return cue source for this value // Return cue source for this value
func (v *Value) Source() ([]byte, error) { func (v *Value) Source() ([]byte, error) {
v.cc.RLock()
defer v.cc.RUnlock()
return cueformat.Node(v.val.Eval().Syntax()) return cueformat.Node(v.val.Eval().Syntax())
} }