Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append CreatedDate and CreatedBy if its not part of payload or nil #156

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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