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

QOL make our gem requireable and autoload/eager load some constants #171

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

davebenvenuti
Copy link
Contributor

@davebenvenuti davebenvenuti commented Jan 15, 2025

Pairing with @nirvdrum , we noticed require "protoboeuf" didn't work despite having it in our Gemfile which led to some confusion (#170). This makes require "protoboeuf" work and auto- and eagerloads some constants for the user.

  • ProtoBoeuf::CodeGen is now autoloaded from the base lib/protoboeuf.rb
  • ProtoBoeuf::Google is also now autoloaded from the base lib/protoboeuf.rb
  • Everything in lib/protoboeuf/google/**/*.rb is eager loaded when ProtoBoeuf::Google loads. There isn't a clean 1:1 mapping between constants and *.rb files so autoload would be fairly tricky. Although this could be a controversial decision, I think this makes things easier to work with. But feedback is welcome.
  • rake well_known_types now generates modules that autoload all child constants for well known types (eg: ProtoBoeuf::Google::Protobuf with the help of ProtoBoeuf::AutoloaderGen!

Copy link
Contributor

@rwstauner rwstauner left a comment

Choose a reason for hiding this comment

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

Makes sense, just a few small things to fix

test/gem_test.rb Outdated Show resolved Hide resolved
lib/protoboeuf/google.rb Outdated Show resolved Hide resolved
@davebenvenuti davebenvenuti force-pushed the davebenvenuti/make-require-protoboeuf-work branch from cafa5bd to ecdc7b5 Compare January 15, 2025 20:34

# There isn't a clean 1:1 mapping between constants and *.rb files, so eager load instead of autoload.

Dir[File.expand_path("google/**/*.rb", __dir__)].each { |file| require file }
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer we don't do this pattern, and rather generate the requires as part of our .proto to .rb step.

2 reasons:

  1. It's important to know what we're requiring
  2. I'm not sure if directory listings are sorted (they weren't in the past) and it can cause platform dependent problems.

If generating a file with the requires is to onerous, I can understand and we can go this route, but I'd prefer if we didn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll give that a whirl

Copy link
Contributor Author

@davebenvenuti davebenvenuti Jan 17, 2025

Choose a reason for hiding this comment

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

Added a new commit: 7dab057

We now generate modules in lib/protoboeuf/google/api.rb and lib/protoboeuf/google/protobuf.rb that autoload all of the constants defined in lib/protoboeuf/google/**/*.rb! Note that they're collapsed by default in the PR diff view because I marked them as autogenerated in .gitattributes. Big thanks to @rwstauner for helping me update the Rakefile in a way where I wouldn't embarrass myself.

https://github.com/Shopify/protoboeuf/blob/7dab057840f374538c061ce438f3b1db5a41f739/lib/protoboeuf/google/api.rb

https://github.com/Shopify/protoboeuf/blob/7dab057840f374538c061ce438f3b1db5a41f739/lib/protoboeuf/google/protobuf.rb

@davebenvenuti davebenvenuti marked this pull request as draft January 17, 2025 00:07
@davebenvenuti davebenvenuti force-pushed the davebenvenuti/make-require-protoboeuf-work branch 8 times, most recently from 5e8c636 to a4ea5b8 Compare January 17, 2025 21:28
@davebenvenuti davebenvenuti force-pushed the davebenvenuti/make-require-protoboeuf-work branch from a4ea5b8 to 7dab057 Compare January 17, 2025 21:30
@davebenvenuti davebenvenuti marked this pull request as ready for review January 17, 2025 21:35
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