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

descriptor bytes and fully qualified name helpers #397

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

nerdrew
Copy link
Contributor

@nerdrew nerdrew commented Jun 10, 2019

  • add the descriptor proto bytes for each generated proto file
  • add a helper for the fully qualified name of each proto.

@nerdrew
Copy link
Contributor Author

nerdrew commented Jun 10, 2019

This has failing specs for the functional code generation specs. Those specs seem to rely on the generated message from a pretty old version of protoc. The failure output is also pretty hard to read. Thoughts on refactoring the tests?

I'd like to remove the bytes comparisons and instead require the generated files and check some of the fields, check encode -> decode roundtrip, check the custom options work as expected.

@nerdrew nerdrew force-pushed the nerdrew-include-descriptor-in-generated-code branch from a7ccbde to e2f173b Compare June 10, 2019 21:45
@nerdrew
Copy link
Contributor Author

nerdrew commented Jun 10, 2019

e.g., I'm thinking of tests like this:

  before(:all) do
    require PROTOS_PATH.join('google_unittest.pb')
    require PROTOS_PATH.join('map-test.pb')
    require PROTOS_PATH.join('google_unittest_custom_options.pb')
  end

  it "generates code for google's unittest.proto" do
    attrs = {
      optional_int32: 1,
      repeated_string: %w[boom],
      optional_nested_message: { bb: 10 },
      optional_nested_enum: ::Protobuf_unittest::TestAllTypes::NestedEnum::BAZ,
      optional_foreign_message: { c: 12 },
      repeated_import_message: [{ d: 13 }],
    }
    bytes = ::Protobuf_unittest::TestAllTypes.new(attrs).encode
    message = ::Protobuf_unittest::TestAllTypes.decode(bytes)
    expect(message.to_hash).to eq(attrs)
    expect(message.default_int32).to eq(41)
    expect(::Protobuf_unittest::TestAllTypes::FULLY_QUALIFIED_NAME).to eq('protobuf_unittest.TestAllTypes')
    descriptor_file = ::Protobuf_unittest.descriptor_set.file.first
    expect(descriptor_file.name).to eq('protos/google_unittest.proto')
    expect(descriptor_file.package).to eq('protobuf_unittest')
  end

  it "generates code for google's unittest.proto extensions" do
    attrs = { optional_import_message_extension: { d: 14 } }
    bytes = ::Protobuf_unittest::TestAllExtensions.new(attrs).encode
    message = ::Protobuf_unittest::TestAllExtensions.decode(bytes)
    expect(message.to_hash).to eq(attrs)
    expect(message.default_int64_extension).to eq(42)
  end

@nerdrew
Copy link
Contributor Author

nerdrew commented Jun 12, 2019

cc @embark

@dsimmsatsquare
Copy link

I am completely biased, but I would love to have something like this! Thanks @nerdrew !

@nerdrew nerdrew force-pushed the nerdrew-include-descriptor-in-generated-code branch 2 times, most recently from 3b29e47 to 594fb56 Compare June 25, 2019 18:14
@nerdrew
Copy link
Contributor Author

nerdrew commented Jun 25, 2019

The tests currently pass, but rubocop fails. I can update rubocop if this PR otherwise looks good. Thoughts on dropping support for EOL-ed ruby versions and updating rubocop to a modern version? (Both would be separate PRs).

@nerdrew nerdrew force-pushed the nerdrew-include-descriptor-in-generated-code branch from 594fb56 to 681633d Compare June 25, 2019 18:39
task :spec do
proto_path = ::File.expand_path('../spec/support/', __FILE__)
proto_files = Dir[File.join(proto_path, '**', '*.proto')]
cmd = %(protoc --plugin=./bin/protoc-gen-ruby --ruby_out=#{proto_path} -I #{proto_path} #{proto_files.join(' ')})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Related to: #345

@nerdrew nerdrew force-pushed the nerdrew-include-descriptor-in-generated-code branch from 681633d to 9899d92 Compare July 2, 2019 17:25
@nerdrew
Copy link
Contributor Author

nerdrew commented Jul 8, 2019

Any thoughts on this?

@dsimmsatsquare
Copy link

Any thoughts on this?

I for one love these changes to include the descriptors and type names! The functional specs I am less help with, but would help if you need.

- add the descriptor proto bytes for each generated proto file
- add a helper for the fully qualified name of each proto.
- add ruby 2.6 to travis, remove EOL-ed rubies
- the newer versions of protoc have a --ruby_out that takes precedence,
alias the ruby plugin so it works with new versions of protoc
- use binary protoc version 3.8.0
@nerdrew nerdrew force-pushed the nerdrew-include-descriptor-in-generated-code branch from 9899d92 to 34346ee Compare July 15, 2019 16:52
@nerdrew
Copy link
Contributor Author

nerdrew commented Apr 6, 2023

We've been dog fooding this for years now. Any objection to a rebase + merge? cc @film42

@film42
Copy link
Member

film42 commented Apr 6, 2023

@nerdrew I'm down to rebase and merge. Y'all have been great with your previous patches. A few questions:

  1. Any breaking changes with this?
  2. Is there a reason for printing the proto bytes?

@nerdrew
Copy link
Contributor Author

nerdrew commented Apr 6, 2023

  • Any breaking changes with this?

I don't think there are any breaking changes. We added constants all over with the fully qualified name for messages, enums, etc.

  • Is there a reason for printing the proto bytes?

I.e. why'd we add the proto descriptor bytes after __END__ in these files? We have some dynamic systems that don't know what the proto message it will handle looks like, so you send in a descriptor proto along with the serialized message. The service can then dynamically generate code to deserialize the proto. By packaging the descriptor + serialized bytes, the combo becomes "self describing" in a way.

@nerdrew
Copy link
Contributor Author

nerdrew commented Apr 6, 2023

I'll rebase and cleanup soon.

@film42
Copy link
Member

film42 commented Apr 11, 2023

Thanks @nerdrew ! Makes sense about the descriptor being printed. Only nit would be how it's added to the files themselves. Could this be something we add after calling a dsl method like:

class SomeMessage < ::ProtobufMessage
  descriptor_bytes "<base64_encoded_message>"
end

where descriptor_bytes sets up the descriptors method calls?

More specifically, the instance variable checking as emitted code doesn't look particularly great for something that's supposed to be a human readable DSL.

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