Skip to content

LuaValue.Read<EnumType>() should work for "number" types #198

@CodeSmile-0000011110110111

Description

(Try)Read fail for enum types since LuaValue doesn't know how to handle these even if the value is a number:
luaValue.Read<ApplicationInstallMode>(); // fails

I added this block to LuaValue.TryRead in the LuaValueType.Number case to make this work as expected:

                else if (t.IsEnum)
                {
                    if (!MathEx.IsInteger(value)) break;
                    var v = (long)value;
                    result = Unsafe.As<long, T>(ref v);
                    return true;
                }

Note: this just duplicates the if (t == typeof(long)) block. Could be merged into one.

Now I can use it in my unit test like so:

Assert.That(retvals[2].Read<ApplicationInstallMode>(), 
    Is.EqualTo(ApplicationInstallMode.DeveloperBuild));

Avoids having to read as and cast to a number type:

Assert.That(retvals[2].Read<int>(), 
    Is.EqualTo((int)ApplicationInstallMode.DeveloperBuild));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions