Fix panic when fetching user preferences#28751
Conversation
There was a problem hiding this comment.
I know 🙈 But IMO doesn't make sense to have the default values already specified above and keep adding nested ifs to set the same values instead of using reflection here. I was hoping to find something in proto package, but proto.Range() only iterates over set values, and proto.Merge() merges collections, so it won't work here.
There was a problem hiding this comment.
But we do want to iterate only over fields that are set in src, right? So it seems Range() should do the job?. OTOH Range is pretty close to reflection either way so.. 🤷
There was a problem hiding this comment.
Range() iterates only over non-nil fields, and these are exactly the fields that I need.
There was a problem hiding this comment.
So.. it can be used? AFAICT the flow is as follow:
- find all fields that are set (non-default) in src
- check if field is set in dst
- if not set it to value from src
Rangecan give you the first step, right?
There was a problem hiding this comment.
@zmb3 @probakowski I've removed the reflection explicit from our code.
1756cb2 to
0c4f943
Compare
This commit updates the approach for comparing equality in user preferences test. It introduces the use of the "go-cmp" library which provides more flexibility in handling comparison of struct elements, thus helping to catch any unexpected changes that could be overlooked with the standard equality checking. We also revised the logic to handle default preferences by overwriting values for better efficiency and readability, also removing unnecessary checking and merging of values.
0c4f943 to
0d6bdc5
Compare
| dest.Theme = src.Theme | ||
| } | ||
| // overwriteValues overwrites the values in dst with the values in src. | ||
| func overwriteValues(dst, src protoreflect.ProtoMessage) error { |
There was a problem hiding this comment.
can you please add a comment mentioning that it only works because proto.range only visits non-nil/non-empty fields?
It would become clear to anyone using it how it works.
Added detailed comments to better explain the overwriteValues function in userpreferences.go file. The comments clarify how the function uses proto.Ranges to iterate over fields and only overwrite non-nil/empty fields.
* Fix panic when fetching user preferences Closes #28740 * Prevent overwriting mismatched types in user preferences * Add error handling to user preferences service * GCI imports * Refactor user preferences test and logic This commit updates the approach for comparing equality in user preferences test. It introduces the use of the "go-cmp" library which provides more flexibility in handling comparison of struct elements, thus helping to catch any unexpected changes that could be overlooked with the standard equality checking. We also revised the logic to handle default preferences by overwriting values for better efficiency and readability, also removing unnecessary checking and merging of values. * Add comments to overwriteValues method in userpreferences.go Added detailed comments to better explain the overwriteValues function in userpreferences.go file. The comments clarify how the function uses proto.Ranges to iterate over fields and only overwrite non-nil/empty fields. * Apply some magic to preferences test * Change the import to avoid go.mod changed and match our other imports
* Add onboarding questionnaire components (#27977) * Posthog event for onboarding questionnaire submit (#28328) * update protos * create Posthog event for onboarding questionnaire submit * set onboarding survey on user preferences (#28530) * Fix panic when fetching user preferences (#28751) * Fix panic when fetching user preferences Closes #28740 * Prevent overwriting mismatched types in user preferences * Add error handling to user preferences service * GCI imports * Refactor user preferences test and logic This commit updates the approach for comparing equality in user preferences test. It introduces the use of the "go-cmp" library which provides more flexibility in handling comparison of struct elements, thus helping to catch any unexpected changes that could be overlooked with the standard equality checking. We also revised the logic to handle default preferences by overwriting values for better efficiency and readability, also removing unnecessary checking and merging of values. * Add comments to overwriteValues method in userpreferences.go Added detailed comments to better explain the overwriteValues function in userpreferences.go file. The comments clarify how the function uses proto.Ranges to iterate over fields and only overwrite non-nil/empty fields. * Apply some magic to preferences test * Change the import to avoid go.mod changed and match our other imports * Onboarding changes to support e survey (#28981) * Display a survey during onboarding - Adds user context wrapper to the Welcome flow in order to update preferences - Sets selected resources on user preferences * imports & describes * named enums * remove onboard survey from oss - will be moved to /e - export shared types * refactor welcome - move new credential to welcome prop * refactor new credentails - container passes questionnaire to component * remove survey ls dispatch * pr feedback * Revert "pr feedback" This reverts commit 060f34fb60242856f233df6a1006c5192ea321fb. --------- Co-authored-by: Jakub Nyckowski <jakub.nyckowski@goteleport.com>
Closes #28740