aboutsummaryrefslogtreecommitdiff
path: root/weed/mq/schema/schema.go
blob: 04d0a7b5e1c1956786f985e4f8ffad84298ef758 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package schema

import (
	"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
)

type Schema struct {
	Namespace  string
	Name       string
	RevisionId uint32
	RecordType *schema_pb.RecordType
	fieldMap   map[string]*schema_pb.Field
}

func NewSchema(namespace string, name string, recordType *schema_pb.RecordType) *Schema {
	fieldMap := make(map[string]*schema_pb.Field)
	for _, field := range recordType.Fields {
		fieldMap[field.Name] = field
	}
	return &Schema{
		Namespace:  namespace,
		Name:       name,
		RecordType: recordType,
		fieldMap:   fieldMap,
	}
}

func (s *Schema) GetField(name string) (*schema_pb.Field, bool) {
	field, ok := s.fieldMap[name]
	return field, ok
}

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"
}