-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
36 lines (29 loc) · 993 Bytes
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package PubSubToBQ
import (
"context"
"encoding/json"
"fmt"
"cloud.google.com/go/bigquery"
)
// validateMessage validates the Pub/Sub message data.
// It checks if the message data is empty and returns an error if true.
// msg: The MessagePublishedData containing the Pub/Sub message.
// Returns an error if the message is invalid.
func validateMessage(msg MessagePublishedData) error {
if len(msg.Message.Data) == 0 {
return fmt.Errorf("empty message data")
}
return nil
}
// processMessage unmarshals a message and inserts it into BigQuery
// It returns an error if unmarshaling or insertion fails
func processMessage(ctx context.Context, messageData []byte, inserter *bigquery.Inserter) error {
var data BigQueryRow
if err := json.Unmarshal(messageData, &data); err != nil {
return fmt.Errorf("failed to unmarshal message: %w", err)
}
if err := inserter.Put(ctx, data); err != nil {
return fmt.Errorf("failed to insert data into BigQuery: %w", err)
}
return nil
}