-
Notifications
You must be signed in to change notification settings - Fork 125
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
Simplify check for Google::Protobuf::Map
type - redux
#1541
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,7 +78,7 @@ class Field < T::Struct | |
|
||
extend T::Sig | ||
|
||
ConstantType = type_member { { fixed: Module } } | ||
ConstantType = type_member { { fixed: T::Class[T.anything] } } | ||
|
||
FIELD_RE = /^[a-z_][a-zA-Z0-9_]*$/ | ||
|
||
|
@@ -206,7 +206,7 @@ def type_of(descriptor) | |
# > field, even if it was not defined in the enum. | ||
"T.any(Symbol, Integer)" | ||
when :message | ||
descriptor.subtype.msgclass.name | ||
descriptor.subtype.msgclass.name || "T.untyped" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a name in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, that returns the name of the protobuf message, not the class name; I was wrong about that in the previous change. For example, in the binary serialized test, if we hadn't assigned the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right! Would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it wouldn't since there is no named constant defined in Ruby that corresponds to that |
||
when :int32, :int64, :uint32, :uint64 | ||
"Integer" | ||
when :double, :float | ||
|
@@ -225,14 +225,24 @@ def nilable_descriptor?(descriptor) | |
descriptor.label == :optional && descriptor.type == :message | ||
end | ||
|
||
sig { params(descriptor: Google::Protobuf::FieldDescriptor).returns(T::Boolean) } | ||
def map_type?(descriptor) | ||
# Defensively make sure that we are dealing with a repeated field | ||
return false unless descriptor.label == :repeated | ||
|
||
# Try to create a new instance with the field that maps to the descriptor name | ||
# being assinged a hash value. If this goes through, then it's a map type. | ||
constant.new(**{ descriptor.name => {} }) | ||
true | ||
rescue ArgumentError | ||
# This means the descriptor is not a map type | ||
false | ||
end | ||
|
||
sig { params(descriptor: Google::Protobuf::FieldDescriptor).returns(Field) } | ||
def field_of(descriptor) | ||
if descriptor.label == :repeated | ||
# Here we're going to check if the submsg_name is named according to | ||
# how Google names map entries. | ||
# https://github.com/protocolbuffers/protobuf/blob/f82e26/ruby/ext/google/protobuf_c/defs.c#L1963-L1966 | ||
if descriptor.submsg_name.to_s.end_with?("_MapEntry_#{descriptor.name}") || | ||
descriptor.submsg_name.to_s.end_with?("FieldsEntry") | ||
if map_type?(descriptor) | ||
key = descriptor.subtype.lookup("key") | ||
value = descriptor.subtype.lookup("value") | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, could this cause problems on applications using a Sorbet version older than sorbet/sorbet#6781?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't: https://github.com/Shopify/tapioca/blob/main/lib/tapioca/sorbet_ext/backcompat_patches.rb#L14-L24