Skip to content
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

Preserve types transform #1103

Merged
merged 21 commits into from
Nov 19, 2022
Merged

Preserve types transform #1103

merged 21 commits into from
Nov 19, 2022

Conversation

mpraglowski
Copy link
Member

No description provided.

@swistak35
Copy link
Contributor

I like that 👍

@mpraglowski mpraglowski force-pushed the preserve-types-transform branch 2 times, most recently from 00222dc to f15b5ff Compare August 10, 2021 12:35
def dump(record)
data = transform(record.data)
metadata = transform(record.metadata)
if (metadata.respond_to?(:[]))
Copy link
Member

Choose a reason for hiding this comment

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

can we make an assumption on Metadata being always in an Event?

Copy link
Member Author

Choose a reason for hiding this comment

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

With RubyEventStore::Event this assumption is always true because of its initializer implementation

    def initialize(event_id: SecureRandom.uuid, metadata: nil, data: {})
      @event_id = event_id.to_s
      @metadata = Metadata.new(metadata.to_h)
      @data     = data
    end

If we prepare event with nil metadata (or use another event class that allows that) we will get:

> e = RubyEventStore::Event.new
=> #<RubyEventStore::Event:0x00007fc630389408 @event_id="2755b447-78d1-4dad-aec3-29792a9b6567", @metad...

> e.metadata
=> #<RubyEventStore::Metadata:0x00007fc630388eb8 @h={}>

> e.instance_variable_set("@metadata", nil)
=> nil
> e.metadata
=> nil

> c = RubyEventStore::Client.new(repository: RubyEventStore::InMemoryRepository.new)
=> #<RubyEventStore::Client:0xb4>

> c.publish(e)
Traceback (most recent call last):
       9: from /Users/mpraglowski/.rbenv/versions/2.7.2/bin/irb:23:in `<main>'
       8: from /Users/mpraglowski/.rbenv/versions/2.7.2/bin/irb:23:in `load'
       7: from /Users/mpraglowski/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/irb-1.2.6/exe/irb:11:in `<top (required)>'
       6: from (irb):13
       5: from /Users/mpraglowski/arkency/rails_event_store/ruby_event_store/lib/ruby_event_store/client.rb:32:in `publish'
       4: from /Users/mpraglowski/arkency/rails_event_store/ruby_event_store/lib/ruby_event_store/client.rb:341:in `enrich_events_metadata'
       3: from /Users/mpraglowski/arkency/rails_event_store/ruby_event_store/lib/ruby_event_store/client.rb:341:in `each'
       2: from /Users/mpraglowski/arkency/rails_event_store/ruby_event_store/lib/ruby_event_store/client.rb:341:in `block in enrich_events_metadata'
       1: from /Users/mpraglowski/arkency/rails_event_store/ruby_event_store/lib/ruby_event_store/client.rb:347:in `enrich_event_metadata'
NoMethodError (undefined method `[]' for nil:NilClass)

So here in transformations I could assume metadata are present. The question is if I should make such assumptions for both data & metadata ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

BTW this works because:

> nil.respond_to?(:[])
=> false

@mpraglowski mpraglowski merged commit fd00d43 into master Nov 19, 2022
@mpraglowski mpraglowski deleted the preserve-types-transform branch November 19, 2022 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants