funcs_obj.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 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. "fmt"
  17. "reflect"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/lf-edge/ekuiper/pkg/ast"
  20. "github.com/lf-edge/ekuiper/pkg/cast"
  21. )
  22. func registerObjectFunc() {
  23. builtins["keys"] = builtinFunc{
  24. fType: ast.FuncTypeScalar,
  25. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  26. arg := args[0]
  27. if arg, ok := arg.(map[string]interface{}); ok {
  28. list := make([]string, 0, len(arg))
  29. for key := range arg {
  30. list = append(list, key)
  31. }
  32. return list, true
  33. }
  34. return fmt.Errorf("the argument should be map[string]interface{}"), false
  35. },
  36. val: ValidateOneArg,
  37. check: returnNilIfHasAnyNil,
  38. }
  39. builtins["values"] = builtinFunc{
  40. fType: ast.FuncTypeScalar,
  41. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  42. arg := args[0]
  43. if arg, ok := arg.(map[string]interface{}); ok {
  44. list := make([]interface{}, 0, len(arg))
  45. for _, value := range arg {
  46. list = append(list, value)
  47. }
  48. return list, true
  49. }
  50. return fmt.Errorf("the argument should be map[string]interface{}"), false
  51. },
  52. val: ValidateOneArg,
  53. check: returnNilIfHasAnyNil,
  54. }
  55. builtins["object"] = builtinFunc{
  56. fType: ast.FuncTypeScalar,
  57. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  58. keys, ok := args[0].([]interface{})
  59. if !ok {
  60. return fmt.Errorf("first argument should be []string"), false
  61. }
  62. values, ok := args[1].([]interface{})
  63. if !ok {
  64. return fmt.Errorf("second argument should be []interface{}"), false
  65. }
  66. if len(keys) != len(values) {
  67. return fmt.Errorf("the length of the arguments should be same"), false
  68. }
  69. if len(keys) == 0 {
  70. return nil, true
  71. }
  72. m := make(map[string]interface{}, len(keys))
  73. for i, k := range keys {
  74. key, ok := k.(string)
  75. if !ok {
  76. return fmt.Errorf("first argument should be []string"), false
  77. }
  78. m[key] = values[i]
  79. }
  80. return m, true
  81. },
  82. val: func(ctx api.FunctionContext, args []ast.Expr) error {
  83. return ValidateLen(2, len(args))
  84. },
  85. check: returnNilIfHasAnyNil,
  86. }
  87. builtins["zip"] = builtinFunc{
  88. fType: ast.FuncTypeScalar,
  89. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  90. lists, ok := args[0].([]interface{})
  91. if !ok {
  92. return fmt.Errorf("each argument should be [][2]interface{}"), false
  93. }
  94. if len(lists) == 0 {
  95. return nil, true
  96. }
  97. m := make(map[string]interface{}, len(lists))
  98. for _, item := range lists {
  99. a, ok := item.([]interface{})
  100. if !ok {
  101. return fmt.Errorf("each argument should be [][2]interface{}"), false
  102. }
  103. if len(a) != 2 {
  104. return fmt.Errorf("each argument should be [][2]interface{}"), false
  105. }
  106. key, ok := a[0].(string)
  107. if !ok {
  108. return fmt.Errorf("the first element in the list item should be string"), false
  109. }
  110. m[key] = a[1]
  111. }
  112. return m, true
  113. },
  114. val: ValidateOneArg,
  115. check: returnNilIfHasAnyNil,
  116. }
  117. builtins["items"] = builtinFunc{
  118. fType: ast.FuncTypeScalar,
  119. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  120. m, ok := args[0].(map[string]interface{})
  121. if !ok {
  122. return fmt.Errorf("first argument should be map[string]interface{}"), false
  123. }
  124. if len(m) < 1 {
  125. return nil, true
  126. }
  127. list := make([]interface{}, 0, len(m))
  128. for k, v := range m {
  129. list = append(list, []interface{}{k, v})
  130. }
  131. return list, true
  132. },
  133. val: ValidateOneArg,
  134. check: returnNilIfHasAnyNil,
  135. }
  136. builtins["object_concat"] = builtinFunc{
  137. fType: ast.FuncTypeScalar,
  138. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  139. res := make(map[string]interface{})
  140. for i, arg := range args {
  141. if arg == nil {
  142. continue
  143. }
  144. arg, ok := arg.(map[string]interface{})
  145. if !ok {
  146. return fmt.Errorf("the argument should be map[string]interface{}, got %v", args[i]), false
  147. }
  148. for k, v := range arg {
  149. res[k] = v
  150. }
  151. }
  152. return res, true
  153. },
  154. val: func(_ api.FunctionContext, args []ast.Expr) error {
  155. return ValidateAtLeast(2, len(args))
  156. },
  157. }
  158. builtins["erase"] = builtinFunc{
  159. fType: ast.FuncTypeScalar,
  160. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  161. contains := func(array []string, target string) bool {
  162. for _, v := range array {
  163. if target == v {
  164. return true
  165. }
  166. }
  167. return false
  168. }
  169. if len(args) != 2 {
  170. return fmt.Errorf("the argument number should be 2, got %v", len(args)), false
  171. }
  172. res := make(map[string]interface{})
  173. argMap, ok := args[0].(map[string]interface{})
  174. if !ok {
  175. return fmt.Errorf("the first argument should be map[string]interface{}, got %v", args[0]), false
  176. }
  177. eraseArray := make([]string, 0)
  178. v := reflect.ValueOf(args[1])
  179. switch v.Kind() {
  180. case reflect.Slice:
  181. array, err := cast.ToStringSlice(args[1], cast.CONVERT_ALL)
  182. if err != nil {
  183. return err, false
  184. }
  185. eraseArray = append(eraseArray, array...)
  186. case reflect.String:
  187. str := args[1].(string)
  188. for k, v := range argMap {
  189. if k != str {
  190. res[k] = v
  191. }
  192. }
  193. return res, true
  194. default:
  195. return fmt.Errorf("the augument should be slice or string"), false
  196. }
  197. for k, v := range argMap {
  198. if !contains(eraseArray, k) {
  199. res[k] = v
  200. }
  201. }
  202. return res, true
  203. },
  204. val: func(_ api.FunctionContext, args []ast.Expr) error {
  205. return ValidateAtLeast(2, len(args))
  206. },
  207. check: returnNilIfHasAnyNil,
  208. }
  209. }