-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revised transformer conds implementation
- Loading branch information
Showing
9 changed files
with
169 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package toolkit | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/expr-lang/expr" | ||
"github.com/expr-lang/expr/vm" | ||
) | ||
|
||
type WhenCond struct { | ||
rc *RecordContext | ||
whenCond *vm.Program | ||
when string | ||
} | ||
|
||
func NewWhenCond(when string, driver *Driver) (*WhenCond, ValidationWarnings) { | ||
var ( | ||
rc *RecordContext | ||
whenCond *vm.Program | ||
warnings ValidationWarnings | ||
) | ||
if when != "" { | ||
whenCond, rc, warnings = compileCond(when, driver) | ||
if warnings.IsFatal() { | ||
return nil, warnings | ||
} | ||
} | ||
return &WhenCond{ | ||
rc: rc, | ||
whenCond: whenCond, | ||
when: when, | ||
}, nil | ||
} | ||
|
||
func (wc *WhenCond) Evaluate(r *Record) (bool, error) { | ||
if wc.whenCond == nil { | ||
return true, nil | ||
} | ||
wc.rc.SetRecord(r) | ||
|
||
output, err := expr.Run(wc.whenCond, nil) | ||
if err != nil { | ||
return false, fmt.Errorf("unable to evaluate when condition: %w", err) | ||
} | ||
|
||
cond, ok := output.(bool) | ||
if ok { | ||
return cond, nil | ||
} | ||
|
||
return false, fmt.Errorf("when condition should return boolean, got (%T) and value %+v", cond, cond) | ||
} | ||
|
||
// compileCond compiles the when condition and returns the compiled program and the record context | ||
// with the functions for the columns. The functions represent the column names and return the column values. | ||
func compileCond(whenCond string, driver *Driver) (*vm.Program, *RecordContext, ValidationWarnings) { | ||
if whenCond == "" { | ||
return nil, nil, nil | ||
} | ||
rc, funcs := newRecordContext(driver) | ||
|
||
cond, err := expr.Compile(whenCond, funcs...) | ||
if err != nil { | ||
return nil, nil, ValidationWarnings{ | ||
NewValidationWarning(). | ||
SetSeverity(ErrorValidationSeverity). | ||
AddMeta("Error", err.Error()). | ||
SetMsg("unable to compile when condition"), | ||
} | ||
} | ||
|
||
return cond, rc, nil | ||
} | ||
|
||
func newRecordContext(driver *Driver) (*RecordContext, []expr.Option) { | ||
var funcs []expr.Option | ||
rctx := NewRecordContext() | ||
for _, c := range driver.Table.Columns { | ||
|
||
f := expr.Function( | ||
fmt.Sprintf("__%s", c.Name), | ||
func(name string) func(params ...any) (any, error) { | ||
return func(params ...any) (any, error) { | ||
return rctx.GetColumnRawValue(name) | ||
} | ||
}(c.Name), | ||
) | ||
funcs = append(funcs, f) | ||
} | ||
|
||
return rctx, funcs | ||
} |
Oops, something went wrong.