-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Firestore: Make @DocumentID value setter private #10167
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
Conversation
Updated the `DocumentID` Swift property wrapper's `value` property to be private(set) since it is ignored in the methods that write to Firestore.
|
Also may considered a breaking change. Adding @peterfriese for input |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am generally in favour of this, since it makes the current behaviour explicit. To my knowledge, there is no way to ever change a Firestore document ID.
I'd like to suggest to document this behaviour at a suitable place on the property wrapper.
Also, I am not sure if it's possible to write a test for this?
|
It's true that you can't change an id, but you could expect an initial write to use a manually set document id. This, however, is not how the api works, so making it read only will clarify any confusion one might have had about that behavior. |
|
LGTM, adding @chliangGoogle for API review. |
@peterfriese I've attempted to add some more documentation to the property wrapper to clarify the usage. I'm not sure that it's possible to write a test for this, beyond the existing |
@mortenbekditlevsen That's helpful feedback though and we'll want to keep it in mind when designing APIs in the future. |
Coverage Report 1Affected Products
Test Logs |
# Conflicts: # Firestore/Swift/CHANGELOG.md # Firestore/Swift/Source/Codable/DocumentID.swift
#10275) * [Firestore] Mark `@DocumentID` setter deprecated and add log statement in init * Fix typo in import * Update CHANGELOG * Review and remove deprecation * Review and add FIRLogWarningSwift * Add FirebaseCorExtension to Firestore's Podfile
|
Merged associated PRs and resolved simple conflicts in the last two commits. Proceeding with merge. Will move release tags, stage |
|
Zip build in progress at https://github.com/firebase/firebase-ios-sdk/actions/runs/3141588403 |
| "[FirebaseFirestoreSwift]", | ||
| "I-FST000002", | ||
| """ | ||
| Attempting to initialize or set a @DocumentID property with a non-nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Breaking the error message like this results in the following, which is less than ideal:
2022-09-28 12:49:44.813075+0200 Make It So[29167:1864911] 10.0.0 - [FirebaseFirestoreSwift][I-FST000002] Attempting to initialize or set a @DocumentID property with a non-nil
value: B89FE0DD-7ACB-4D55-9FBE-CFC70512E760. The document ID is managed by Firestore and any
initialized or set value will be ignored. The ID is automatically set
when reading from Firestore."
Can we make sure the error message ends up being a single line? It also seems like there is a stray quotation mark at the end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @peterfriese, if you don't mind and your app still running... Could you suggest the formatted string that renders it better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! In a comment, or rather on a follow-up PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do a follow-up PR!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I didn't reply in time. This looks good to me on the console:
message: "Attempting to initialize or set a @DocumentID property with a non-nil value:"
+ " \"\(value)\". The document ID is managed by Firestore and any initialized or set value"
+ " will be ignored. The ID is automatically set when reading from Firestore."
I'm hoping the compiler will optimize it into one long string but it may do concatenation at runtime, not sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've found a way to use multi-line strings, see #10289
| get { value } | ||
| set { value = newValue } | ||
| set { | ||
| if let someNewValue = newValue { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why we use someNewValue here? In line 111, we use if let value = value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I recall being confused about introducing another newValue into the scope. Mainly because I didn't know if it could cause problems on L123. Swift should be able to understand that the non-optional newValue is used in the unwrapped scope and the optional newValue should be set in the outer scope on L123. But I figured it was possible that an older Swift version may not know. To be extra safe (it was getting late when I worked on this 😅 ), I opted for introducing a new name in the scope.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess in Swift 5.7, we should even be able to use
if let newValue {
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we can use this feature yet since we still need to build for devs using Xcode 13.3.1 x Swift 5.6.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just verified my assumption.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, wrt the below #10167 (comment), the setter should log if you did something like:
struct Model: Codable {
@DocumentID var id: String = "hello"
}
let model = // ...
model.id = nil // ⚠️ Logs a warning ⚠️
Updated the
DocumentIDSwift property wrapper'svalueproperty to be private(set) since it is ignored in the methods that write to Firestore.Existing Implementation (See issue #9368)
Updated Implementation
API