Skip to content

What is the correct way in proto3 to use HasField? #1402

Description

@dhermes

Originally came up in #1329. HasField with proto3 fails on simple / singular / non-message fields.

@nathanielmanistaatgoogle Mentioned that @haberman might be able to shine some light. Josh, what should we be doing here? The current approach isn't great:

# NOTE: As of proto3, HasField() only works for message fields, not for# singular (non-message) fields. First try to use HasField and# if it fails (with a ValueError) we manually consult the fields.try:
returnmessage_pb.HasField(property_name)
exceptValueError:
all_fields=set([field.nameforfieldinmessage_pb._fields])
returnproperty_nameinall_fields

Should we bother with checking "has field", e.g.

if_has_field(key_pb.partition_id, 'dataset_id'):
...

or should we instead just check Truth-iness

ifkey_pb.partition_id.dataset_id:
...
# OR MORE STRICTifkey_pb.partition_id.dataset_id!='':
...

Metadata

Metadata

Assignees

Labels

api: coreapi: datastoreIssues related to the Datastore API.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions