This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ViewDataEvaluator.cs
211 lines (186 loc) · 8.13 KB
/
ViewDataEvaluator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
namespace Microsoft.AspNetCore.Mvc.ViewFeatures
{
public static class ViewDataEvaluator
{
/// <summary>
/// Gets <see cref="ViewDataInfo"/> for named <paramref name="expression"/> in given
/// <paramref name="viewData"/>.
/// </summary>
/// <param name="viewData">
/// The <see cref="ViewDataDictionary"/> that may contain the <paramref name="expression"/> value.
/// </param>
/// <param name="expression">Expression name, relative to <c>viewData.Model</c>.</param>
/// <returns>
/// <see cref="ViewDataInfo"/> for named <paramref name="expression"/> in given <paramref name="viewData"/>.
/// </returns>
public static ViewDataInfo Eval(ViewDataDictionary viewData, string expression)
{
if (viewData == null)
{
throw new ArgumentNullException(nameof(viewData));
}
// While it is not valid to generate a field for the top-level model itself because the result is an
// unnamed input element, do not throw here if full name is null or empty. Support is needed for cases
// such as Html.Label() and Html.Value(), where the user's code is not creating a name attribute. Checks
// are in place at higher levels for the invalid cases.
var fullName = viewData.TemplateInfo.GetFullHtmlFieldName(expression);
// Given an expression "one.two.three.four" we look up the following (pseudo-code):
// this["one.two.three.four"]
// this["one.two.three"]["four"]
// this["one.two"]["three.four]
// this["one.two"]["three"]["four"]
// this["one"]["two.three.four"]
// this["one"]["two.three"]["four"]
// this["one"]["two"]["three.four"]
// this["one"]["two"]["three"]["four"]
// Try to find a matching ViewData entry using the full expression name. If that fails, fall back to
// ViewData.Model using the expression's relative name.
var result = EvalComplexExpression(viewData, fullName);
if (result == null)
{
if (string.IsNullOrEmpty(expression))
{
// Null or empty expression name means current model even if that model is null.
result = new ViewDataInfo(container: viewData, value: viewData.Model);
}
else
{
result = EvalComplexExpression(viewData.Model, expression);
}
}
return result;
}
/// <summary>
/// Gets <see cref="ViewDataInfo"/> for named <paramref name="expression"/> in given
/// <paramref name="indexableObject"/>.
/// </summary>
/// <param name="indexableObject">
/// The <see cref="object"/> that may contain the <paramref name="expression"/> value.
/// </param>
/// <param name="expression">Expression name, relative to <paramref name="indexableObject"/>.</param>
/// <returns>
/// <see cref="ViewDataInfo"/> for named <paramref name="expression"/> in given
/// <paramref name="indexableObject"/>.
/// </returns>
public static ViewDataInfo Eval(object indexableObject, string expression)
{
// Run through many of the same cases as other Eval() overload.
return EvalComplexExpression(indexableObject, expression);
}
private static ViewDataInfo EvalComplexExpression(object indexableObject, string expression)
{
if (indexableObject == null)
{
return null;
}
if (expression == null)
{
// In case a Dictionary indexableObject contains a "" entry, don't short-circuit the logic below.
expression = string.Empty;
}
return InnerEvalComplexExpression(indexableObject, expression);
}
private static ViewDataInfo InnerEvalComplexExpression(object indexableObject, string expression)
{
Debug.Assert(expression != null);
var leftExpression = expression;
do
{
var targetInfo = GetPropertyValue(indexableObject, leftExpression);
if (targetInfo != null)
{
if (leftExpression.Length == expression.Length)
{
// Nothing remaining in expression after leftExpression.
return targetInfo;
}
if (targetInfo.Value != null)
{
var rightExpression = expression.Substring(leftExpression.Length + 1);
targetInfo = InnerEvalComplexExpression(targetInfo.Value, rightExpression);
if (targetInfo != null)
{
return targetInfo;
}
}
}
leftExpression = GetNextShorterExpression(leftExpression);
}
while (!string.IsNullOrEmpty(leftExpression));
return null;
}
// Given "one.two.three.four" initially, calls return
// "one.two.three"
// "one.two"
// "one"
// ""
// Recursion of InnerEvalComplexExpression() further sub-divides these cases to cover the full set of
// combinations shown in Eval(ViewDataDictionary, string) comments.
private static string GetNextShorterExpression(string expression)
{
if (string.IsNullOrEmpty(expression))
{
return string.Empty;
}
var lastDot = expression.LastIndexOf('.');
if (lastDot == -1)
{
return string.Empty;
}
return expression.Substring(startIndex: 0, length: lastDot);
}
private static ViewDataInfo GetIndexedPropertyValue(object indexableObject, string key)
{
var dict = indexableObject as IDictionary<string, object>;
object value = null;
var success = false;
if (dict != null)
{
success = dict.TryGetValue(key, out value);
}
else
{
// Fall back to TryGetValue() calls for other Dictionary types.
var tryDelegate = TryGetValueProvider.CreateInstance(indexableObject.GetType());
if (tryDelegate != null)
{
success = tryDelegate(indexableObject, key, out value);
}
}
if (success)
{
return new ViewDataInfo(indexableObject, value);
}
return null;
}
// This method handles one "segment" of a complex property expression
private static ViewDataInfo GetPropertyValue(object container, string propertyName)
{
// First, try to evaluate the property based on its indexer.
var value = GetIndexedPropertyValue(container, propertyName);
if (value != null)
{
return value;
}
// Do not attempt to find a property with an empty name and or of a ViewDataDictionary.
if (string.IsNullOrEmpty(propertyName) || container is ViewDataDictionary)
{
return null;
}
// If the indexer didn't return anything useful, try to use PropertyInfo and treat the expression
// as a property name.
var propertyInfo = container.GetType().GetRuntimeProperty(propertyName);
if (propertyInfo == null)
{
return null;
}
return new ViewDataInfo(container, propertyInfo);
}
}
}