ext_inferer_protobuf.go 4.1 KB

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