This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
dagger/dagger/spec.go
dubo-dubon-duponey 0097c73931
Fix crash
Signed-off-by: dubo-dubon-duponey <dubodubonduponey+github@pm.me>
2021-01-11 11:09:08 -08:00

49 lines
1015 B
Go

package dagger
import (
"fmt"
"cuelang.org/go/cue"
cueerrors "cuelang.org/go/cue/errors"
)
// Cue spec validator
type Spec struct {
root *Value
}
// eg. Validate(op, "#Op")
func (s Spec) Validate(v *Value, defpath string) (err error) {
// Expand cue errors to get full details
// FIXME: there is probably a cleaner way to do this.
defer func() {
if err != nil {
//debugf("ERROR while validating %v against %v err=%q", v, defpath, err)
err = fmt.Errorf("%s", cueerrors.Details(err, nil))
}
}()
// Lookup def by name, eg. "#Script" or "#Copy"
// See dagger/spec.cue
def := s.root.Get(defpath)
if err := def.Validate(); err != nil {
return err
}
merged := def.Unwrap().Fill(v.Value)
if err := merged.Err(); err != nil {
return err
}
if err := merged.Validate(cue.Final()); err != nil {
return err
}
return nil
}
func (s Spec) Match(v *Value, defpath string) bool {
return s.Validate(v, defpath) == nil
}
func (s Spec) Get(target string) *Value {
return s.root.Get(target)
}