Table of Contents

Class MigrateFromAttribute

Namespace
KeenEyes
Assembly
KeenEyes.Abstractions.dll

Marks a static method as a migration handler for upgrading component data from an older schema version.

[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
[ExcludeFromCodeCoverage]
public sealed class MigrateFromAttribute : Attribute
Inheritance
MigrateFromAttribute
Inherited Members

Examples

[Component(Serializable = true, Version = 3)]
public partial struct Position : IComponent
{
    public float X;
    public float Y;
    public float Z;  // Added in v2
    public float W;  // Added in v3

    /// <summary>
    /// Migrates from v1 (X, Y only) to v2 (adds Z).
    /// </summary>
    [MigrateFrom(1)]
    private static Position MigrateFromV1(JsonElement oldData)
    {
        return new Position
        {
            X = oldData.GetProperty("x").GetSingle(),
            Y = oldData.GetProperty("y").GetSingle(),
            Z = 0f  // Default value for new field
        };
    }

    /// <summary>
    /// Migrates from v2 (X, Y, Z) to v3 (adds W).
    /// </summary>
    [MigrateFrom(2)]
    private static Position MigrateFromV2(JsonElement oldData)
    {
        return new Position
        {
            X = oldData.GetProperty("x").GetSingle(),
            Y = oldData.GetProperty("y").GetSingle(),
            Z = oldData.GetProperty("z").GetSingle(),
            W = 1f  // Default value for new field
        };
    }
}

Remarks

Migration methods are used during deserialization to transform component data from older versions to the current version. When a serialized component has a lower version than the current Version, the migration pipeline will invoke the appropriate migration methods in sequence.

Migration methods must:

  • Be static
  • Return the component type they are defined in
  • Take a JsonElement parameter containing the old component data

For multi-version migrations (e.g., v1 → v4), define separate migration methods for each version step. The migration pipeline will chain them automatically: v1 → v2 → v3 → v4.

Constructors

MigrateFromAttribute(int)

Initializes a new instance of the MigrateFromAttribute class.

public MigrateFromAttribute(int fromVersion)

Parameters

fromVersion int

The source version that this migration handles. Must be at least 1 and less than the current Version.

Exceptions

ArgumentOutOfRangeException

Thrown when fromVersion is less than 1.

Properties

FromVersion

Gets the source version that this migration handles.

public int FromVersion { get; }

Property Value

int

Remarks

The migration method will be invoked when deserializing component data with this version. The method should return component data compatible with version FromVersion + 1.