Tweak expr record value selection and bool casting
* properly handle casting of false values (empty string) * add support for record.values.xyz expressions where values is not set
This commit is contained in:
@@ -84,7 +84,7 @@ const (
|
||||
func Workflow(log *zap.Logger, corredorOpt options.CorredorOpt, opt options.WorkflowOpt) *workflow {
|
||||
return &workflow{
|
||||
log: log,
|
||||
opt: opt,
|
||||
opt: opt,
|
||||
actionlog: DefaultActionlog,
|
||||
store: DefaultStore,
|
||||
ac: DefaultAccessControl,
|
||||
|
||||
@@ -75,9 +75,6 @@ func CastToComposeRecord(val interface{}) (out *types.Record, err error) {
|
||||
val = &types.Record{}
|
||||
}
|
||||
|
||||
if val.Values == nil {
|
||||
val.Values = types.RecordValueSet{}
|
||||
}
|
||||
return val, nil
|
||||
case map[string]interface{}:
|
||||
out = &types.Record{}
|
||||
@@ -127,10 +124,6 @@ func (t *ComposeRecord) SelectGVal(_ context.Context, k string) (interface{}, er
|
||||
t.value.Values = types.RecordValueSet{}
|
||||
}
|
||||
|
||||
if t.value.Values.Len() == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &ComposeRecordValues{t.value}, nil
|
||||
}
|
||||
|
||||
@@ -254,6 +247,15 @@ func (t *ComposeRecordValues) SelectGVal(_ context.Context, k string) (interface
|
||||
return composeRecordValuesGValSelector(t.value, k)
|
||||
}
|
||||
|
||||
// IsEmpty implements pkg/expr.empty requirements to be able to determine if
|
||||
// the value is empty.
|
||||
//
|
||||
// This is needed cor cases when we are working with empty records, but are trying
|
||||
// to access their values.
|
||||
func (t *ComposeRecordValues) IsEmpty() bool {
|
||||
return t == nil || t.value == nil || len(t.value.Values) == 0
|
||||
}
|
||||
|
||||
// Select is field accessor for *types.Record
|
||||
//
|
||||
// Similar to SelectGVal but returns typed values
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/expr"
|
||||
"github.com/cortezaproject/corteza-server/pkg/filter"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cast"
|
||||
@@ -73,7 +74,7 @@ func (v RecordValue) Cast(f *ModuleField) (interface{}, error) {
|
||||
return cast.ToTimeE(v.Value)
|
||||
|
||||
case f.IsBoolean():
|
||||
return cast.ToBoolE(v.Value)
|
||||
return expr.CastToBoolean(v.Value)
|
||||
|
||||
case f.IsNumeric():
|
||||
if f.Options.Precision() == 0 {
|
||||
|
||||
@@ -7,6 +7,12 @@ import (
|
||||
"github.com/PaesslerAG/gval"
|
||||
)
|
||||
|
||||
type (
|
||||
empty interface {
|
||||
IsEmpty() bool
|
||||
}
|
||||
)
|
||||
|
||||
func GenericFunctions() []gval.Language {
|
||||
return []gval.Language{
|
||||
gval.Function("coalesce", coalesce),
|
||||
@@ -57,6 +63,10 @@ func isEmpty(i interface{}) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
if c, ok := i.(empty); ok {
|
||||
return c.IsEmpty()
|
||||
}
|
||||
|
||||
switch reflect.TypeOf(i).Kind() {
|
||||
case reflect.Slice, reflect.Array, reflect.Map:
|
||||
return reflect.ValueOf(i).Len() == 0
|
||||
|
||||
Reference in New Issue
Block a user