-
Notifications
You must be signed in to change notification settings - Fork 630
Filtering fix for consistency in handling empty string as value #5273
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
Changes from all commits
eda629b
81da624
7c60956
75c455d
9838781
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(), | ||
|
|
@@ -59,8 +60,10 @@ func (attrs attributesFilter) Filter(ctx context.Context, event cloudevents.Even | |
| for k, v := range attrs { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 == "") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ const ( | |
| eventSource = `/mycontext` | ||
| extensionName = `myextension` | ||
| extensionValue = `my-extension-value` | ||
| subjectValue = `mysubject` | ||
| ) | ||
|
|
||
| func TestAttributesFilter_Filter(t *testing.T) { | ||
|
|
@@ -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": { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"), | ||
|
|
@@ -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, | ||
|
|
@@ -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, | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.