seaweedfs/weed/mq/schema/value_builder.go

114 lines
4.9 KiB
Go
Raw Normal View History

2024-04-22 15:42:18 +08:00
package schema
import "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
// RecordValueBuilder helps in constructing RecordValue protobuf messages
type RecordValueBuilder struct {
recordValue *schema_pb.RecordValue
}
// NewRecordValueBuilder creates a new RecordValueBuilder instance
func NewRecordValueBuilder() *RecordValueBuilder {
return &RecordValueBuilder{recordValue: &schema_pb.RecordValue{Fields: make(map[string]*schema_pb.Value)}}
}
// Build returns the constructed RecordValue message
func (rvb *RecordValueBuilder) Build() *schema_pb.RecordValue {
return rvb.recordValue
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetBoolValue(key string, value bool) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_BoolValue{BoolValue: value}}
return rvb
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetIntValue(key string, value int32) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_Int32Value{Int32Value: value}}
return rvb
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetLongValue(key string, value int64) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_Int64Value{Int64Value: value}}
return rvb
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetFloatValue(key string, value float32) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_FloatValue{FloatValue: value}}
return rvb
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetDoubleValue(key string, value float64) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_DoubleValue{DoubleValue: value}}
return rvb
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetBytesValue(key string, value []byte) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_BytesValue{BytesValue: value}}
return rvb
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetStringValue(key string, value string) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_StringValue{StringValue: value}}
return rvb
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetRecordValue(key string, value *RecordValueBuilder) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_RecordValue{RecordValue: value.Build()}}
return rvb
}
func (rvb *RecordValueBuilder) addListValue(key string, values []*schema_pb.Value) *RecordValueBuilder {
rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_ListValue{ListValue: &schema_pb.ListValue{Values: values}}}
return rvb
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetBoolListValue(key string, values ...bool) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
var listValues []*schema_pb.Value
for _, v := range values {
listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_BoolValue{BoolValue: v}})
}
return rvb.addListValue(key, listValues)
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetIntListValue(key string, values ...int32) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
var listValues []*schema_pb.Value
for _, v := range values {
listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_Int32Value{Int32Value: v}})
}
return rvb.addListValue(key, listValues)
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetLongListValue(key string, values ...int64) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
var listValues []*schema_pb.Value
for _, v := range values {
listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_Int64Value{Int64Value: v}})
}
return rvb.addListValue(key, listValues)
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetFloatListValue(key string, values ...float32) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
var listValues []*schema_pb.Value
for _, v := range values {
listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_FloatValue{FloatValue: v}})
}
return rvb.addListValue(key, listValues)
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetDoubleListValue(key string, values ...float64) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
var listValues []*schema_pb.Value
for _, v := range values {
listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_DoubleValue{DoubleValue: v}})
}
return rvb.addListValue(key, listValues)
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetBytesListValue(key string, values ...[]byte) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
var listValues []*schema_pb.Value
for _, v := range values {
listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_BytesValue{BytesValue: v}})
}
return rvb.addListValue(key, listValues)
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetStringListValue(key string, values ...string) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
var listValues []*schema_pb.Value
for _, v := range values {
listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_StringValue{StringValue: v}})
}
return rvb.addListValue(key, listValues)
}
2024-04-28 14:30:15 +08:00
func (rvb *RecordValueBuilder) SetRecordListValue(key string, values ...*RecordValueBuilder) *RecordValueBuilder {
2024-04-22 15:42:18 +08:00
var listValues []*schema_pb.Value
for _, v := range values {
listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_RecordValue{RecordValue: v.Build()}})
}
return rvb.addListValue(key, listValues)
}