Skip to content

Commit

Permalink
fix: Convert extension attributes to strings, and ignore nils (#19)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Azuma <dazuma@gmail.com>
  • Loading branch information
dazuma committed Sep 2, 2020
1 parent dd1abc6 commit a7ca953
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/cloud_events/event/field_interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def initialize args
end

def finish_attributes
@attributes.merge! @args
@args.each do |key, value|
@attributes[key] = value.to_s unless value.nil?
end
@args = {}
@attributes
end
Expand Down
31 changes: 31 additions & 0 deletions test/event/test_v0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
let(:my_time_string) { "2020-01-12T20:52:05-08:00" }
let(:my_date_time) { DateTime.rfc3339 my_time_string }
let(:my_time) { my_date_time.to_time }
let(:my_trace_parent) { "12345678" }

it "handles string inputs" do
event = CloudEvents::Event::V0.new id: my_id,
Expand Down Expand Up @@ -204,4 +205,34 @@
end
assert_equal "The type field is required", error.message
end

it "handles extension attributes" do
event = CloudEvents::Event::V0.new id: my_id,
source: my_source,
type: my_type,
spec_version: spec_version,
traceparent: my_trace_parent
assert_equal my_trace_parent, event[:traceparent]
assert_equal my_trace_parent, event.to_h["traceparent"]
end

it "handles nonstring extension attributes" do
event = CloudEvents::Event::V0.new id: my_id,
source: my_source,
type: my_type,
spec_version: spec_version,
dataref: my_source
assert_equal my_source_string, event[:dataref]
assert_equal my_source_string, event.to_h["dataref"]
end

it "handles nil extension attributes" do
event = CloudEvents::Event::V0.new id: my_id,
source: my_source,
type: my_type,
spec_version: spec_version,
traceparent: nil
assert_nil event[:traceparent]
refute_includes event.to_h, "traceparent"
end
end
31 changes: 31 additions & 0 deletions test/event/test_v1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
let(:my_time_string) { "2020-01-12T20:52:05-08:00" }
let(:my_date_time) { DateTime.rfc3339 my_time_string }
let(:my_time) { my_date_time.to_time }
let(:my_trace_parent) { "12345678" }

it "handles string inputs" do
event = CloudEvents::Event::V1.new id: my_id,
Expand Down Expand Up @@ -195,4 +196,34 @@
end
assert_equal "The type field is required", error.message
end

it "handles extension attributes" do
event = CloudEvents::Event::V1.new id: my_id,
source: my_source,
type: my_type,
spec_version: spec_version,
traceparent: my_trace_parent
assert_equal my_trace_parent, event[:traceparent]
assert_equal my_trace_parent, event.to_h["traceparent"]
end

it "handles nonstring extension attributes" do
event = CloudEvents::Event::V1.new id: my_id,
source: my_source,
type: my_type,
spec_version: spec_version,
dataref: my_source
assert_equal my_source_string, event[:dataref]
assert_equal my_source_string, event.to_h["dataref"]
end

it "handles nil extension attributes" do
event = CloudEvents::Event::V1.new id: my_id,
source: my_source,
type: my_type,
spec_version: spec_version,
traceparent: nil
assert_nil event[:traceparent]
refute_includes event.to_h, "traceparent"
end
end

0 comments on commit a7ca953

Please sign in to comment.