split queue.NewBatchMessage funcs

This commit is contained in:
syumai 2024-11-09 23:49:32 +09:00
parent 9cfa0111ef
commit 488aec9585

View File

@ -8,13 +8,31 @@ import (
) )
type BatchMessage struct { type BatchMessage struct {
body any body js.Value
options *sendOptions options *sendOptions
} }
// NewBatchMessage creates a single message to be batched before sending to a queue. func NewTextBatchMessage(content string, opts ...SendOption) *BatchMessage {
func NewBatchMessage(body any, opts ...SendOption) *BatchMessage { return newBatchMessage(js.ValueOf(content), contentTypeText, opts...)
options := defaultSendOptions() }
func NewBytesBatchMessage(content []byte, opts ...SendOption) *BatchMessage {
return newBatchMessage(js.ValueOf(content), contentTypeBytes, opts...)
}
func NewJSONBatchMessage(content any, opts ...SendOption) *BatchMessage {
return newBatchMessage(js.ValueOf(content), contentTypeJSON, opts...)
}
func NewV8BatchMessage(content js.Value, opts ...SendOption) *BatchMessage {
return newBatchMessage(content, contentTypeV8, opts...)
}
// newBatchMessage creates a single message to be batched before sending to a queue.
func newBatchMessage(body js.Value, contentType contentType, opts ...SendOption) *BatchMessage {
options := &sendOptions{
ContentType: contentType,
}
for _, opt := range opts { for _, opt := range opts {
opt(options) opt(options)
} }
@ -26,13 +44,8 @@ func (m *BatchMessage) toJS() (js.Value, error) {
return js.Undefined(), errors.New("message is nil") return js.Undefined(), errors.New("message is nil")
} }
jsValue, err := m.options.ContentType.mapValue(m.body)
if err != nil {
return js.Undefined(), err
}
obj := jsutil.NewObject() obj := jsutil.NewObject()
obj.Set("body", jsValue) obj.Set("body", m.body)
obj.Set("options", m.options.toJS()) obj.Set("options", m.options.toJS())
return obj, nil return obj, nil