seaweedfs/weed/mq/schema/schema.go

54 lines
1.2 KiB
Go
Raw Normal View History

2024-04-13 13:31:28 +08:00
package schema
import (
"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
)
type Schema struct {
RecordType *schema_pb.RecordType
2024-05-21 02:03:56 +08:00
fieldMap map[string]*schema_pb.Field
2024-04-13 13:31:28 +08:00
}
func NewSchema(recordType *schema_pb.RecordType) (*Schema, error) {
2024-05-21 02:03:56 +08:00
fieldMap := make(map[string]*schema_pb.Field)
2024-04-13 13:31:28 +08:00
for _, field := range recordType.Fields {
2024-04-18 14:49:21 +08:00
fieldMap[field.Name] = field
2024-04-13 13:31:28 +08:00
}
return &Schema{
RecordType: recordType,
2024-05-21 02:03:56 +08:00
fieldMap: fieldMap,
2024-04-13 13:31:28 +08:00
}, nil
}
2024-04-18 14:49:21 +08:00
func (s *Schema) GetField(name string) (*schema_pb.Field, bool) {
field, ok := s.fieldMap[name]
return field, ok
}
2024-10-03 15:13:46 +08:00
func TypeToString(t *schema_pb.Type) string {
switch t.Kind.(type) {
case *schema_pb.Type_ScalarType:
switch t.GetScalarType() {
case schema_pb.ScalarType_BOOL:
return "bool"
case schema_pb.ScalarType_INT32:
return "int32"
case schema_pb.ScalarType_INT64:
return "int64"
case schema_pb.ScalarType_FLOAT:
return "float"
case schema_pb.ScalarType_DOUBLE:
return "double"
case schema_pb.ScalarType_BYTES:
return "bytes"
case schema_pb.ScalarType_STRING:
return "string"
}
case *schema_pb.Type_ListType:
return "list"
case *schema_pb.Type_RecordType:
return "record"
}
return "unknown"
}