Skip to content

Commit

Permalink
Append CreatedDate and CreatedBy if its not part of payload or nil
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-cross committed Nov 20, 2024
1 parent d051794 commit 09949a6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pubsub/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ func (c *Client) prepareRecord(codec *goavro.Codec, r opencdc.Record) ([]byte, e
if err != nil {
return nil, errors.Errorf("failed to extract payload data: %w", err)
}
avroPrepared, err := validateAndPreparePayload(data, codec.Schema())
avroPrepared, err := validateAndPreparePayload(data, codec.Schema(), c.userID)
if err != nil {
return nil, errors.Errorf("error validating and preparing avro data:%w", err)
}
Expand Down
11 changes: 10 additions & 1 deletion pubsub/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"crypto/x509"
"encoding/json"
"strings"
"time"

"github.com/conduitio/conduit-commons/opencdc"
"github.com/go-errors/errors"
Expand Down Expand Up @@ -98,7 +99,7 @@ func invalidReplayIDErr(err error) bool {
return strings.Contains(strings.ToLower(err.Error()), "replay id validation failed")
}

func validateAndPreparePayload(dataMap opencdc.StructuredData, avroSchema string) (map[string]interface{}, error) {
func validateAndPreparePayload(dataMap opencdc.StructuredData, avroSchema string, userID string) (map[string]interface{}, error) {
var schema map[string]interface{}
if err := json.Unmarshal([]byte(avroSchema), &schema); err != nil {
return nil, err
Expand Down Expand Up @@ -140,6 +141,14 @@ func validateAndPreparePayload(dataMap opencdc.StructuredData, avroSchema string
}
}

if val, ok := avroRecord["CreatedDate"]; !ok || val == nil {
avroRecord["CreatedDate"] = time.Now().Unix()
}

if val, ok := avroRecord["CreatedById"]; !ok || val == nil {
avroRecord["CreatedById"] = userID
}

return avroRecord, nil
}

Expand Down

0 comments on commit 09949a6

Please sign in to comment.