Skip to content

Commit

Permalink
validate
Browse files Browse the repository at this point in the history
  • Loading branch information
icco committed Sep 14, 2024
1 parent 3e8c7a0 commit 0c5c044
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ type WebVital struct {
Service bigquery.NullString
}

func (wv *WebVital) Validate() error {
if !wv.Service.Valid {
return fmt.Errorf("service is null")
}

if wv.Service.StringVal == "" {
return fmt.Errorf("service is empty")
}

return nil
}

// ParseAnalytics parses a webvitals request body.
func ParseAnalytics(body, service string) (*WebVital, error) {
now := civil.DateTimeOf(time.Now())
Expand Down Expand Up @@ -85,6 +97,12 @@ func getAnalyticsSchema() (bigquery.Schema, error) {

// WriteAnalyticsToBigQuery saves a webvital to bq.
func WriteAnalyticsToBigQuery(ctx context.Context, project, dataset, table string, data []*WebVital) error {
for _, d := range data {
if err := d.Validate(); err != nil {
return fmt.Errorf("validating data: %w", err)
}
}

client, err := bigquery.NewClient(ctx, project)
if err != nil {
return fmt.Errorf("connecting to bq: %w", err)
Expand Down

0 comments on commit 0c5c044

Please sign in to comment.