Skip to content

Remove OpenStruct usage with Struct replacement#53

Merged
ilyazub merged 2 commits into
serpapi:masterfrom
javierjulio:patch-1
Apr 16, 2024
Merged

Remove OpenStruct usage with Struct replacement#53
ilyazub merged 2 commits into
serpapi:masterfrom
javierjulio:patch-1

Conversation

@javierjulio

@javierjulio javierjulio commented Apr 15, 2024

Copy link
Copy Markdown
Contributor

This replacement to use a struct should still resolve the original issue in #49 but also with #49 (comment) for the reported ruby core issue.

Example you can try in an IRB console:

data = {
  a: 1,
  b: 2,
  c: {
    d: 3,
    e: {
      f: 4,
      g: 5
    }
  }
}

class Hash
  def to_struct
    Struct.new(*self.keys).new(*self.values.map { |value| value.is_a?(Hash) ? value.to_struct : value })
  end
end

result = data.to_struct

This is no longer necessary since we've replaced the single OpenStruct reference with a plain Struct instead.
@javierjulio

javierjulio commented Apr 15, 2024

Copy link
Copy Markdown
Contributor Author

Since #49 also introduced the json gem dependency, I believe that can be removed here since OpenStruct is no longer used. If desired, I can remove the gemspec dependency here.

Comment on lines 3 to 5
def to_struct
OpenStruct.new(self.each_with_object({}) do |(key, val), acc|
acc[key] = val.is_a?(Hash) ? val.to_struct : val
end)
Struct.new(*self.keys).new(*self.values.map { |value| value.is_a?(Hash) ? value.to_struct : value })
end

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm wondering if to_struct is even needed. It converts the notification passed to the group_started method. It looks like, RSpec::Core::Notifications::GroupNotification can be used instead.

@smileart Do you remember why RSpec::Core::Notifications::GroupNotification was not used instead in #7?

Comment thread lib/turbo_tests.rb
require "open3"
require "fileutils"
require "ostruct"
require "json"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

json gem is used TurboTests::JsonRowsFormatter#JsonRowsFormatter to provide to_json method, similarly to the RSpec::Core::Formatters::JsonFormatter#close method.

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.

Oh, indeed! Sounds like we still need json then!

@deivid-rodriguez

Copy link
Copy Markdown
Contributor

Nice! I don't know the root cause of the ruby-core issue, but this direction feels like a nice regardless.

@ilyazub ilyazub left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you, @javierjulio!

I'll merge this PR and address to_struct removal later (ref: https://github.com/serpapi/turbo_tests/pull/53/files#r1567009884),

@ilyazub ilyazub merged commit 6ada9df into serpapi:master Apr 16, 2024
@javierjulio

Copy link
Copy Markdown
Contributor Author

@ilyazub you're welcome and thank you! That sounds good if it's not needed then.

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