From 7310d022ad369ef1dda43af336698eaf838e72c6 Mon Sep 17 00:00:00 2001 From: chrislu Date: Sun, 28 Apr 2024 11:14:13 -0700 Subject: [PATCH] recursive struct to schema --- weed/mq/schema/schema_builder.go | 16 ++++----- weed/mq/schema/struct_to_schema.go | 55 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 weed/mq/schema/struct_to_schema.go diff --git a/weed/mq/schema/schema_builder.go b/weed/mq/schema/schema_builder.go index bcb46d2bb..cadf4f927 100644 --- a/weed/mq/schema/schema_builder.go +++ b/weed/mq/schema/schema_builder.go @@ -31,7 +31,7 @@ func (rtb *RecordTypeBuilder) Build() *schema_pb.RecordType { return rtb.recordType } -func (rtb *RecordTypeBuilder) addField(name string, scalarType *schema_pb.Type) *RecordTypeBuilder { +func (rtb *RecordTypeBuilder) setField(name string, scalarType *schema_pb.Type) *RecordTypeBuilder { rtb.recordType.Fields = append(rtb.recordType.Fields, &schema_pb.Field{ Name: name, Type: scalarType, @@ -40,25 +40,25 @@ func (rtb *RecordTypeBuilder) addField(name string, scalarType *schema_pb.Type) } func (rtb *RecordTypeBuilder) SetBoolField(name string) *RecordTypeBuilder { - return rtb.addField(name, TypeBoolean) + return rtb.setField(name, TypeBoolean) } func (rtb *RecordTypeBuilder) SetIntegerField(name string) *RecordTypeBuilder { - return rtb.addField(name, TypeInteger) + return rtb.setField(name, TypeInteger) } func (rtb *RecordTypeBuilder) SetLongField(name string) *RecordTypeBuilder { - return rtb.addField(name, TypeLong) + return rtb.setField(name, TypeLong) } func (rtb *RecordTypeBuilder) SetFloatField(name string) *RecordTypeBuilder { - return rtb.addField(name, TypeFloat) + return rtb.setField(name, TypeFloat) } func (rtb *RecordTypeBuilder) SetDoubleField(name string) *RecordTypeBuilder { - return rtb.addField(name, TypeDouble) + return rtb.setField(name, TypeDouble) } func (rtb *RecordTypeBuilder) SetBytesField(name string) *RecordTypeBuilder { - return rtb.addField(name, TypeBytes) + return rtb.setField(name, TypeBytes) } func (rtb *RecordTypeBuilder) SetStringField(name string) *RecordTypeBuilder { - return rtb.addField(name, TypeString) + return rtb.setField(name, TypeString) } func (rtb *RecordTypeBuilder) SetRecordField(name string, recordTypeBuilder *RecordTypeBuilder) *RecordTypeBuilder { diff --git a/weed/mq/schema/struct_to_schema.go b/weed/mq/schema/struct_to_schema.go new file mode 100644 index 000000000..bdd3e2465 --- /dev/null +++ b/weed/mq/schema/struct_to_schema.go @@ -0,0 +1,55 @@ +package schema + +import ( + "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb" + "reflect" +) + +func StructToSchema(instance any) *RecordTypeBuilder { + rtb := NewRecordTypeBuilder() + myType := reflect.TypeOf(instance) + for i := 0; i < myType.NumField(); i++ { + field := myType.Field(i) + fieldType := field.Type + fieldName := field.Name + schemaField := reflectTypeToSchemaType(fieldType) + if schemaField == nil { + continue + } + rtb.setField(fieldName, schemaField) + } + return rtb +} + +func reflectTypeToSchemaType(t reflect.Type) *schema_pb.Type { + switch t.Kind() { + case reflect.Bool: + return TypeBoolean + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32: + return TypeInteger + case reflect.Int64: + return TypeLong + case reflect.Float32: + return TypeFloat + case reflect.Float64: + return TypeDouble + case reflect.String: + return TypeString + case reflect.Slice: + switch t.Elem().Kind() { + case reflect.Uint8: + return TypeBytes + default: + if st := reflectTypeToSchemaType(t.Elem()); st != nil { + return &schema_pb.Type{ + Kind: &schema_pb.Type_ListType{ + ListType: &schema_pb.ListType{ + ElementType: st, + }, + }, + } + } + } + } + return nil +}