Skip to content

Implement HasPresence for C# - #7466

Merged
jskeet merged 1 commit into
protocolbuffers:masterfrom
jskeet:has-presence-2
May 8, 2020
Merged

jskeet merged 1 commit into
protocolbuffers:masterfrom
jskeet:has-presence-2

Conversation

@jskeet

@jskeet jskeet commented May 5, 2020

Copy link
Copy Markdown
Contributor

FieldDescriptor.HasPresence returns true if both ClearValue and HasValue (on the accessor) can be expected to work. Some fields have a working ClearValue, but no HasValue; HasPresence returns false for those fields.

Generally:

  • Extension fields have presence if and only if they're singular
  • Repeated fields do not support presence
  • Map fields do not support presence
  • Message fields support presence
  • Oneof fields support presence (this includes synthetic oneof fields, so that covers proto3 optional singular fields)
  • Proto2 singular primitive fields support presence
  • Proto3 singular primitive fields do not support presence (unless they're in a oneof, covered above)

FieldDescriptor.HasPresence returns true if both ClearValue and HasValue (on the accessor) can be expected to work. Some fields have a working ClearValue, but no HasValue; HasPresence returns false for those fields.

Generally:

- Extension fields have presence if and only if they're singular
- Repeated fields do not support presence
- Map fields do not support presence
- Message fields support presence
- Oneof fields support presence (this includes synthetic oneof fields, so that covers proto3 optional singular fields)
- Proto2 singular primitive fields support presence
- Proto3 singular primitive fields do not support presence (unless they're in a oneof, covered above)
@jskeet
jskeet merged commit 6b0ff74 into protocolbuffers:master May 8, 2020
/// on this field's accessor with a suitable message.
/// </summary>
public bool HasPresence =>
Extension != null ? !Extension.IsRepeated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn't Extension.IsRepeated the same as FieldDescriptor.IsRepeated?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Possibly. I didn't find it simple enough to reason about to convince myself of that - I figured this way may be a little redundant, but at least it's definitely right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants