Skip to content

Fix panic when fetching user preferences#28751

Merged
jakule merged 8 commits intomasterfrom
jakule/fix-panic-user-prefs
Jul 11, 2023
Merged

Fix panic when fetching user preferences#28751
jakule merged 8 commits intomasterfrom
jakule/fix-panic-user-prefs

Conversation

@jakule
Copy link
Copy Markdown
Contributor

@jakule jakule commented Jul 6, 2023

Closes #28740

@jakule jakule marked this pull request as ready for review July 6, 2023 14:53
@jakule jakule requested a review from michellescripts July 6, 2023 14:53
@github-actions github-actions Bot requested review from atburke and probakowski July 6, 2023 14:53
Comment thread lib/services/local/userpreferences.go Outdated
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🙈

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.

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.

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.

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.. 🤷

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.

Range() iterates only over non-nil fields, and these are exactly the fields that I need.

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.

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
    Range can give you the first step, right?

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.

@zmb3 @probakowski I've removed the reflection explicit from our code.

@jakule jakule force-pushed the jakule/fix-panic-user-prefs branch from 1756cb2 to 0c4f943 Compare July 11, 2023 19:37
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.
@jakule jakule force-pushed the jakule/fix-panic-user-prefs branch from 0c4f943 to 0d6bdc5 Compare July 11, 2023 19:38
@jakule jakule requested a review from zmb3 July 11, 2023 19:40
@public-teleport-github-review-bot public-teleport-github-review-bot Bot removed the request for review from atburke July 11, 2023 19:44
dest.Theme = src.Theme
}
// overwriteValues overwrites the values in dst with the values in src.
func overwriteValues(dst, src protoreflect.ProtoMessage) error {
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.

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.

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.

Added

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.
@jakule jakule enabled auto-merge July 11, 2023 20:19
@jakule jakule disabled auto-merge July 11, 2023 20:24
@jakule jakule enabled auto-merge July 11, 2023 20:45
@jakule jakule added this pull request to the merge queue Jul 11, 2023
Merged via the queue into master with commit fa78584 Jul 11, 2023
@jakule jakule deleted the jakule/fix-panic-user-prefs branch July 11, 2023 21:19
michellescripts pushed a commit that referenced this pull request Jul 21, 2023
* 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
github-merge-queue Bot pushed a commit that referenced this pull request Jul 21, 2023
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/v1/webapi/user/preferences crashes

5 participants