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. "github.com/lf-edge/ekuiper/internal/xsql"
  19. "github.com/lf-edge/ekuiper/pkg/ast"
  20. "github.com/lf-edge/ekuiper/pkg/cast"
  21. "reflect"
  22. "time"
  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 := t.([]interface{})
  82. for i, e := range a {
  83. ne, err := p.validateAndConvertField(sf.Items, e)
  84. if err != nil {
  85. return nil, fmt.Errorf("array element type mismatch: %v", err)
  86. }
  87. if ne != nil {
  88. a[i] = ne
  89. }
  90. }
  91. return a, nil
  92. } else {
  93. return nil, fmt.Errorf("expect array but got %v", t)
  94. }
  95. case (ast.STRUCT).String():
  96. var (
  97. nextJ map[string]interface{}
  98. ok bool
  99. )
  100. if t == nil {
  101. return map[string]interface{}(nil), nil
  102. } else if jtype == reflect.Map {
  103. nextJ, ok = t.(map[string]interface{})
  104. if !ok {
  105. return nil, fmt.Errorf("expect map but found %[1]T(%[1]v)", t)
  106. }
  107. } else if jtype == reflect.String {
  108. err := json.Unmarshal([]byte(t.(string)), &nextJ)
  109. if err != nil {
  110. return nil, fmt.Errorf("invalid data type for %s, expect map but found %[1]T(%[1]v)", t)
  111. }
  112. } else {
  113. return nil, fmt.Errorf("expect struct but found %[1]T(%[1]v)", t)
  114. }
  115. return p.validateAndConvertMessage(sf.Properties, nextJ)
  116. default:
  117. return nil, fmt.Errorf("unsupported type %s", sf.Type)
  118. }
  119. }
  120. func (p *defaultFieldProcessor) parseTime(s string) (time.Time, error) {
  121. if p.timestampFormat != "" {
  122. return cast.ParseTime(s, p.timestampFormat)
  123. } else {
  124. return time.Parse(cast.JSISO, s)
  125. }
  126. }