Skip to content

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

Open

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));

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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