-
Notifications
You must be signed in to change notification settings - Fork 15.6k
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
Support hashes for struct initializers #5716
Conversation
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.
👍
@@ -71,6 +71,9 @@ static VALUE table_key(Map* self, VALUE key, | |||
case UPB_TYPE_BYTES: | |||
case UPB_TYPE_STRING: | |||
// Strings: use string content directly. | |||
if (TYPE(key) == T_SYMBOL) { |
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.
Does this also changes API of normal map fields?
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.
I don't think so, and it didn't break any tests that I'm aware of. Can you think of a case where it would make a difference?
This would not throw type errors in some cases where it would have previously but I don't know if that should be considered additive vs a breaking change?
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 doesn't break existing test cases. However, it allows symbol for string map, which is not allowed previously.
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.
Right, is there any reason to prohibit that?
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 makes map not consistent with optional, repeated fields.
Besides, how to compare symbol? identity compare? value compare? Is the semantic same as string?
Besides, is the feature quite important? Otherwise, we don't want to over-provide it, as these trivial features necessarily makes our library complicate.
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 also breaks existing type checking. We should added tests for our type checking.
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 allows users to provide:
Google::Protobuf::Struct.new(fields: {
a: {number_value: 2.0}
})
instead of:
Google::Protobuf::Struct.new(fields: {
'a' => {number_value: 2.0}
})
I would argue that Converting Symbol
key values to String
in Struct
is a beneficial affordance, as Struct
will only every have String
keys. Map
can have any any value type as a key, so it wouldn't make as much sense.
That said, I do think it would be fine to accept Symbol
values every time a String
is expected. In Ruby, I would write it like this:
class Google::Protobuf::Struct
def [](key)
inner_struct[String(key)]
end
end
Basically, anytime we expect a String
object, pass it to the String()
method to perform the necessary conversions.
@TeBoring what kind of type checking tests did you have in mind? I'm happy to add them but I'm not sure exactly what you're asking for. |
The check test I have in mind is something like:
However, this could be a separate change. |
If ruby users do expect Symbol to be the same as string, I think we should make it consistent in all our APIs. |
I'm not sure that the inconsistency is all that bad. Backing up a bit, the first version of this PR didn't include the symbol bit. I was trying to fix an existing issue that prevented using a hash to init a map value: So, instead of this: Google::Protobuf::Struct.new(fields: {
'a' => Google::Protobuf::Value.new({number_value: 2.0})
}) one could write a more concise version: Google::Protobuf::Struct.new(fields: {
'a' => {number_value: 2.0}
}) I shouldn't speak for @blowmage, but he pointed out that getting rid of the hash rocket would look even better. It's shorter and likely the more commonly used syntax. So, I added the implicit string/symbol conversion to allow this: Google::Protobuf::Struct.new(fields: {
a: {number_value: 2.0}
}) Sure, it's a little inconsistent but since a map key can't be symbol allowing either to be used in this context seems like a useful convenience. I'm not sure if I understand what you mean by making it consistent for optional fields or treating string/symbol as the same, etc. msg.a_string = :some_thing
Never mind, that actually works fine already. What else were you thinking we should change? |
@TeBoring any updates on this (and could you run the tests again)? |
I mean please make the Symbol a separate change. |
@TeBoring what other use cases / changes did you have in mind? It looks like Symbol behaves as a string in certain contexts already (see my previous example). What else should we support? |
I see your comment. So you only want to make string map key to accept
symbol?
…On Tue, Mar 26, 2019 at 17:43 Joe Bolinger ***@***.***> wrote:
@TeBoring <https://github.com/TeBoring> what other use cases / changes
did you have in mind? It looks like Symbol behaves as a string in certain
contexts already (see my previous example). What else should we support?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5716 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AE9H5bP6pI_dDmKy6JUliMS8IUqeccFLks5var6kgaJpZM4a4N8P>
.
|
Only for the map in struct?
…On Tue, Mar 26, 2019 at 18:00 Paul Yang ***@***.***> wrote:
I see your comment. So you only want to make string map key to accept
symbol?
On Tue, Mar 26, 2019 at 17:43 Joe Bolinger ***@***.***>
wrote:
> @TeBoring <https://github.com/TeBoring> what other use cases / changes
> did you have in mind? It looks like Symbol behaves as a string in certain
> contexts already (see my previous example). What else should we support?
>
> —
> You are receiving this because you were mentioned.
>
>
> Reply to this email directly, view it on GitHub
> <#5716 (comment)>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AE9H5bP6pI_dDmKy6JUliMS8IUqeccFLks5var6kgaJpZM4a4N8P>
> .
>
|
If you could make the change only affect the map in struct but not other
maps, I am fine with that.
…On Tue, Mar 26, 2019 at 18:02 Paul Yang ***@***.***> wrote:
Only for the map in struct?
On Tue, Mar 26, 2019 at 18:00 Paul Yang ***@***.***> wrote:
> I see your comment. So you only want to make string map key to accept
> symbol?
> On Tue, Mar 26, 2019 at 17:43 Joe Bolinger ***@***.***>
> wrote:
>
>> @TeBoring <https://github.com/TeBoring> what other use cases / changes
>> did you have in mind? It looks like Symbol behaves as a string in certain
>> contexts already (see my previous example). What else should we support?
>>
>> —
>> You are receiving this because you were mentioned.
>>
>>
>> Reply to this email directly, view it on GitHub
>> <#5716 (comment)>,
>> or mute the thread
>> <https://github.com/notifications/unsubscribe-auth/AE9H5bP6pI_dDmKy6JUliMS8IUqeccFLks5var6kgaJpZM4a4N8P>
>> .
>>
>
|
Never mind. I think I can accept this change. I want to let @haberman to also take a look, since this affects API. |
Thanks @TeBoring! I see a build failure but I don't think it's related to this, but let me know if it is or if anything else needs to be done. |
Are you still interested in accepting this change? I think it's a nice simplification but it fell off my radar. Let me know if I can do anything else. |
It seems ruby 2.3 still failed? |
Hmm, that's sort of a strange error that looks like it might be related to the rvm or something unrelated in the environment (openssl?). I can try to reproduce it when I'm back in the office on Monday. |
Maybe sync and push? |
https://www.ruby-lang.org/en/news/2019/03/31/support-of-ruby-2-3-has-ended/ Since the support for ruby 2.3 has been ended, we will deprecate ruby 2.3. |
This change simplifies working with
Google::Protobuf::Struct
andGoogle::Protobuf::Value
so that code like this:can be written as:
This would simplify struct usage in many practical cases, such as: googleapis/google-cloud-ruby#2839
I believe this also resolves #3120.