funcs_obj.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. "github.com/lf-edge/ekuiper/pkg/api"
  18. "github.com/lf-edge/ekuiper/pkg/ast"
  19. )
  20. func registerObjectFunc() {
  21. builtins["keys"] = builtinFunc{
  22. fType: ast.FuncTypeScalar,
  23. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  24. arg := args[0]
  25. if arg, ok := arg.(map[string]interface{}); ok {
  26. list := make([]string, 0, len(arg))
  27. for key := range arg {
  28. list = append(list, key)
  29. }
  30. return list, true
  31. }
  32. return fmt.Errorf("the argument should be map[string]interface{}"), false
  33. },
  34. val: ValidateOneArg,
  35. }
  36. builtins["values"] = builtinFunc{
  37. fType: ast.FuncTypeScalar,
  38. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  39. arg := args[0]
  40. if arg, ok := arg.(map[string]interface{}); ok {
  41. list := make([]interface{}, 0, len(arg))
  42. for _, value := range arg {
  43. list = append(list, value)
  44. }
  45. return list, true
  46. }
  47. return fmt.Errorf("the argument should be map[string]interface{}"), false
  48. },
  49. val: ValidateOneArg,
  50. }
  51. builtins["object"] = builtinFunc{
  52. fType: ast.FuncTypeScalar,
  53. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  54. keys, ok := args[0].([]interface{})
  55. if !ok {
  56. return fmt.Errorf("first argument should be []string"), false
  57. }
  58. values, ok := args[1].([]interface{})
  59. if !ok {
  60. return fmt.Errorf("second argument should be []interface{}"), false
  61. }
  62. if len(keys) != len(values) {
  63. return fmt.Errorf("the length of the arguments should be same"), false
  64. }
  65. if len(keys) == 0 {
  66. return nil, true
  67. }
  68. m := make(map[string]interface{}, len(keys))
  69. for i, k := range keys {
  70. key, ok := k.(string)
  71. if !ok {
  72. return fmt.Errorf("first argument should be []string"), false
  73. }
  74. m[key] = values[i]
  75. }
  76. return m, true
  77. },
  78. val: func(ctx api.FunctionContext, args []ast.Expr) error {
  79. return ValidateLen(2, len(args))
  80. },
  81. }
  82. builtins["zip"] = builtinFunc{
  83. fType: ast.FuncTypeScalar,
  84. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  85. lists, ok := args[0].([]interface{})
  86. if !ok {
  87. return fmt.Errorf("each argument should be [][2]interface{}"), false
  88. }
  89. if len(lists) == 0 {
  90. return nil, true
  91. }
  92. m := make(map[string]interface{}, len(lists))
  93. for _, item := range lists {
  94. a, ok := item.([]interface{})
  95. if !ok {
  96. return fmt.Errorf("each argument should be [][2]interface{}"), false
  97. }
  98. if len(a) != 2 {
  99. return fmt.Errorf("each argument should be [][2]interface{}"), false
  100. }
  101. key, ok := a[0].(string)
  102. if !ok {
  103. return fmt.Errorf("the first element in the list item should be string"), false
  104. }
  105. m[key] = a[1]
  106. }
  107. return m, true
  108. },
  109. val: ValidateOneArg,
  110. }
  111. builtins["items"] = builtinFunc{
  112. fType: ast.FuncTypeScalar,
  113. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  114. m, ok := args[0].(map[string]interface{})
  115. if !ok {
  116. return fmt.Errorf("first argument should be map[string]interface{}"), false
  117. }
  118. if len(m) < 1 {
  119. return nil, true
  120. }
  121. list := make([]interface{}, 0, len(m))
  122. for k, v := range m {
  123. list = append(list, []interface{}{k, v})
  124. }
  125. return list, true
  126. },
  127. val: ValidateOneArg,
  128. }
  129. }