Merge pull request #57 from blocklayerhq/clean-abort-non-concrete
script: abort execution with a warning when the op is not concrete
This commit is contained in:
commit
a9698ccd18
@ -4,6 +4,11 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrAbortExecution = errors.New("execution stopped")
|
||||
)
|
||||
|
||||
type Script struct {
|
||||
@ -25,8 +30,13 @@ func (s *Script) Execute(ctx context.Context, fs FS, out Fillable) (FS, error) {
|
||||
// If op not concrete, interrupt without error.
|
||||
// This allows gradual resolution:
|
||||
// compute what you can compute.. leave the rest incomplete.
|
||||
if !v.IsConcreteR() {
|
||||
return nil
|
||||
if err := v.IsConcreteR(); err != nil {
|
||||
log.
|
||||
Ctx(ctx).
|
||||
Warn().
|
||||
Err(err).
|
||||
Msg("script is unspecified, aborting execution")
|
||||
return ErrAbortExecution
|
||||
}
|
||||
op, err := v.Op()
|
||||
if err != nil {
|
||||
@ -38,6 +48,11 @@ func (s *Script) Execute(ctx context.Context, fs FS, out Fillable) (FS, error) {
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
// If the execution was gracefully stopped, do not return an error
|
||||
if err == ErrAbortExecution {
|
||||
return fs, nil
|
||||
}
|
||||
return fs, err
|
||||
}
|
||||
|
||||
|
@ -163,30 +163,8 @@ func (v *Value) MergeTarget(x interface{}, target string) (*Value, error) {
|
||||
}
|
||||
|
||||
// Recursive concreteness check.
|
||||
// Return false if v is not concrete, or contains any
|
||||
// non-concrete fields or items.
|
||||
func (v *Value) IsConcreteR() bool {
|
||||
// FIXME: use Value.Walk
|
||||
if it, err := v.Fields(); err == nil {
|
||||
for it.Next() {
|
||||
w := v.Wrap(it.Value())
|
||||
if !w.IsConcreteR() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
if it, err := v.List(); err == nil {
|
||||
for it.Next() {
|
||||
w := v.Wrap(it.Value())
|
||||
if !w.IsConcreteR() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
dv, _ := v.val.Default()
|
||||
return v.val.IsConcrete() || dv.IsConcrete()
|
||||
func (v *Value) IsConcreteR() error {
|
||||
return v.val.Validate(cue.Concrete(true))
|
||||
}
|
||||
|
||||
// Export concrete values to JSON. ignoring non-concrete values.
|
||||
|
Reference in New Issue
Block a user