Skip to content
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
7 changes: 5 additions & 2 deletions pkg/eventfilter/attributes/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (attrs attributesFilter) Filter(ctx context.Context, event cloudevents.Even
"subject": event.Subject(),
"id": event.ID(),
"time": event.Time().String(),
"dataschema": event.DataSchema(),
"schemaurl": event.DataSchema(),
"datacontenttype": event.DataContentType(),
"datamediatype": event.DataMediaType(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

datamediatype is not a real attribute.

Expand All @@ -59,8 +60,10 @@ func (attrs attributesFilter) Filter(ctx context.Context, event cloudevents.Even
for k, v := range attrs {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This entire method should be rewritten to me more performant. No need to explode the event for filters that do not care about most of the attributes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(this comment was not about this PR, but the code in general. I used it to make a new issue)

var value interface{}
value, ok := ce[k]
// If the attribute does not exist in the event, return false.
if !ok {
// If the attribute does not exist in the event (extension context attributes) or if the event attribute
// has an empty string value (optional attributes) - which means it was never set in the incoming event,
// return false.
if !ok || (v == eventingv1.TriggerAnyFilter && value == "") {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this looks correct.

logging.FromContext(ctx).Debug("Attribute not found", zap.String("attribute", k))
return eventfilter.FailFilter
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/eventfilter/attributes/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
eventSource = `/mycontext`
extensionName = `myextension`
extensionValue = `my-extension-value`
subjectValue = `mysubject`
)

func TestAttributesFilter_Filter(t *testing.T) {
Expand Down Expand Up @@ -72,6 +73,29 @@ func TestAttributesFilter_Filter(t *testing.T) {
event: makeEventWithExtension(extensionName, extensionValue),
want: eventfilter.PassFilter,
},
"Any Extension with attribs - without Extension in Event": {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for the tests 👍

filter: attributesWithExtension(eventType, eventSource, ""),
want: eventfilter.FailFilter,
},
"Any Extension with attribs - with Extension in Event": {
filter: attributesWithExtension(eventType, eventSource, ""),
event: makeEventWithExtension(extensionName, extensionValue),
want: eventfilter.PassFilter,
},
"Subject with attribs": {
filter: attributesWithSubject(eventType, eventSource, subjectValue),
event: makeEventWithSubject(subjectValue),
want: eventfilter.PassFilter,
},
"Any Subject with attribs - without Subject in Event": {
filter: attributesWithSubject(eventType, eventSource, ""),
want: eventfilter.FailFilter,
},
"Any Subject with attribs - with Subject in Event": {
filter: attributesWithSubject(eventType, eventSource, ""),
event: makeEventWithSubject(subjectValue),
want: eventfilter.PassFilter,
},
"Any with attribs - Arrival extension": {
filter: attributes("", ""),
event: makeEventWithExtension(broker.EventArrivalTime, "2019-08-26T23:38:17.834384404Z"),
Expand Down Expand Up @@ -111,6 +135,12 @@ func makeEventWithExtension(extName, extValue string) *cloudevents.Event {
return e
}

func makeEventWithSubject(sub string) *cloudevents.Event {
e := makeEvent()
e.SetSubject(sub)
return e
}

func attributes(t, s string) map[string]string {
return map[string]string{
"type": t,
Expand All @@ -125,3 +155,11 @@ func attributesWithExtension(t, s, e string) map[string]string {
extensionName: e,
}
}

func attributesWithSubject(t, s, sub string) map[string]string {
return map[string]string{
"type": t,
"source": s,
"subject": sub,
}
}