Skip to content

Commit

Permalink
Fixed panic ins typed nil coalesce
Browse files Browse the repository at this point in the history
  • Loading branch information
generikvault committed Jun 3, 2021
1 parent 4f5f9c0 commit 02e0f36
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions gval.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ var full = NewLanguage(arithmetic, bitmask, text, propositionalLogic, ljson,
InfixOperator("in", inArray),

InfixShortCircuit("??", func(a interface{}) (interface{}, bool) {
return a, a != false && a != nil
v := reflect.ValueOf(a)
return a, a != nil && !v.IsZero()
}),
InfixOperator("??", func(a, b interface{}) (interface{}, error) {
if a == false || a == nil {
if v := reflect.ValueOf(a); a == nil || v.IsZero() {
return b, nil
}
return a, nil
Expand Down
16 changes: 16 additions & 0 deletions gval_parameterized_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,22 @@ func TestParameterized(t *testing.T) {
},
want: 1.,
},
{
name: "coalesce typed nil 0",
expression: `ProjectID ?? 0`,
parameter: struct {
ProjectID *uint
}{},
want: 0.,
},
{
name: "coalesce typed nil 99",
expression: `ProjectID ?? 99`,
parameter: struct {
ProjectID *uint
}{},
want: 99.,
},
},
t,
)
Expand Down
6 changes: 6 additions & 0 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func convertToBool(o interface{}) (bool, bool) {
v := reflect.ValueOf(o)
for o != nil && v.Kind() == reflect.Ptr {
v = v.Elem()
if !v.IsValid() {
return false, false
}
o = v.Interface()
}
if o == false || o == nil || o == "false" || o == "FALSE" {
Expand Down Expand Up @@ -173,6 +176,9 @@ func convertToFloat(o interface{}) (float64, bool) {
v := reflect.ValueOf(o)
for o != nil && v.Kind() == reflect.Ptr {
v = v.Elem()
if !v.IsValid() {
return 0, false
}
o = v.Interface()
}
switch v.Kind() {
Expand Down

0 comments on commit 02e0f36

Please sign in to comment.