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

Clear oneofs correctly #6

Merged
merged 1 commit into from
Apr 25, 2024
Merged

Conversation

rodaine
Copy link
Owner

@rodaine rodaine commented Apr 25, 2024

When protogofakeit attempts to fake out a oneof union, depending on the random value it rolls it may choose to have the oneof have no set fields. Instead of ignoring it, the faker attempts to clear the oneof, in the event the message already had fields set within the oneof. The previous logic handled this by populating the first field in the oneof with its default (protoreflect.FieldDescriptor#Default), and then clearing that field, which would force the oneof to be empty. This worked fine for scalar fields, but a message field's default is nil. Calling protoreflect.Message#Set with a nil value results in a panic.

Two solutions were available here. First, is to instead do protoreflect.Message#NewField which would return a viable message value that could be set and subsequently cleared, but I realized that this was unnecessary work to clear the field. Instead, the message is now interrogated about which field in the oneof is set (if any) and clear that specific field. Much better imo.

Fixes #5

@rodaine rodaine merged commit dea7fe1 into main Apr 25, 2024
3 checks passed
@rodaine rodaine deleted the rodaine/oneof-first-field-message branch April 25, 2024 16:04
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.

Panic: repeated oneOf causes Panic
1 participant