53 lines
1.2 KiB

11 months ago
10 months ago
11 months ago
10 months ago
11 months ago
11 months ago
10 months ago
11 months ago
  1. package schema
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  4. )
  5. type Schema struct {
  6. RecordType *schema_pb.RecordType
  7. fieldMap map[string]*schema_pb.Field
  8. }
  9. func NewSchema(recordType *schema_pb.RecordType) (*Schema, error) {
  10. fieldMap := make(map[string]*schema_pb.Field)
  11. for _, field := range recordType.Fields {
  12. fieldMap[field.Name] = field
  13. }
  14. return &Schema{
  15. RecordType: recordType,
  16. fieldMap: fieldMap,
  17. }, nil
  18. }
  19. func (s *Schema) GetField(name string) (*schema_pb.Field, bool) {
  20. field, ok := s.fieldMap[name]
  21. return field, ok
  22. }
  23. func TypeToString(t *schema_pb.Type) string {
  24. switch t.Kind.(type) {
  25. case *schema_pb.Type_ScalarType:
  26. switch t.GetScalarType() {
  27. case schema_pb.ScalarType_BOOL:
  28. return "bool"
  29. case schema_pb.ScalarType_INT32:
  30. return "int32"
  31. case schema_pb.ScalarType_INT64:
  32. return "int64"
  33. case schema_pb.ScalarType_FLOAT:
  34. return "float"
  35. case schema_pb.ScalarType_DOUBLE:
  36. return "double"
  37. case schema_pb.ScalarType_BYTES:
  38. return "bytes"
  39. case schema_pb.ScalarType_STRING:
  40. return "string"
  41. }
  42. case *schema_pb.Type_ListType:
  43. return "list"
  44. case *schema_pb.Type_RecordType:
  45. return "record"
  46. }
  47. return "unknown"
  48. }