ext_inferer_protobuf.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 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. //go:build schema || !core
  15. package schema
  16. import (
  17. "fmt"
  18. dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
  19. "github.com/jhump/protoreflect/desc"
  20. "github.com/jhump/protoreflect/desc/protoparse"
  21. "github.com/lf-edge/ekuiper/internal/pkg/def"
  22. "github.com/lf-edge/ekuiper/pkg/ast"
  23. "github.com/lf-edge/ekuiper/pkg/message"
  24. )
  25. var protoParser *protoparse.Parser
  26. func init() {
  27. inferes[message.FormatProtobuf] = InferProtobuf
  28. protoParser = &protoparse.Parser{}
  29. }
  30. // InferProtobuf infers the schema from a protobuf file dynamically in case the schema file changed
  31. func InferProtobuf(schemaFile string, messageName string) (ast.StreamFields, error) {
  32. ffs, err := GetSchemaFile(def.PROTOBUF, schemaFile)
  33. if err != nil {
  34. return nil, err
  35. }
  36. if fds, err := protoParser.ParseFiles(ffs.SchemaFile); err != nil {
  37. return nil, fmt.Errorf("parse schema file %s failed: %s", ffs.SchemaFile, err)
  38. } else {
  39. messageDescriptor := fds[0].FindMessage(messageName)
  40. if messageDescriptor == nil {
  41. return nil, fmt.Errorf("message type %s not found in schema file %s", messageName, schemaFile)
  42. }
  43. return convertMessage(messageDescriptor)
  44. }
  45. }
  46. func convertMessage(m *desc.MessageDescriptor) (ast.StreamFields, error) {
  47. mfs := m.GetFields()
  48. result := make(ast.StreamFields, 0, len(mfs))
  49. for _, f := range mfs {
  50. ff, err := convertField(f)
  51. if err != nil {
  52. return nil, err
  53. }
  54. result = append(result, ff)
  55. }
  56. return result, nil
  57. }
  58. func convertField(f *desc.FieldDescriptor) (ast.StreamField, error) {
  59. ff := ast.StreamField{
  60. Name: f.GetName(),
  61. }
  62. var (
  63. ft ast.FieldType
  64. err error
  65. )
  66. ft, err = convertFieldType(f.GetType(), f)
  67. if err != nil {
  68. return ff, err
  69. }
  70. if f.IsRepeated() {
  71. switch t := ft.(type) {
  72. case *ast.BasicType:
  73. ft = &ast.ArrayType{
  74. Type: t.Type,
  75. }
  76. case *ast.RecType:
  77. ft = &ast.ArrayType{
  78. Type: ast.STRUCT,
  79. FieldType: t,
  80. }
  81. case *ast.ArrayType:
  82. ft = &ast.ArrayType{
  83. Type: ast.ARRAY,
  84. FieldType: t,
  85. }
  86. }
  87. }
  88. ff.FieldType = ft
  89. return ff, nil
  90. }
  91. func convertFieldType(tt dpb.FieldDescriptorProto_Type, f *desc.FieldDescriptor) (ast.FieldType, error) {
  92. var ft ast.FieldType
  93. switch tt {
  94. case dpb.FieldDescriptorProto_TYPE_DOUBLE,
  95. dpb.FieldDescriptorProto_TYPE_FLOAT:
  96. ft = &ast.BasicType{Type: ast.FLOAT}
  97. case dpb.FieldDescriptorProto_TYPE_INT32, dpb.FieldDescriptorProto_TYPE_SFIXED32, dpb.FieldDescriptorProto_TYPE_SINT32,
  98. dpb.FieldDescriptorProto_TYPE_INT64, dpb.FieldDescriptorProto_TYPE_SFIXED64, dpb.FieldDescriptorProto_TYPE_SINT64,
  99. dpb.FieldDescriptorProto_TYPE_FIXED32, dpb.FieldDescriptorProto_TYPE_UINT32,
  100. dpb.FieldDescriptorProto_TYPE_FIXED64, dpb.FieldDescriptorProto_TYPE_UINT64:
  101. ft = &ast.BasicType{Type: ast.BIGINT}
  102. case dpb.FieldDescriptorProto_TYPE_BOOL:
  103. ft = &ast.BasicType{Type: ast.BOOLEAN}
  104. case dpb.FieldDescriptorProto_TYPE_STRING:
  105. ft = &ast.BasicType{Type: ast.STRINGS}
  106. case dpb.FieldDescriptorProto_TYPE_BYTES:
  107. ft = &ast.BasicType{Type: ast.BYTEA}
  108. case dpb.FieldDescriptorProto_TYPE_MESSAGE:
  109. sfs, err := convertMessage(f.GetMessageType())
  110. if err != nil {
  111. return nil, fmt.Errorf("invalid struct field type: %v", err)
  112. }
  113. ft = &ast.RecType{StreamFields: sfs}
  114. default:
  115. return nil, fmt.Errorf("invalid type for field '%s'", f.GetName())
  116. }
  117. return ft, nil
  118. }