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
Firestore: more flexible serialization #2534
Comments
The issue is the same for |
I definitely don't want to support While we could support the implicit conversion, it does introduce extra complexity which I'd really rather not - as per our previous discussions. (If there are multiple implicit conversions, which one is used? How does inheritance play into this? What about explicit conversions?) Now that private properties are supported, it's easy to work around this though with no library change: [FirestoreData]
public class UserAccount
{
public Email EmailAddress { get; set; }
// Private property to serialize the data for Firestore
[FirestoreProperty("email")]
private string FirestoreEmail { get => EmailAddress; set => EmailAddress = value; }
} |
Thank you for your response. I wasn't aware of I will also agree that implicit conversation is not a good solution either. Since this is clearly a feature request, I would like to propose a more generic solution to handle this and other scenarios where some sort of custom serialization is required: Provide a generic interface like public interface IFirestoreValueSerializer
{
Value SerializeValue(object value);
object DeserializeValue(Value value);
} Allow [FirestoreData]
public class UserAccount
{
[FirestoreProperty("Email", FirestoreValueSerializerType = typeof(MyValueSerializer))]
public Email Email { get; set; }
} |
That sounds reasonable - I suspect there'll need to be a lot of warnings about not reusing I suspect the It's unlikely that I'll get to this for about a month I'm afraid due to other commitments - but at least the workaround should help you out for the moment. |
I am happy with the workaround for now. Thank you. Btw, are pull requests welcome on this repository? |
Yes, although I may well not be able to prioritize reviewing a PR any higher than writing the code itself, I'm afraid :( |
Any updates on this case? |
No updates at the moment, other than that I need to write a doc to present to the Firestore team about this. (We're not developing in a vacuum here - it gives us slightly less flexibility, but will help with consistency.) I do have that on my work backlog, but Spanner work is higher priority right now. |
Thank you for the feedback. |
This is to demonstrate *one* possible approach to googleapis#2534.
Right, we've made some progress in internal discussions, and we'd like some feedback if possible. We'd like the interface to use POCOs rather than protos, so that users don't have to know anything about protos at all. At first glance, that would suggest an interface like this: public interface IFirestoreValueSerializer
{
object SerializeValue(object value);
object DeserializeValue(object value);
} However, I suspect it's actually going to be cleaner to view this as an interface that you use to decorate a type rather than a property. I've written a prototype with this interface instead: /// <summary>
/// Converter used during serialization
/// </summary>
public interface IFirestoreConverter<T>
{
/// <summary>
/// Converts a value to its Firestore representation.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
object ToFirestore(T value);
/// <summary>
/// Converts a value from its Firestore representation.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
T FromFirestore(object value);
} The user code might look like this:
The document would just be a map with two string values and one integer value. Types with custom converters could only be used as document "root" types if they serialized to dictionaries, but that's probably a reasonable restriction. To be clear, this is not a promise that this is the direction we'll take, but it's an initial prototype just for feedback. Do you believe this would satisfy your requirements? |
Hi John, thank for your feedback. I will follow you on this one. Decorating a type rather than every property of that type is much cleaner. This would completely satisfy my requirements. Regarding |
The intention is that users shouldn't need to know about It's possible that we could add an option to the interface saying "Just pass me the value, I'll do everything" but that would probably come later. |
It makes sense. I am happy with this solution. |
Hooray. I'll keep this issue updated with progress. (Even if the API is exactly as written, my current prototype implementation is decidedly not production-ready, or comprehensively tested!) Thanks for all the feedback, helping to make the API better for all users. |
Thank you. |
PR #2734 is the first pass of the "real" version of this. |
This completes the work for googleapis#2534.
This completes the work for googleapis#2534.
This completes the work for googleapis#2534.
Available in version 1.0.0-beta14. |
Great! Thank you very much. I will give it a try next week. |
I am having some issues trying to get Firestore API to convert between
StringValue
and DDD value objects which are fully convertible from/to string. Let me give an example:When calling
snapshot.ConvertTo<UserAccount>()
,ValueDeserializer
fails withSystem.ArgumentException: 'Unable to convert value type StringValue to Email'
.It seems
ValueDeserializer
ignores type converters and implicit operators completely.The text was updated successfully, but these errors were encountered: