Skip to content
10 changes: 9 additions & 1 deletion internal/events/nats/natschannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,15 @@ func sendEvent(
event.SetID(msg.UUID)
event.SetType(eventType)
event.SetSource("minder") // The system which generated the event. The Minder URL would be nice here.
event.SetSubject("TODO") // This *should* represent the entity, but we don't have a standard field for it yet.
subject := ""

if val, ok := msg.Metadata["entity_id"]; ok && val != "" {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to check for presence if you're already checking that val is non-zero-valued.

Suggested change
if val, ok := msg.Metadata["entity_id"]; ok && val != "" {
if val := msg.Metadata["entity_id"]; val != "" {

subject = val
} else {
subject = "minder"
}

event.SetSubject(subject)
Comment on lines +209 to +217

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a stupid little Go trick, but you can use the default-zero return value from map lookup combined with cmp.Or (return first non-zero value) to write this as:

Suggested change
subject := ""
if val, ok := msg.Metadata["entity_id"]; ok && val != "" {
subject = val
} else {
subject = "minder"
}
event.SetSubject(subject)
event.SetSubject(cmp.Or(msg.Metadata["entity_id"], "minder"))


// All our current payloads are encoded JSON; we need to unmarshal
payload := map[string]any{}
Expand Down
Loading