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

Look up enum value maps by their proto name #315

Merged
merged 3 commits into from
Feb 24, 2017

Conversation

nilium
Copy link
Contributor

@nilium nilium commented Feb 23, 2017

This fixes parsing of the string form of protobuf enum values in query
parameters and fixes the enum value map lookup done by query.go,
originally introduced in #314.

Previously, the runtime looked up the the enum value map via the Go
type name of the enum, derived from its reflect.Type. The Go type name
may not always map 1:1 to the protobuf name, though. For example, if
the proto file declares a package such as google.protobuf, it won't
match since the enum's name is fully qualified when registered for the
proto package. To make sure tests reflected the disparity, the query
test was amended to use a different package name from the Go package
when registering the enum.

To support this, it's necessary to pass the *proto.Properties for
fields forward when populating them, in order to allow the
populateField functions to determine if the field is an enum and look
up its value map by the props.Enum field.

As a result of this, the tests now pass, using the protobuf enum name
as registered (i.e., fully qualified package name) instead of the Go
package name derived by calling (reflect.Type).String. This should have
no real impact on existing use, but it may be justified to re-add
a fallback case for that reflect.Type name for hand-written protobuf
types. Even then, however, it seems unlikely that it should be expected
for enums-by-name to work when registering the enum map under a name
other than the one it's referred to in protobuf.

This also means that the reflect-based name tests will now fail. This
is intentional, we want to use the protobuf enum's name to look it up
via proto.EnumValueMap, not the reflect-based type name, since that can
vary.
This adds pass-through of the *proto.Properties for fields so that the
fields being populated can be addressed by their enum name when the
type is an enum (i.e., (*proto.Properties).Enum is set).

As a result of this, the tests now pass, using the protobuf enum name
as registered (i.e., fully qualified package name) instead of the Go
package name derived by calling (reflect.Type).String. This should have
no real impact on existing use, but it may be justified to re-add
a fallback case for that reflect.Type name for hand-written protobuf
types. Even then, however, it seems unlikely that it should be expected
for enums-by-name to work when registering the enum map under a name
other than the one it's referred to in protobuf.
}
}
return reflect.Value{}
return reflect.Value{}, nil

Choose a reason for hiding this comment

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

Can this nil propagate to calls to populateRepeatedField or populateField?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haven't seen it do so in tests. I could add a nil check around its uses, but an enumeration with no properties seems like bad code gen more than anything else. I'll poke around a bit and see what I can find.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like the only case where it's nil would be if the reflect.Value is invalid. If the reflect.Value is invalid, then the caller returns an error (and in this case, there's only one place that fieldByProtoName is called). Could also add a prop == nil check to where it's being called, though? That'd make doubly sure we've got a proto field, I just don't know if it's paranoid vs. practical.

@tmc
Copy link
Collaborator

tmc commented Feb 23, 2017

@nilium thanks for catching this -- have you signed the CLA?

@nilium
Copy link
Contributor Author

nilium commented Feb 23, 2017

@tmc Yes, I have. I guess the checker isn't working right now? Nevermind, is now.

@tmc
Copy link
Collaborator

tmc commented Feb 24, 2017

Thank you.

@tmc tmc merged commit f3aa758 into grpc-ecosystem:master Feb 24, 2017
@tamalsaha tamalsaha mentioned this pull request Mar 30, 2017
1 task
adasari pushed a commit to adasari/grpc-gateway that referenced this pull request Apr 9, 2020
Look up enum value maps by their proto name
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