You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.3 KiB

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