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

Upgrading a VersionTolerant Model #185

Closed
dnarayan opened this issue Sep 28, 2023 · 3 comments
Closed

Upgrading a VersionTolerant Model #185

dnarayan opened this issue Sep 28, 2023 · 3 comments

Comments

@dnarayan
Copy link

dnarayan commented Sep 28, 2023

If you have something like this already serialized:

[MemoryPackable(GenerateType.VersionTolerant)]
public partial class Model
{
[MemoryPackOrder(0)]
public string Name { get; set; }
}

Then lets say you need to upgrade the model by adding an additional property, but you want the new property to have non-default value, like this:

[MemoryPackable(GenerateType.VersionTolerant)]
public partial class Model
{
[MemoryPackOrder(0)]
public string Name { get; set; }

[MemoryPackOrder(1)]
[DefaultValue(99)]
public int Age { get; set; } = 99;
}

So when the original model is deserialized the value of Age should be 99 even though this was not in the original byte[]. Is there any way to do that? (I showed how I'd do it with Protobuf-net, but this does not seem to work in Memorypack.)

Thanks!

@hadashiA
Copy link
Contributor

Hello. There seems to be a problem as you have pointed out.
In #242, I tried to fix deriving default values from C# type definitions.
Thanks a lot.

@hadashiA
Copy link
Contributor

hadashiA commented Mar 22, 2024

The feature to detect default values was problematic and was reverted in 1.20.5
We would like to consider another method.

@hadashiA
Copy link
Contributor

Hello.

Before, we tried to take care of all default values, but it is hard to deal with all cases and there were performance problems.
Also, this problem only occurs when the binary is old.

In the end, we only addressed it by adding an attribute for cases where you want to handle a special default value.

Please check [SuppressDefaultInitialization] added in 1.21.0.
https://github.com/Cysharp/MemoryPack?tab=readme-ov-file#version-tolerant

[MemoryPackable].
public partial class DefaultValue
{
    public string Prop1 { get; set; }

    [SuppressDefaultInitialization].
    public int Prop2 { get; set; } = 111; // < if old data is missing, set `111`.
    
    public int Prop3 { get; set; } = 222; // < if old data is missing, set `default`.
}

For members with this attribute, the default value of the type declaration can be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants