-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from cocoahero/enum-descriptor
Add `EnumDescriptor` wrapper
- Loading branch information
Showing
7 changed files
with
164 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
module ProtoPlugin | ||
# A wrapper class around `Google::Protobuf::EnumDescriptorProto` | ||
# which provides helpers and more idiomatic Ruby access patterns. | ||
# | ||
# Any method not defined directly is delegated to the descriptor the wrapper was initialized with. | ||
# | ||
# @see https://github.com/protocolbuffers/protobuf/blob/v28.2/src/google/protobuf/descriptor.proto#L336 | ||
class EnumDescriptor < SimpleDelegator | ||
# @return [Google::Protobuf::EnumDescriptorProto] | ||
attr_reader :descriptor | ||
|
||
# The file or message descriptor this enum was defined within. | ||
# | ||
# @return [FileDescriptor] if defined as a root enum | ||
# @return [MessageDescriptor] if defined as a nested enum (inverse of `enum_type`) | ||
attr_reader :parent | ||
|
||
# @param descriptor [Google::Protobuf::EnumDescriptorProto] | ||
# @param parent [FileDescriptorFileDescriptorProto, MessageDescriptor] | ||
# The file or message descriptor this enum was defined within. | ||
def initialize(descriptor, parent) | ||
super(descriptor) | ||
@descriptor = descriptor | ||
@parent = parent | ||
end | ||
|
||
# The full name of the enum, including parent namespace. | ||
# | ||
# @example | ||
# "My::Ruby::Package::EnumName" | ||
# | ||
# @return [String] | ||
def full_name | ||
@full_name ||= begin | ||
prefix = case parent | ||
when MessageDescriptor | ||
parent.full_name | ||
when FileDescriptor | ||
parent.namespace | ||
end | ||
"#{prefix}::#{name}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module ProtoPlugin | ||
class EnumDescriptorTest < Minitest::Test | ||
def setup | ||
@file = FileDescriptor.new( | ||
Google::Protobuf::FileDescriptorProto.new( | ||
package: "my.package.name", | ||
message_type: [ | ||
Google::Protobuf::DescriptorProto.new( | ||
name: "RootMessage", | ||
enum_type: [ | ||
Google::Protobuf::EnumDescriptorProto.new( | ||
name: "ChildEnumOne", | ||
), | ||
], | ||
), | ||
], | ||
enum_type: [ | ||
Google::Protobuf::EnumDescriptorProto.new( | ||
name: "RootEnum", | ||
), | ||
], | ||
), | ||
) | ||
|
||
@enum = @file.enums.first | ||
end | ||
|
||
def test_full_name_of_file_enum | ||
assert_equal("My::Package::Name::RootEnum", @enum.full_name) | ||
end | ||
|
||
def test_full_name_of_message_enum | ||
enum = @file.messages.first.enums.first | ||
assert_equal("My::Package::Name::RootMessage::ChildEnumOne", enum.full_name) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters