value_builder.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package schema
  2. import "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  3. // RecordValueBuilder helps in constructing RecordValue protobuf messages
  4. type RecordValueBuilder struct {
  5. recordValue *schema_pb.RecordValue
  6. }
  7. // RecordBegin creates a new RecordValueBuilder instance
  8. func RecordBegin() *RecordValueBuilder {
  9. return &RecordValueBuilder{recordValue: &schema_pb.RecordValue{Fields: make(map[string]*schema_pb.Value)}}
  10. }
  11. // RecordEnd returns the constructed RecordValue message
  12. func (rvb *RecordValueBuilder) RecordEnd() *schema_pb.RecordValue {
  13. return rvb.recordValue
  14. }
  15. func (rvb *RecordValueBuilder) SetBool(key string, value bool) *RecordValueBuilder {
  16. rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_BoolValue{BoolValue: value}}
  17. return rvb
  18. }
  19. func (rvb *RecordValueBuilder) SetInt32(key string, value int32) *RecordValueBuilder {
  20. rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_Int32Value{Int32Value: value}}
  21. return rvb
  22. }
  23. func (rvb *RecordValueBuilder) SetInt64(key string, value int64) *RecordValueBuilder {
  24. rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_Int64Value{Int64Value: value}}
  25. return rvb
  26. }
  27. func (rvb *RecordValueBuilder) SetFloat(key string, value float32) *RecordValueBuilder {
  28. rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_FloatValue{FloatValue: value}}
  29. return rvb
  30. }
  31. func (rvb *RecordValueBuilder) SetDouble(key string, value float64) *RecordValueBuilder {
  32. rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_DoubleValue{DoubleValue: value}}
  33. return rvb
  34. }
  35. func (rvb *RecordValueBuilder) SetBytes(key string, value []byte) *RecordValueBuilder {
  36. rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_BytesValue{BytesValue: value}}
  37. return rvb
  38. }
  39. func (rvb *RecordValueBuilder) SetString(key string, value string) *RecordValueBuilder {
  40. rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_StringValue{StringValue: value}}
  41. return rvb
  42. }
  43. func (rvb *RecordValueBuilder) SetRecord(key string, value *schema_pb.RecordValue) *RecordValueBuilder {
  44. rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_RecordValue{RecordValue: value}}
  45. return rvb
  46. }
  47. func (rvb *RecordValueBuilder) addListValue(key string, values []*schema_pb.Value) *RecordValueBuilder {
  48. rvb.recordValue.Fields[key] = &schema_pb.Value{Kind: &schema_pb.Value_ListValue{ListValue: &schema_pb.ListValue{Values: values}}}
  49. return rvb
  50. }
  51. func (rvb *RecordValueBuilder) SetBoolList(key string, values ...bool) *RecordValueBuilder {
  52. var listValues []*schema_pb.Value
  53. for _, v := range values {
  54. listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_BoolValue{BoolValue: v}})
  55. }
  56. return rvb.addListValue(key, listValues)
  57. }
  58. func (rvb *RecordValueBuilder) SetInt32List(key string, values ...int32) *RecordValueBuilder {
  59. var listValues []*schema_pb.Value
  60. for _, v := range values {
  61. listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_Int32Value{Int32Value: v}})
  62. }
  63. return rvb.addListValue(key, listValues)
  64. }
  65. func (rvb *RecordValueBuilder) SetInt64List(key string, values ...int64) *RecordValueBuilder {
  66. var listValues []*schema_pb.Value
  67. for _, v := range values {
  68. listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_Int64Value{Int64Value: v}})
  69. }
  70. return rvb.addListValue(key, listValues)
  71. }
  72. func (rvb *RecordValueBuilder) SetFloatList(key string, values ...float32) *RecordValueBuilder {
  73. var listValues []*schema_pb.Value
  74. for _, v := range values {
  75. listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_FloatValue{FloatValue: v}})
  76. }
  77. return rvb.addListValue(key, listValues)
  78. }
  79. func (rvb *RecordValueBuilder) SetDoubleList(key string, values ...float64) *RecordValueBuilder {
  80. var listValues []*schema_pb.Value
  81. for _, v := range values {
  82. listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_DoubleValue{DoubleValue: v}})
  83. }
  84. return rvb.addListValue(key, listValues)
  85. }
  86. func (rvb *RecordValueBuilder) SetBytesList(key string, values ...[]byte) *RecordValueBuilder {
  87. var listValues []*schema_pb.Value
  88. for _, v := range values {
  89. listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_BytesValue{BytesValue: v}})
  90. }
  91. return rvb.addListValue(key, listValues)
  92. }
  93. func (rvb *RecordValueBuilder) SetStringList(key string, values ...string) *RecordValueBuilder {
  94. var listValues []*schema_pb.Value
  95. for _, v := range values {
  96. listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_StringValue{StringValue: v}})
  97. }
  98. return rvb.addListValue(key, listValues)
  99. }
  100. func (rvb *RecordValueBuilder) SetRecordList(key string, values ...*schema_pb.RecordValue) *RecordValueBuilder {
  101. var listValues []*schema_pb.Value
  102. for _, v := range values {
  103. listValues = append(listValues, &schema_pb.Value{Kind: &schema_pb.Value_RecordValue{RecordValue: v}})
  104. }
  105. return rvb.addListValue(key, listValues)
  106. }