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

Remove OpenStruct usage with Struct replacement #53

Merged
merged 2 commits into from
Apr 16, 2024

Conversation

javierjulio
Copy link
Contributor

@javierjulio javierjulio commented Apr 15, 2024

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
Copy link
Contributor Author

javierjulio commented Apr 15, 2024

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
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?

@@ -2,7 +2,6 @@

require "open3"
require "fileutils"
require "ostruct"
require "json"
Copy link
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
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
Contributor

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

Copy link
Collaborator

@ilyazub ilyazub left a comment

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
5 checks passed
@javierjulio
Copy link
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.

None yet

3 participants