field_processor.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2021-2022 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 operator
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "reflect"
  19. "time"
  20. "github.com/lf-edge/ekuiper/internal/xsql"
  21. "github.com/lf-edge/ekuiper/pkg/ast"
  22. "github.com/lf-edge/ekuiper/pkg/cast"
  23. )
  24. // Only run when strict validation mode is on, fields is defined and is not binary
  25. // Do not convert types
  26. type defaultFieldProcessor struct {
  27. streamFields map[string]*ast.JsonStreamField
  28. timestampFormat string
  29. }
  30. func (p *defaultFieldProcessor) validateAndConvert(tuple *xsql.Tuple) error {
  31. _, err := p.validateAndConvertMessage(p.streamFields, tuple.Message)
  32. return err
  33. }
  34. func (p *defaultFieldProcessor) validateAndConvertMessage(schema map[string]*ast.JsonStreamField, message xsql.Message) (map[string]interface{}, error) {
  35. for name, sf := range schema {
  36. v, ok := message.Value(name, "")
  37. if !ok {
  38. return nil, fmt.Errorf("field %s is not found", name)
  39. }
  40. if nv, err := p.validateAndConvertField(sf, v); err != nil {
  41. return nil, fmt.Errorf("field %s type mismatch: %v", name, err)
  42. } else {
  43. message[name] = nv
  44. }
  45. }
  46. return message, nil
  47. }
  48. // Validate and convert field value to the type defined in schema
  49. func (p *defaultFieldProcessor) validateAndConvertField(sf *ast.JsonStreamField, t interface{}) (interface{}, error) {
  50. v := reflect.ValueOf(t)
  51. jtype := v.Kind()
  52. switch sf.Type {
  53. case (ast.BIGINT).String():
  54. if jtype == reflect.Int64 {
  55. return t, nil
  56. }
  57. return cast.ToInt64(t, cast.CONVERT_SAMEKIND)
  58. case (ast.FLOAT).String():
  59. if jtype == reflect.Float64 {
  60. return t, nil
  61. }
  62. return cast.ToFloat64(t, cast.CONVERT_SAMEKIND)
  63. case (ast.BOOLEAN).String():
  64. if jtype == reflect.Bool {
  65. return t, nil
  66. }
  67. return cast.ToBool(t, cast.CONVERT_SAMEKIND)
  68. case (ast.STRINGS).String():
  69. if jtype == reflect.String {
  70. return t, nil
  71. }
  72. return cast.ToString(t, cast.CONVERT_SAMEKIND)
  73. case (ast.DATETIME).String():
  74. return cast.InterfaceToTime(t, p.timestampFormat)
  75. case (ast.BYTEA).String():
  76. return cast.ToByteA(t, cast.CONVERT_SAMEKIND)
  77. case (ast.ARRAY).String():
  78. if t == nil {
  79. return []interface{}(nil), nil
  80. } else if jtype == reflect.Slice {
  81. a, ok := t.([]interface{})
  82. if !ok {
  83. return nil, fmt.Errorf("cannot convert %v to []interface{}", t)
  84. }
  85. for i, e := range a {
  86. ne, err := p.validateAndConvertField(sf.Items, e)
  87. if err != nil {
  88. return nil, fmt.Errorf("array element type mismatch: %v", err)
  89. }
  90. if ne != nil {
  91. a[i] = ne
  92. }
  93. }
  94. return a, nil
  95. } else {
  96. return nil, fmt.Errorf("expect array but got %v", t)
  97. }
  98. case (ast.STRUCT).String():
  99. var (
  100. nextJ map[string]interface{}
  101. ok bool
  102. )
  103. if t == nil {
  104. return map[string]interface{}(nil), nil
  105. } else if jtype == reflect.Map {
  106. nextJ, ok = t.(map[string]interface{})
  107. if !ok {
  108. return nil, fmt.Errorf("expect map but found %[1]T(%[1]v)", t)
  109. }
  110. } else if jtype == reflect.String {
  111. err := json.Unmarshal(cast.StringToBytes(t.(string)), &nextJ)
  112. if err != nil {
  113. return nil, fmt.Errorf("invalid data type for %s, expect map but found %[1]T(%[1]v)", t)
  114. }
  115. } else {
  116. return nil, fmt.Errorf("expect struct but found %[1]T(%[1]v)", t)
  117. }
  118. return p.validateAndConvertMessage(sf.Properties, nextJ)
  119. default:
  120. return nil, fmt.Errorf("unsupported type %s", sf.Type)
  121. }
  122. }
  123. func (p *defaultFieldProcessor) parseTime(s string) (time.Time, error) {
  124. return cast.ParseTime(s, p.timestampFormat)
  125. }