3
0

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:
Tomaž Jerman
2021-10-01 12:39:20 +02:00
parent 2ee926940a
commit 2def92d41f
4 changed files with 22 additions and 9 deletions
+1 -1
View File
@@ -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,
+9 -7
View File
@@ -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
+2 -1
View File
@@ -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 {
+10
View File
@@ -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