funcs_str.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // Copyright 2022-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package function
  15. import (
  16. "bytes"
  17. "fmt"
  18. "regexp"
  19. "strings"
  20. "unicode"
  21. "unicode/utf8"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. "github.com/lf-edge/ekuiper/pkg/ast"
  24. "github.com/lf-edge/ekuiper/pkg/cast"
  25. )
  26. func registerStrFunc() {
  27. builtins["concat"] = builtinFunc{
  28. fType: ast.FuncTypeScalar,
  29. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  30. var b bytes.Buffer
  31. for _, arg := range args {
  32. b.WriteString(cast.ToStringAlways(arg))
  33. }
  34. return b.String(), true
  35. },
  36. val: func(_ api.FunctionContext, args []ast.Expr) error {
  37. if len(args) == 0 {
  38. return fmt.Errorf("The arguments should be at least one.")
  39. }
  40. for i, a := range args {
  41. if ast.IsNumericArg(a) || ast.IsTimeArg(a) || ast.IsBooleanArg(a) {
  42. return ProduceErrInfo(i, "string")
  43. }
  44. }
  45. return nil
  46. },
  47. }
  48. builtins["endswith"] = builtinFunc{
  49. fType: ast.FuncTypeScalar,
  50. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  51. arg0, arg1 := cast.ToStringAlways(args[0]), cast.ToStringAlways(args[1])
  52. return strings.HasSuffix(arg0, arg1), true
  53. },
  54. val: ValidateTwoStrArg,
  55. check: returnFalseIfHasAnyNil,
  56. }
  57. builtins["indexof"] = builtinFunc{
  58. fType: ast.FuncTypeScalar,
  59. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  60. if args[0] == nil || args[1] == nil {
  61. return -1, true
  62. }
  63. arg0, arg1 := cast.ToStringAlways(args[0]), cast.ToStringAlways(args[1])
  64. return strings.Index(arg0, arg1), true
  65. },
  66. val: ValidateTwoStrArg,
  67. }
  68. builtins["length"] = builtinFunc{
  69. fType: ast.FuncTypeScalar,
  70. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  71. arg0 := cast.ToStringAlways(args[0])
  72. switch v := args[0].(type) {
  73. case []interface{}:
  74. return len(v), true
  75. case map[string]interface{}:
  76. return len(v), true
  77. case nil:
  78. return 0, true
  79. default:
  80. }
  81. return utf8.RuneCountInString(arg0), true
  82. },
  83. val: ValidateOneArg,
  84. check: return0IfHasAnyNil,
  85. }
  86. builtins["lower"] = builtinFunc{
  87. fType: ast.FuncTypeScalar,
  88. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  89. arg0 := cast.ToStringAlways(args[0])
  90. return strings.ToLower(arg0), true
  91. },
  92. val: ValidateOneStrArg,
  93. check: returnNilIfHasAnyNil,
  94. }
  95. builtins["lpad"] = builtinFunc{
  96. fType: ast.FuncTypeScalar,
  97. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  98. arg0 := cast.ToStringAlways(args[0])
  99. arg1, err := cast.ToInt(args[1], cast.STRICT)
  100. if err != nil {
  101. return err, false
  102. }
  103. return strings.Repeat(" ", arg1) + arg0, true
  104. },
  105. val: ValidateOneStrOneInt,
  106. check: returnNilIfHasAnyNil,
  107. }
  108. builtins["ltrim"] = builtinFunc{
  109. fType: ast.FuncTypeScalar,
  110. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  111. arg0 := cast.ToStringAlways(args[0])
  112. return strings.TrimLeftFunc(arg0, unicode.IsSpace), true
  113. },
  114. val: ValidateOneStrArg,
  115. check: returnNilIfHasAnyNil,
  116. }
  117. builtins["numbytes"] = builtinFunc{
  118. fType: ast.FuncTypeScalar,
  119. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  120. arg0 := cast.ToStringAlways(args[0])
  121. return len(arg0), true
  122. },
  123. val: ValidateOneStrArg,
  124. check: return0IfHasAnyNil,
  125. }
  126. builtins["regexp_matches"] = builtinFunc{
  127. fType: ast.FuncTypeScalar,
  128. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  129. arg0, arg1 := cast.ToStringAlways(args[0]), cast.ToStringAlways(args[1])
  130. if matched, err := regexp.MatchString(arg1, arg0); err != nil {
  131. return err, false
  132. } else {
  133. return matched, true
  134. }
  135. },
  136. val: ValidateTwoStrArg,
  137. check: returnFalseIfHasAnyNil,
  138. }
  139. builtins["regexp_replace"] = builtinFunc{
  140. fType: ast.FuncTypeScalar,
  141. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  142. arg0, arg1, arg2 := cast.ToStringAlways(args[0]), cast.ToStringAlways(args[1]), cast.ToStringAlways(args[2])
  143. if re, err := regexp.Compile(arg1); err != nil {
  144. return err, false
  145. } else {
  146. return re.ReplaceAllString(arg0, arg2), true
  147. }
  148. },
  149. val: func(_ api.FunctionContext, args []ast.Expr) error {
  150. if err := ValidateLen(3, len(args)); err != nil {
  151. return err
  152. }
  153. for i := 0; i < 3; i++ {
  154. if ast.IsNumericArg(args[i]) || ast.IsTimeArg(args[i]) || ast.IsBooleanArg(args[i]) {
  155. return ProduceErrInfo(i, "string")
  156. }
  157. }
  158. return nil
  159. },
  160. check: returnNilIfHasAnyNil,
  161. }
  162. builtins["regexp_substr"] = builtinFunc{
  163. fType: ast.FuncTypeScalar,
  164. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  165. arg0, arg1 := cast.ToStringAlways(args[0]), cast.ToStringAlways(args[1])
  166. if re, err := regexp.Compile(arg1); err != nil {
  167. return err, false
  168. } else {
  169. return re.FindString(arg0), true
  170. }
  171. },
  172. val: ValidateTwoStrArg,
  173. check: returnNilIfHasAnyNil,
  174. }
  175. builtins["rpad"] = builtinFunc{
  176. fType: ast.FuncTypeScalar,
  177. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  178. arg0 := cast.ToStringAlways(args[0])
  179. arg1, err := cast.ToInt(args[1], cast.STRICT)
  180. if err != nil {
  181. return err, false
  182. }
  183. return arg0 + strings.Repeat(" ", arg1), true
  184. },
  185. val: ValidateOneStrOneInt,
  186. check: returnNilIfHasAnyNil,
  187. }
  188. builtins["rtrim"] = builtinFunc{
  189. fType: ast.FuncTypeScalar,
  190. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  191. arg0 := cast.ToStringAlways(args[0])
  192. return strings.TrimRightFunc(arg0, unicode.IsSpace), true
  193. },
  194. val: ValidateOneStrArg,
  195. check: returnNilIfHasAnyNil,
  196. }
  197. builtins["substring"] = builtinFunc{
  198. fType: ast.FuncTypeScalar,
  199. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  200. arg0 := cast.ToStringAlways(args[0])
  201. arg1, err := cast.ToInt(args[1], cast.STRICT)
  202. if err != nil {
  203. return err, false
  204. }
  205. if arg1 < 0 {
  206. return fmt.Errorf("start index must be a positive number"), false
  207. }
  208. if len(args) > 2 {
  209. arg2, err := cast.ToInt(args[2], cast.STRICT)
  210. if err != nil {
  211. return err, false
  212. }
  213. if arg2 < 0 {
  214. return fmt.Errorf("end index must be a positive number"), false
  215. }
  216. if arg1 > arg2 {
  217. return fmt.Errorf("start index must be smaller than end index"), false
  218. }
  219. if arg1 > len(arg0) {
  220. return "", true
  221. }
  222. if arg2 > len(arg0) {
  223. return arg0[arg1:], true
  224. }
  225. return arg0[arg1:arg2], true
  226. } else {
  227. if arg1 > len(arg0) {
  228. return "", true
  229. }
  230. return arg0[arg1:], true
  231. }
  232. },
  233. val: func(_ api.FunctionContext, args []ast.Expr) error {
  234. l := len(args)
  235. if l != 2 && l != 3 {
  236. return fmt.Errorf("the arguments for substring should be 2 or 3")
  237. }
  238. if ast.IsNumericArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  239. return ProduceErrInfo(0, "string")
  240. }
  241. for i := 1; i < l; i++ {
  242. if ast.IsFloatArg(args[i]) || ast.IsTimeArg(args[i]) || ast.IsBooleanArg(args[i]) || ast.IsStringArg(args[i]) {
  243. return ProduceErrInfo(i, "int")
  244. }
  245. }
  246. if s, ok := args[1].(*ast.IntegerLiteral); ok {
  247. sv := s.Val
  248. if sv < 0 {
  249. return fmt.Errorf("The start index should not be a nagtive integer.")
  250. }
  251. if l == 3 {
  252. if e, ok1 := args[2].(*ast.IntegerLiteral); ok1 {
  253. ev := e.Val
  254. if ev < sv {
  255. return fmt.Errorf("The end index should be larger than start index.")
  256. }
  257. }
  258. }
  259. }
  260. return nil
  261. },
  262. check: returnNilIfHasAnyNil,
  263. }
  264. builtins["startswith"] = builtinFunc{
  265. fType: ast.FuncTypeScalar,
  266. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  267. arg0, arg1 := cast.ToStringAlways(args[0]), cast.ToStringAlways(args[1])
  268. return strings.HasPrefix(arg0, arg1), true
  269. },
  270. val: ValidateTwoStrArg,
  271. check: returnFalseIfHasAnyNil,
  272. }
  273. builtins["split_value"] = builtinFunc{
  274. fType: ast.FuncTypeScalar,
  275. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  276. arg0, arg1 := cast.ToStringAlways(args[0]), cast.ToStringAlways(args[1])
  277. ss := strings.Split(arg0, arg1)
  278. v, _ := cast.ToInt(args[2], cast.STRICT)
  279. if v > (len(ss) - 1) {
  280. return fmt.Errorf("%d out of index array (size = %d)", v, len(ss)), false
  281. } else {
  282. return ss[v], true
  283. }
  284. },
  285. val: func(_ api.FunctionContext, args []ast.Expr) error {
  286. l := len(args)
  287. if l != 3 {
  288. return fmt.Errorf("the arguments for split_value should be 3")
  289. }
  290. if ast.IsNumericArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  291. return ProduceErrInfo(0, "string")
  292. }
  293. if ast.IsNumericArg(args[1]) || ast.IsTimeArg(args[1]) || ast.IsBooleanArg(args[1]) {
  294. return ProduceErrInfo(1, "string")
  295. }
  296. if ast.IsFloatArg(args[2]) || ast.IsTimeArg(args[2]) || ast.IsBooleanArg(args[2]) || ast.IsStringArg(args[2]) {
  297. return ProduceErrInfo(2, "int")
  298. }
  299. if s, ok := args[2].(*ast.IntegerLiteral); ok {
  300. if s.Val < 0 {
  301. return fmt.Errorf("The index should not be a nagtive integer.")
  302. }
  303. }
  304. return nil
  305. },
  306. check: returnNilIfHasAnyNil,
  307. }
  308. builtins["trim"] = builtinFunc{
  309. fType: ast.FuncTypeScalar,
  310. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  311. arg0 := cast.ToStringAlways(args[0])
  312. return strings.TrimSpace(arg0), true
  313. },
  314. val: ValidateOneStrArg,
  315. check: returnNilIfHasAnyNil,
  316. }
  317. builtins["upper"] = builtinFunc{
  318. fType: ast.FuncTypeScalar,
  319. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  320. arg0 := cast.ToStringAlways(args[0])
  321. return strings.ToUpper(arg0), true
  322. },
  323. val: ValidateOneStrArg,
  324. check: returnNilIfHasAnyNil,
  325. }
  326. }