From ac3e740e7fd50c8f890e455dffb2a14e094af022 Mon Sep 17 00:00:00 2001 From: ayende Date: Thu, 4 Oct 2007 17:33:31 +0000 Subject: [PATCH] Fixing MR-309, nullable propogation on proxies. git-svn-id: https://svn.castleproject.org/svn/castle/trunk@4370 73e77b4c-caa6-f847-a29a-24ab75ae54b6 --- .../BrailBasicFunctionality.cs | 11 +++++++ MonoRail/Castle.MonoRail.Views.Brail/IgnoreNull.cs | 37 +++++++++++++++------- .../TestSiteBrail/Controllers/HomeController.cs | 25 +++++++++++++-- MonoRail/TestSiteBrail/TestSiteBrail.csproj | 1 + .../home/WithNullableDynamicProxyObject.brail | 12 +++++++ 5 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 MonoRail/TestSiteBrail/Views/home/WithNullableDynamicProxyObject.brail diff --git a/MonoRail/Castle.MonoRail.Views.Brail.Tests/BrailBasicFunctionality.cs b/MonoRail/Castle.MonoRail.Views.Brail.Tests/BrailBasicFunctionality.cs index 8341f7db9..b49d4670e 100644 --- a/MonoRail/Castle.MonoRail.Views.Brail.Tests/BrailBasicFunctionality.cs +++ b/MonoRail/Castle.MonoRail.Views.Brail.Tests/BrailBasicFunctionality.cs @@ -30,6 +30,17 @@ namespace Castle.MonoRail.Views.Brail.Tests } [Test] + public void WithNullableDynamicProxyObject() + { + DoGet("home/WithNullableDynamicProxyObject.rails"); + string expected = @"BarBaz +foo +what? +there"; + AssertReplyEqualTo(expected); + } + + [Test] public void AppPathChangeOnTheFly() { string script = Path.Combine(GetPhysicalDir(), @"Views\AppPath\Index.brail"); diff --git a/MonoRail/Castle.MonoRail.Views.Brail/IgnoreNull.cs b/MonoRail/Castle.MonoRail.Views.Brail/IgnoreNull.cs index 31a938f55..be5359713 100644 --- a/MonoRail/Castle.MonoRail.Views.Brail/IgnoreNull.cs +++ b/MonoRail/Castle.MonoRail.Views.Brail/IgnoreNull.cs @@ -14,6 +14,7 @@ namespace Castle.MonoRail.Views.Brail { + using System.Collections.Generic; using System.Reflection; using Boo.Lang; using Castle.MonoRail.Framework; @@ -31,31 +32,43 @@ namespace Castle.MonoRail.Views.Brail { if (target == null) return this; - PropertyInfo property = target.GetType().GetProperty(name); - if (property == null) - throw new RailsException("Could not find property " + name + " on " + target.GetType().FullName); - return new IgnoreNull(property.GetValue(target, parameters)); + object value; + if (IsNullOrEmpty(parameters)) + value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.GetProperty(target, name); + else + value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.GetSlice(target, name, parameters); + return new IgnoreNull(value); + } + + private static bool IsNullOrEmpty(object[] parameters) + { + return parameters == null || parameters.Length == 0; } public object QuackSet(string name, object[] parameters, object obj) { if (target == null) return this; - PropertyInfo property = target.GetType().GetProperty(name); - if (property == null) - throw new RailsException("Could not find property " + name + " on " + target.GetType().FullName); - property.SetValue(target, obj, parameters); + if (IsNullOrEmpty(parameters)) + ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.SetProperty(target, name, obj); + else + ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.SetSlice(target, name, GetParameterArray(parameters, obj)); return this; } + private static object[] GetParameterArray(object[] parameters, object obj) + { + List args = new List(parameters); + args.Add(obj); + return args.ToArray(); + } + public object QuackInvoke(string name, object[] args) { if (target == null) return this; - MethodInfo method = target.GetType().GetMethod(name); - if (method == null) - throw new RailsException("Could not find method " + name + " on " + target.GetType().FullName); - return new IgnoreNull(method.Invoke(target, args)); + object value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.Invoke(target, name, args); + return new IgnoreNull(value); } public override string ToString() diff --git a/MonoRail/TestSiteBrail/Controllers/HomeController.cs b/MonoRail/TestSiteBrail/Controllers/HomeController.cs index d0e19120f..975ace7bb 100644 --- a/MonoRail/TestSiteBrail/Controllers/HomeController.cs +++ b/MonoRail/TestSiteBrail/Controllers/HomeController.cs @@ -82,12 +82,33 @@ namespace Castle.MonoRail.Views.Brail.TestSite.Controllers PropertyBag["src"] = o; } + public void WithNullableDynamicProxyObject() + { + ProxyGenerator generator = new ProxyGenerator(); + SimpleProxy proxy = (SimpleProxy)generator.CreateClassProxy(typeof(SimpleProxy), new StandardInterceptor()); + PropertyBag["src"] = proxy; + } + public class SimpleProxy { + private string text = "BarBaz"; + readonly Hashtable items = new Hashtable(); + + public virtual object this[string key] + { + get { return items[key]; } + set{ items[key] = value; } + } + public virtual string Text { - get { return "BarBaz"; } - set { } + get { return text; } + set { text = value;} + } + + public virtual string Say() + { + return "what?"; } } diff --git a/MonoRail/TestSiteBrail/TestSiteBrail.csproj b/MonoRail/TestSiteBrail/TestSiteBrail.csproj index 783f244b0..a773e6879 100644 --- a/MonoRail/TestSiteBrail/TestSiteBrail.csproj +++ b/MonoRail/TestSiteBrail/TestSiteBrail.csproj @@ -212,6 +212,7 @@ + diff --git a/MonoRail/TestSiteBrail/Views/home/WithNullableDynamicProxyObject.brail b/MonoRail/TestSiteBrail/Views/home/WithNullableDynamicProxyObject.brail new file mode 100644 index 000000000..03a4b7454 --- /dev/null +++ b/MonoRail/TestSiteBrail/Views/home/WithNullableDynamicProxyObject.brail @@ -0,0 +1,12 @@ +<% output ?src.Text %> +<% +?src.Text = "foo" +output ?src.Text +%> +<% +output ?src.Say() +%> +<% +?src["hi"] = "there" +output ?src["hi"] +%> \ No newline at end of file -- 2.11.4.GIT