|
|
@ -98,49 +98,55 @@ func ParseValue(val *Value) (interface{}, error) { |
|
|
|
// Return value of provided key
|
|
|
|
return Vars[*val.VarVal], nil |
|
|
|
} else if val.Expr != nil { |
|
|
|
// Parse value of left side of expression
|
|
|
|
left, _ := callIfFunc(ParseValue(val.Expr.Left)) |
|
|
|
// Return evaluated expression
|
|
|
|
return evalExpr(*val.Expr) |
|
|
|
} |
|
|
|
return nil, nil |
|
|
|
} |
|
|
|
|
|
|
|
// Evaluate given expression, returning its value and optionally an error
|
|
|
|
func evalExpr(expression Expression) (interface{}, error) { |
|
|
|
// Parse value of left side of expression
|
|
|
|
left, _ := callIfFunc(ParseValue(expression.Left)) |
|
|
|
// If value is string, requote
|
|
|
|
if isStr(left) { |
|
|
|
left = requoteStr(left.(string)) |
|
|
|
} |
|
|
|
// Create new nil string
|
|
|
|
var right string |
|
|
|
// For every right gsegment
|
|
|
|
for _, segment := range expression.RightSegs { |
|
|
|
// Parse value of right segment, calling it if it is a function
|
|
|
|
rVal, _ := callIfFunc(ParseValue(segment.Right)) |
|
|
|
// If value is string, requote
|
|
|
|
if isStr(left) { |
|
|
|
left = requoteStr(left.(string)) |
|
|
|
if isStr(rVal) { |
|
|
|
rVal = requoteStr(rVal.(string)) |
|
|
|
} |
|
|
|
// Create new nil string
|
|
|
|
var right string |
|
|
|
// For every right segment
|
|
|
|
for _, segment := range val.Expr.RightSegs { |
|
|
|
// Parse value of right segment, calling it if it is a function
|
|
|
|
rVal, _ := callIfFunc(ParseValue(segment.Right)) |
|
|
|
// If value is string, requote
|
|
|
|
if isStr(rVal) { |
|
|
|
rVal = requoteStr(rVal.(string)) |
|
|
|
} |
|
|
|
// Append right segment to right string
|
|
|
|
right = right + fmt.Sprintf( |
|
|
|
" %s %v", |
|
|
|
segment.Op, |
|
|
|
rVal, |
|
|
|
) |
|
|
|
} |
|
|
|
// Create string expression from segments and operator
|
|
|
|
exp := fmt.Sprintf( |
|
|
|
"%v %s", |
|
|
|
left, |
|
|
|
right, |
|
|
|
// Append right segment to right string
|
|
|
|
right = right + fmt.Sprintf( |
|
|
|
" %s %v", |
|
|
|
segment.Op, |
|
|
|
rVal, |
|
|
|
) |
|
|
|
// Compile string expression
|
|
|
|
program, err := expr.Compile(strings.ReplaceAll(exp, "^", "**")) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
// Run expression
|
|
|
|
out, err := expr.Run(program, Vars) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
// Return expression output value
|
|
|
|
return out, nil |
|
|
|
} |
|
|
|
return nil, nil |
|
|
|
// Create string expression from segments and operator
|
|
|
|
exp := fmt.Sprintf( |
|
|
|
"%v %s", |
|
|
|
left, |
|
|
|
right, |
|
|
|
) |
|
|
|
// Compile string expression
|
|
|
|
program, err := expr.Compile(strings.ReplaceAll(exp, "^", "**")) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
// Run expression
|
|
|
|
out, err := expr.Run(program, Vars) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
// Return expression output value
|
|
|
|
return out, nil |
|
|
|
} |
|
|
|
|
|
|
|
// Add quotes to an unquoted string
|
|
|
|