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

Added defensive copy mechanism #282

Merged
merged 3 commits into from
Mar 7, 2023
Merged

Added defensive copy mechanism #282

merged 3 commits into from
Mar 7, 2023

Conversation

therealryan
Copy link
Contributor

@therealryan therealryan commented Mar 7, 2023

It can be really bad if mutable fields are added to messages - if those values can get updated independently of the message you're left trying to find out why your message has changed when nothing is touching the message itself. It's a painful problem to debug.

Hence the message types are quite strict about what value types can be added - by default they only accept types that we know are immutable.
I've recently butted up against this limitation:

  • I wanted to add java.util.UUID values (the type is immutable)
  • I wanted to add java.sql.Timestamp values (this type is mutable, but it would be really convenient to use it directly)

This change:

  • Adds UUID to the set of immutable types
  • Adds a new mechanism for allowing mutable types as values: you can register a defensive copy function against a type. That function is used when the value is added to the message (we make a copy and store that instead) and when values are accessed from the message (we return a copy of the value that is held by the message)

@therealryan therealryan linked an issue Mar 7, 2023 that may be closed by this pull request
@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 7, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@therealryan therealryan merged commit 012a46c into main Mar 7, 2023
@therealryan therealryan deleted the immut-uuid branch March 7, 2023 11:09
@therealryan therealryan mentioned this pull request Mar 17, 2023
@therealryan therealryan added the enhancement New feature or request label Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Message field defensive copies
5 participants