Skip to content

Commit

Permalink
Verify Protobuf event data presence in mapper
Browse files Browse the repository at this point in the history
This allows dropping custom initializer for RubyEventStore::Proto

[#409]
  • Loading branch information
fidel committed Oct 29, 2018
1 parent d2272f7 commit 1ddcbaf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
22 changes: 12 additions & 10 deletions ruby_event_store/lib/ruby_event_store/errors.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
module RubyEventStore
WrongExpectedEventVersion = Class.new(StandardError)
InvalidExpectedVersion = Class.new(StandardError)
IncorrectStreamData = Class.new(StandardError)
SubscriberNotExist = Class.new(StandardError)
InvalidPageStart = Class.new(ArgumentError)
InvalidPageSize = Class.new(ArgumentError)
EventDuplicatedInStream = Class.new(StandardError)
NotSupported = Class.new(StandardError)
ReservedInternalName = Class.new(StandardError)
InvalidHandler = Class.new(StandardError)
WrongExpectedEventVersion = Class.new(StandardError)
InvalidExpectedVersion = Class.new(StandardError)
IncorrectStreamData = Class.new(StandardError)
SubscriberNotExist = Class.new(StandardError)
InvalidPageStart = Class.new(ArgumentError)
InvalidPageSize = Class.new(ArgumentError)
EventDuplicatedInStream = Class.new(StandardError)
NotSupported = Class.new(StandardError)
ReservedInternalName = Class.new(StandardError)
InvalidHandler = Class.new(StandardError)
Mappers::Protobuf::InvalidData = Class.new(StandardError)

class EventNotFound < StandardError
attr_reader :event_id

def initialize(event_id)
super("Event not found: #{event_id}")
@event_id = event_id
Expand Down
16 changes: 10 additions & 6 deletions ruby_event_store/lib/ruby_event_store/mappers/protobuf.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
module RubyEventStore
class Proto < RubyEventStore::Event
def initialize(event_id: SecureRandom.uuid, metadata: nil, data:)
super
end

def type
data.class.descriptor.name
end
Expand Down Expand Up @@ -45,8 +41,8 @@ def initialize(events_class_remapping: {})
def event_to_serialized_record(domain_event)
SerializedRecord.new(
event_id: domain_event.event_id,
metadata: ProtobufNestedStruct::HashMapStringValue.dump(domain_event.metadata.each_with_object({}){|(k,v),h| h[k.to_s] =v }),
data: domain_event.data.class.encode(domain_event.data),
metadata: ProtobufNestedStruct::HashMapStringValue.dump(domain_event.metadata.each_with_object({}) {|(k, v), h| h[k.to_s] = v}),
data: encode_data(domain_event.data),
event_type: domain_event.type
)
end
Expand Down Expand Up @@ -74,6 +70,14 @@ def load_data(event_type, protobuf_data)
Google::Protobuf::DescriptorPool.generated_pool.lookup(event_type).msgclass.decode(protobuf_data)
end

def encode_data(domain_event_data)
begin
domain_event_data.class.encode(domain_event_data)
rescue NoMethodError
raise InvalidData
end
end

def require_optional_dependency
require 'protobuf_nested_struct'
rescue LoadError
Expand Down
25 changes: 25 additions & 0 deletions ruby_event_store/spec/mappers/protobuf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,31 @@ def p.require(_name)
expect(event.data.class).to eq(ResTesting::OrderCreated)
expect(event.type).to eq("res_testing.OrderCreated")
end

specify '#event_to_serialized_record raises error when no data' do
domain_event =
RubyEventStore::Proto.new(
event_id: "f90b8848-e478-47fe-9b4a-9f2a1d53622b",
data: nil,
metadata: metadata,
)
expect do
subject.event_to_serialized_record(domain_event)
end.to raise_error(Protobuf::InvalidData)
end

specify '#event_to_serialized_record raises error when wrong data' do
domain_event =
RubyEventStore::Proto.new(
event_id: "f90b8848-e478-47fe-9b4a-9f2a1d53622b",
data: {},
metadata: metadata,
)

expect do
subject.event_to_serialized_record(domain_event)
end.to raise_error(Protobuf::InvalidData)
end
end
end
end

0 comments on commit 1ddcbaf

Please sign in to comment.