From 8f6d2591f3a93cbf38a66b4ef0d769f546e301a5 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Tue, 20 Nov 2018 14:17:15 +0100 Subject: [PATCH] Make userdata fields/properties/methods case insensitive While this can potentially cause collisions, this allows us to easily use Lua's style guide --- Upsilon/BaseTypes/UserData/UserDataType.cs | 9 +++++---- UpsilonTests/UserDataTests.cs | 8 ++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Upsilon/BaseTypes/UserData/UserDataType.cs b/Upsilon/BaseTypes/UserData/UserDataType.cs index f6e03f2..a6814da 100644 --- a/Upsilon/BaseTypes/UserData/UserDataType.cs +++ b/Upsilon/BaseTypes/UserData/UserDataType.cs @@ -9,9 +9,9 @@ namespace Upsilon.BaseTypes.UserData public UserDataType(System.Type type) { Type = type; - Variables = type.GetFields().ToDictionary(x => x.Name, x => x); - Properties = type.GetProperties().ToDictionary(x => x.Name, x => x); - Methods = type.GetMethods().ToDictionary(x => x.Name, x => x); + Variables = type.GetFields().ToDictionary(x => x.Name.ToLowerInvariant(), x => x); + Properties = type.GetProperties().ToDictionary(x => x.Name.ToLowerInvariant(), x => x); + Methods = type.GetMethods().ToDictionary(x => x.Name.ToLowerInvariant(), x => x); } private System.Type Type { get; } @@ -23,6 +23,7 @@ namespace Upsilon.BaseTypes.UserData { if (value.GetType() != Type) return null; + member = member.ToLowerInvariant(); if (Variables.TryGetValue(member, out var info)) { return info.GetValue(value).ToLuaType(); @@ -41,6 +42,7 @@ namespace Upsilon.BaseTypes.UserData public void Set(object value, string member, LuaType newValue) { + member = member.ToLowerInvariant(); if (value.GetType() != Type) return; if (Variables.TryGetValue(member, out var info)) @@ -51,7 +53,6 @@ namespace Upsilon.BaseTypes.UserData { property.SetValue(value, newValue.ToCSharpObject()); } - return; } } } \ No newline at end of file diff --git a/UpsilonTests/UserDataTests.cs b/UpsilonTests/UserDataTests.cs index 6fcf411..d5542a6 100644 --- a/UpsilonTests/UserDataTests.cs +++ b/UpsilonTests/UserDataTests.cs @@ -47,7 +47,7 @@ namespace UpsilonTests var obj = new UserDataHelper(); const string input = @" function test(o) - return o[""FieldString""] + return o.fieldString end "; var script = new Script(input); @@ -63,7 +63,7 @@ end var obj = new UserDataHelper(); const string input = @" function test(o) - o[""FieldStringSet""] = ""Test"" + o.FieldStringSet = ""Test"" end "; var script = new Script(input); @@ -79,7 +79,7 @@ end var obj = new UserDataHelper(); const string input = @" function test(o) - o[""TestMethod""]() + o.testMethod() end "; var script = new Script(input); @@ -95,7 +95,7 @@ end var obj = new UserDataHelper(); const string input = @" function test(o) - return o[""Add""](100, 20) + return o.add(100, 20) end "; var script = new Script(input);