Browse Source

recursive struct to schema

mq-subscribe
chrislu 8 months ago
parent
commit
7310d022ad
  1. 16
      weed/mq/schema/schema_builder.go
  2. 55
      weed/mq/schema/struct_to_schema.go

16
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 {

55
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
}
Loading…
Cancel
Save