From 05d18130b62ccc8e08bba93d5bc5d462a46c9cf6 Mon Sep 17 00:00:00 2001 From: chrislu Date: Thu, 18 Apr 2024 22:27:18 -0700 Subject: [PATCH] add schema builder --- weed/mq/schema/schema_builder.go | 73 +++++++++++++++++++++++++++ weed/mq/schema/schema_builder_test.go | 26 ++++++++++ 2 files changed, 99 insertions(+) create mode 100644 weed/mq/schema/schema_builder.go create mode 100644 weed/mq/schema/schema_builder_test.go diff --git a/weed/mq/schema/schema_builder.go b/weed/mq/schema/schema_builder.go new file mode 100644 index 000000000..cabd0ee7b --- /dev/null +++ b/weed/mq/schema/schema_builder.go @@ -0,0 +1,73 @@ +package schema + +import ( + "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb" +) + +var ( + TypeBoolean = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_BOOLEAN}} + TypeInteger = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_INTEGER}} + TypeLong = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_LONG}} + TypeFloat = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_FLOAT}} + TypeDouble = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_DOUBLE}} + TypeBytes = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_BYTES}} + TypeString = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_STRING}} +) + +type RecordTypeBuilder struct { + recordType *schema_pb.RecordType +} + +func NewRecordTypeBuilder() *RecordTypeBuilder { + return &RecordTypeBuilder{recordType: &schema_pb.RecordType{}} +} + +func (rtb *RecordTypeBuilder) Build() *schema_pb.RecordType { + return rtb.recordType +} + +func (rtb *RecordTypeBuilder) addField(name string, scalarType *schema_pb.Type) *RecordTypeBuilder { + rtb.recordType.Fields = append(rtb.recordType.Fields, &schema_pb.Field{ + Name: name, + Type: scalarType, + }) + return rtb +} + +func (rtb *RecordTypeBuilder) AddBoolField(name string) *RecordTypeBuilder { + return rtb.addField(name, TypeBoolean) +} +func (rtb *RecordTypeBuilder) AddIntegerField(name string) *RecordTypeBuilder { + return rtb.addField(name, TypeInteger) +} +func (rtb *RecordTypeBuilder) AddLongField(name string) *RecordTypeBuilder { + return rtb.addField(name, TypeLong) +} +func (rtb *RecordTypeBuilder) AddFloatField(name string) *RecordTypeBuilder { + return rtb.addField(name, TypeFloat) +} +func (rtb *RecordTypeBuilder) AddDoubleField(name string) *RecordTypeBuilder { + return rtb.addField(name, TypeDouble) +} +func (rtb *RecordTypeBuilder) AddBytesField(name string) *RecordTypeBuilder { + return rtb.addField(name, TypeBytes) +} +func (rtb *RecordTypeBuilder) AddStringField(name string) *RecordTypeBuilder { + return rtb.addField(name, TypeString) +} + +func (rtb *RecordTypeBuilder) AddRecordField(name string, recordTypeBuilder *RecordTypeBuilder) *RecordTypeBuilder { + rtb.recordType.Fields = append(rtb.recordType.Fields, &schema_pb.Field{ + Name: name, + Type: &schema_pb.Type{Kind: &schema_pb.Type_RecordType{RecordType: recordTypeBuilder.recordType}}, + }) + return rtb +} + +func (rtb *RecordTypeBuilder) AddListField(name string, elementType *schema_pb.Type) *RecordTypeBuilder { + rtb.recordType.Fields = append(rtb.recordType.Fields, &schema_pb.Field{ + Name: name, + Type: &schema_pb.Type{Kind: &schema_pb.Type_ListType{ListType: &schema_pb.ListType{ElementType: elementType}}}, + }) + return rtb +} diff --git a/weed/mq/schema/schema_builder_test.go b/weed/mq/schema/schema_builder_test.go new file mode 100644 index 000000000..71a884286 --- /dev/null +++ b/weed/mq/schema/schema_builder_test.go @@ -0,0 +1,26 @@ +package schema + +import ( + "fmt" + "testing" +) + +func TestSchemaBuilder(t *testing.T) { + rtb := NewRecordTypeBuilder() + rtb.AddStringField("ID"). + AddLongField("CreatedAt"). + AddLongField("ModifiedAt"). + AddStringField("User") + recordType := rtb.Build() + fmt.Printf("RecordType: %v\n", recordType) + + recordType2 := NewRecordTypeBuilder(). + AddLongField("ID"). + AddLongField("CreatedAt"). + AddRecordField("Person", NewRecordTypeBuilder(). + AddStringField("Name"). + AddListField("emails", TypeString)).Build() + + fmt.Printf("RecordType2: %v\n", recordType2) + +}