2024-02-18 20:37:09 +00:00
|
|
|
package memoryexecutor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pikami/cosmium/parsers"
|
|
|
|
)
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_StringEquals(arguments []interface{}, row RowType) bool {
|
|
|
|
str1 := c.parseString(arguments[0], row)
|
|
|
|
str2 := c.parseString(arguments[1], row)
|
|
|
|
ignoreCase := c.getBoolFlag(arguments, row)
|
2024-02-18 20:37:09 +00:00
|
|
|
|
|
|
|
if ignoreCase {
|
|
|
|
return strings.EqualFold(str1, str2)
|
|
|
|
}
|
|
|
|
|
|
|
|
return str1 == str2
|
|
|
|
}
|
2024-02-21 18:16:52 +00:00
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_Contains(arguments []interface{}, row RowType) bool {
|
|
|
|
str1 := c.parseString(arguments[0], row)
|
|
|
|
str2 := c.parseString(arguments[1], row)
|
|
|
|
ignoreCase := c.getBoolFlag(arguments, row)
|
2024-02-21 18:25:14 +00:00
|
|
|
|
2024-02-21 18:46:08 +00:00
|
|
|
if ignoreCase {
|
|
|
|
str1 = strings.ToLower(str1)
|
|
|
|
str2 = strings.ToLower(str2)
|
|
|
|
}
|
2024-02-21 18:25:14 +00:00
|
|
|
|
2024-02-21 18:46:08 +00:00
|
|
|
return strings.Contains(str1, str2)
|
|
|
|
}
|
2024-02-21 18:25:14 +00:00
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_EndsWith(arguments []interface{}, row RowType) bool {
|
|
|
|
str1 := c.parseString(arguments[0], row)
|
|
|
|
str2 := c.parseString(arguments[1], row)
|
|
|
|
ignoreCase := c.getBoolFlag(arguments, row)
|
2024-02-21 18:25:14 +00:00
|
|
|
|
2024-02-21 18:46:08 +00:00
|
|
|
if ignoreCase {
|
|
|
|
str1 = strings.ToLower(str1)
|
|
|
|
str2 = strings.ToLower(str2)
|
2024-02-21 18:25:14 +00:00
|
|
|
}
|
|
|
|
|
2024-02-21 18:46:08 +00:00
|
|
|
return strings.HasSuffix(str1, str2)
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_StartsWith(arguments []interface{}, row RowType) bool {
|
|
|
|
str1 := c.parseString(arguments[0], row)
|
|
|
|
str2 := c.parseString(arguments[1], row)
|
|
|
|
ignoreCase := c.getBoolFlag(arguments, row)
|
2024-02-21 18:46:08 +00:00
|
|
|
|
2024-02-21 18:25:14 +00:00
|
|
|
if ignoreCase {
|
|
|
|
str1 = strings.ToLower(str1)
|
|
|
|
str2 = strings.ToLower(str2)
|
|
|
|
}
|
|
|
|
|
2024-02-21 18:46:08 +00:00
|
|
|
return strings.HasPrefix(str1, str2)
|
2024-02-21 18:25:14 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_Concat(arguments []interface{}, row RowType) string {
|
2024-02-21 18:16:52 +00:00
|
|
|
result := ""
|
|
|
|
|
|
|
|
for _, arg := range arguments {
|
|
|
|
if selectItem, ok := arg.(parsers.SelectItem); ok {
|
2024-02-25 20:13:04 +00:00
|
|
|
value := c.getFieldValue(selectItem, row)
|
2024-02-21 18:16:52 +00:00
|
|
|
result += convertToString(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_IndexOf(arguments []interface{}, row RowType) int {
|
|
|
|
str1 := c.parseString(arguments[0], row)
|
|
|
|
str2 := c.parseString(arguments[1], row)
|
2024-02-22 20:12:52 +00:00
|
|
|
|
|
|
|
start := 0
|
|
|
|
if len(arguments) > 2 && arguments[2] != nil {
|
2024-02-25 20:13:04 +00:00
|
|
|
if startPos, ok := c.getFieldValue(arguments[2].(parsers.SelectItem), row).(int); ok {
|
2024-02-22 20:12:52 +00:00
|
|
|
start = startPos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(str1) <= start {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
str1 = str1[start:]
|
|
|
|
result := strings.Index(str1, str2)
|
|
|
|
|
|
|
|
if result == -1 {
|
|
|
|
return result
|
|
|
|
} else {
|
|
|
|
return result + start
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_ToString(arguments []interface{}, row RowType) string {
|
|
|
|
value := c.getFieldValue(arguments[0].(parsers.SelectItem), row)
|
2024-02-22 22:11:14 +00:00
|
|
|
return convertToString(value)
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_Upper(arguments []interface{}, row RowType) string {
|
|
|
|
value := c.getFieldValue(arguments[0].(parsers.SelectItem), row)
|
2024-02-24 18:00:47 +00:00
|
|
|
return strings.ToUpper(convertToString(value))
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_Lower(arguments []interface{}, row RowType) string {
|
|
|
|
value := c.getFieldValue(arguments[0].(parsers.SelectItem), row)
|
2024-02-24 18:00:47 +00:00
|
|
|
return strings.ToLower(convertToString(value))
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_Left(arguments []interface{}, row RowType) string {
|
2024-02-24 19:24:20 +00:00
|
|
|
var ok bool
|
|
|
|
var length int
|
2024-02-25 20:13:04 +00:00
|
|
|
str := c.parseString(arguments[0], row)
|
|
|
|
lengthEx := c.getFieldValue(arguments[1].(parsers.SelectItem), row)
|
2024-02-24 19:24:20 +00:00
|
|
|
|
|
|
|
if length, ok = lengthEx.(int); !ok {
|
|
|
|
fmt.Println("strings_Left - got parameters of wrong type")
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if length <= 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(str) <= length {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
return str[:length]
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_Length(arguments []interface{}, row RowType) int {
|
|
|
|
str := c.parseString(arguments[0], row)
|
2024-02-24 19:24:20 +00:00
|
|
|
return len(str)
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_LTrim(arguments []interface{}, row RowType) string {
|
|
|
|
str := c.parseString(arguments[0], row)
|
2024-02-24 19:24:20 +00:00
|
|
|
return strings.TrimLeft(str, " ")
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_Replace(arguments []interface{}, row RowType) string {
|
|
|
|
str := c.parseString(arguments[0], row)
|
|
|
|
oldStr := c.parseString(arguments[1], row)
|
|
|
|
newStr := c.parseString(arguments[2], row)
|
2024-02-24 19:24:20 +00:00
|
|
|
return strings.Replace(str, oldStr, newStr, -1)
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_Replicate(arguments []interface{}, row RowType) string {
|
2024-02-24 19:24:20 +00:00
|
|
|
var ok bool
|
|
|
|
var times int
|
2024-02-25 20:13:04 +00:00
|
|
|
str := c.parseString(arguments[0], row)
|
|
|
|
timesEx := c.getFieldValue(arguments[1].(parsers.SelectItem), row)
|
2024-02-24 19:24:20 +00:00
|
|
|
|
|
|
|
if times, ok = timesEx.(int); !ok {
|
|
|
|
fmt.Println("strings_Replicate - got parameters of wrong type")
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if times <= 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(str) <= times {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Repeat(str, times)
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_Reverse(arguments []interface{}, row RowType) string {
|
|
|
|
str := c.parseString(arguments[0], row)
|
2024-02-24 19:24:20 +00:00
|
|
|
runes := []rune(str)
|
|
|
|
|
|
|
|
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
runes[i], runes[j] = runes[j], runes[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(runes)
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_Right(arguments []interface{}, row RowType) string {
|
2024-02-24 19:24:20 +00:00
|
|
|
var ok bool
|
|
|
|
var length int
|
2024-02-25 20:13:04 +00:00
|
|
|
str := c.parseString(arguments[0], row)
|
|
|
|
lengthEx := c.getFieldValue(arguments[1].(parsers.SelectItem), row)
|
2024-02-24 19:24:20 +00:00
|
|
|
|
|
|
|
if length, ok = lengthEx.(int); !ok {
|
|
|
|
fmt.Println("strings_Right - got parameters of wrong type")
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if length <= 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(str) <= length {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
return str[len(str)-length:]
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_RTrim(arguments []interface{}, row RowType) string {
|
|
|
|
str := c.parseString(arguments[0], row)
|
2024-02-24 19:24:20 +00:00
|
|
|
return strings.TrimRight(str, " ")
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_Substring(arguments []interface{}, row RowType) string {
|
2024-02-24 19:24:20 +00:00
|
|
|
var ok bool
|
|
|
|
var startPos int
|
|
|
|
var length int
|
2024-02-25 20:13:04 +00:00
|
|
|
str := c.parseString(arguments[0], row)
|
|
|
|
startPosEx := c.getFieldValue(arguments[1].(parsers.SelectItem), row)
|
|
|
|
lengthEx := c.getFieldValue(arguments[2].(parsers.SelectItem), row)
|
2024-02-24 19:24:20 +00:00
|
|
|
|
|
|
|
if startPos, ok = startPosEx.(int); !ok {
|
|
|
|
fmt.Println("strings_Substring - got start parameters of wrong type")
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if length, ok = lengthEx.(int); !ok {
|
|
|
|
fmt.Println("strings_Substring - got length parameters of wrong type")
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if startPos >= len(str) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
endPos := startPos + length
|
|
|
|
if endPos > len(str) {
|
|
|
|
endPos = len(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
return str[startPos:endPos]
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) strings_Trim(arguments []interface{}, row RowType) string {
|
|
|
|
str := c.parseString(arguments[0], row)
|
2024-02-24 19:24:20 +00:00
|
|
|
return strings.TrimSpace(str)
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) getBoolFlag(arguments []interface{}, row RowType) bool {
|
2024-02-21 18:46:08 +00:00
|
|
|
ignoreCase := false
|
|
|
|
if len(arguments) > 2 && arguments[2] != nil {
|
|
|
|
ignoreCaseItem := arguments[2].(parsers.SelectItem)
|
2024-02-25 20:13:04 +00:00
|
|
|
if value, ok := c.getFieldValue(ignoreCaseItem, row).(bool); ok {
|
2024-02-21 18:46:08 +00:00
|
|
|
ignoreCase = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ignoreCase
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) parseString(argument interface{}, row RowType) string {
|
2024-02-21 18:46:08 +00:00
|
|
|
exItem := argument.(parsers.SelectItem)
|
2024-02-25 20:13:04 +00:00
|
|
|
ex := c.getFieldValue(exItem, row)
|
2024-02-21 18:46:08 +00:00
|
|
|
if str1, ok := ex.(string); ok {
|
|
|
|
return str1
|
|
|
|
}
|
|
|
|
|
2024-02-24 18:00:47 +00:00
|
|
|
fmt.Println("StringEquals got parameters of wrong type")
|
2024-02-21 18:46:08 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2024-02-21 18:16:52 +00:00
|
|
|
func convertToString(value interface{}) string {
|
|
|
|
switch v := value.(type) {
|
|
|
|
case string:
|
|
|
|
return v
|
|
|
|
case int:
|
|
|
|
return fmt.Sprintf("%d", v)
|
|
|
|
case float32, float64:
|
|
|
|
return fmt.Sprintf("%f", v)
|
2024-02-22 22:11:14 +00:00
|
|
|
case bool:
|
|
|
|
return fmt.Sprintf("%t", v)
|
2024-02-21 18:16:52 +00:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|