forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptMethods.cs
More file actions
371 lines (308 loc) · 15.1 KB
/
ScriptMethods.cs
File metadata and controls
371 lines (308 loc) · 15.1 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using ServiceStack.Model;
using ServiceStack.Text;
using ServiceStack.Web;
namespace ServiceStack.Script
{
public enum InvokerType
{
Filter,
ContextFilter,
ContextBlock,
}
public interface IResultInstruction {}
public class IgnoreResult : IResultInstruction
{
public static readonly IgnoreResult Value = new IgnoreResult();
private IgnoreResult(){}
}
public class StopExecution : IResultInstruction
{
public static StopExecution Value = new StopExecution();
private StopExecution() { }
}
public class StopFilterExecutionException : StopExecutionException, IResponseStatusConvertible
{
public ScriptScopeContext Scope { get; }
public object Options { get; }
public StopFilterExecutionException(ScriptScopeContext scope, object options, Exception innerException)
: base(nameof(StopFilterExecutionException), innerException)
{
Scope = scope;
Options = options;
}
public ResponseStatus ToResponseStatus()
{
var ex = InnerException ?? this;
return new ResponseStatus {
ErrorCode = ex.GetType().Name,
Message = ex.Message,
StackTrace = ex.StackTrace.LeftPart('\n'),
Meta = new Dictionary<string, string> {
[GetType().Name] = Message + "\n" + this.StackTrace.LeftPart('\n')
}
};
}
}
public class ScriptException : Exception
{
public PageResult PageResult { get; }
public string PageStackTrace => PageResult.LastFilterStackTrace.Map(x => " at " + x).Join(Environment.NewLine);
public ScriptException(PageResult pageResult) : base(
pageResult.LastFilterError?.Message ?? throw new ArgumentNullException(nameof(pageResult)),
pageResult.LastFilterError)
{
PageResult = pageResult;
}
}
public class ScriptMethods
{
public ScriptContext Context { get; set; }
public ISharpPages Pages { get; set; }
private readonly Dictionary<string, MethodInfo> lookupIndex = new Dictionary<string, MethodInfo>();
public ScriptMethods()
{
var methods = GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public);
foreach (var method in methods)
{
var paramType = method.GetParameters().FirstOrDefault()?.ParameterType;
var hasScope = paramType == typeof(ScriptScopeContext) || paramType == typeof(Templates.TemplateScopeContext);
var hasTaskReturnType = method.ReturnType == typeof(Task);
var isFilter = !hasScope && !hasTaskReturnType;
var isContextFilter = hasScope && !hasTaskReturnType;
var isBlockFilter = hasScope && hasTaskReturnType;
if (!isFilter && !isContextFilter && !isBlockFilter)
continue;
var filterType = isBlockFilter
? InvokerType.ContextBlock
: isContextFilter
? InvokerType.ContextFilter
: InvokerType.Filter;
var key = CacheKey(filterType, method.Name, method.GetParameters().Length);
lookupIndex[key] = method;
}
}
private string CacheKey(InvokerType type, string methodName, int argsCount) =>
type + "::" + methodName + "`" + argsCount;
private MethodInfo GetFilterMethod(string cacheKey) => lookupIndex.TryGetValue(cacheKey, out MethodInfo method) ? method : null;
public List<MethodInfo> QueryFilters(string filterName)
{
var filters = GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public)
.Where(x => x.Name.IndexOf(filterName, StringComparison.OrdinalIgnoreCase) >= 0)
.ToList();
return filters;
}
public ConcurrentDictionary<string, MethodInvoker> InvokerCache { get; } = new ConcurrentDictionary<string, MethodInvoker>();
public MethodInvoker GetInvoker(string name, int argsCount, InvokerType type)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
var key = CacheKey(type, name, argsCount);
if (InvokerCache.TryGetValue(key, out MethodInvoker invoker))
return invoker;
var method = GetFilterMethod(key);
if (method == null)
return null;
return InvokerCache[key] = TypeExtensions.GetInvokerToCache(method);
}
}
public static class TemplateFilterUtils
{
public static Dictionary<string, object> AssertOptions(this ScriptScopeContext scope, string filterName, object scopedParams)
{
var pageParams = scopedParams as Dictionary<string, object>;
if (pageParams == null && scopedParams != null)
throw new ArgumentException(
$"{filterName} in '{scope.PageResult.VirtualPath}' only accepts an Object dictionary as an argument but received a '{scopedParams.GetType().Name}' instead");
return pageParams ?? new Dictionary<string, object>();
}
public static Dictionary<string, object> AssertOptions(this object scopedParams, string filterName)
{
var pageParams = scopedParams as Dictionary<string, object>;
if (pageParams == null && scopedParams != null)
throw new ArgumentException(
$"{filterName} only accepts an Object dictionary as an argument but received a '{scopedParams.GetType().Name}' instead");
return pageParams ?? new Dictionary<string, object>();
}
public static object AssertNoCircularDeps(this object value)
{
if (value != null && TypeSerializer.HasCircularReferences(value))
throw new NotSupportedException($"Cannot serialize type '{value.GetType().Name}' with cyclical dependencies");
return value;
}
public static IEnumerable<object> AssertEnumerable(this object items, string filterName)
{
var enumObjects = items as IEnumerable<object>;
if (enumObjects == null && items != null)
{
if (items is IEnumerable enumItems)
{
var to = new List<object>();
foreach (var item in enumItems)
{
to.Add(item);
}
return to;
}
throw new ArgumentException(
$"{filterName} expects an IEnumerable but received a '{items.GetType().Name}' instead");
}
return enumObjects ?? TypeConstants.EmptyObjectArray;
}
public static string AssertExpression(this ScriptScopeContext scope, string filterName, object expression)
{
if (!(expression is string literal))
throw new NotSupportedException($"'{filterName}' in '{scope.PageResult.VirtualPath}' requires a string Expression but received a '{expression?.GetType()?.Name}' instead");
return literal;
}
public static JsToken AssertExpression(this ScriptScopeContext scope, string filterName, object expression, object scopeOptions, out string itemBinding)
{
if (expression is JsArrowFunctionExpression arrowExpr)
{
itemBinding = arrowExpr.Params[0].Name;
return arrowExpr.Body;
}
var literal = scope.AssertExpression(filterName, expression);
var scopedParams = scope.GetParamsWithItemBinding(filterName, scopeOptions, out itemBinding);
var token = literal.GetCachedJsExpression(scope);
return token;
}
public static Dictionary<string, object> GetParamsWithItemBinding(this ScriptScopeContext scope, string filterName, object scopedParams, out string itemBinding) =>
GetParamsWithItemBinding(scope, filterName, null, scopedParams, out itemBinding);
public static Dictionary<string, object> GetParamsWithItemBinding(this ScriptScopeContext scope, string filterName, SharpPage page, object scopedParams, out string itemBinding)
{
var scopeParams = scope.GetParamsWithItemBindingOnly(filterName, page, scopedParams, out itemBinding);
scopeParams.Each((key, val) => scope.ScopedParams[key] = val);
return scopeParams;
}
public static Dictionary<string, object> GetParamsWithItemBindingOnly(this ScriptScopeContext scope, string filterName, SharpPage page, object scopedParams, out string itemBinding)
{
var pageParams = scope.AssertOptions(filterName, scopedParams);
itemBinding = pageParams.TryGetValue("it", out object bindingName) && bindingName is string binding
? binding
: "it";
if (bindingName != null && !(bindingName is string))
throw new NotSupportedException($"'it' option in filter '{filterName}' should contain the name to bind to but contained a '{bindingName.GetType().Name}' instead");
// page vars take precedence
if (page != null && page.Args.TryGetValue("it", out object pageBinding))
itemBinding = (string)pageBinding;
return pageParams;
}
public static ScriptScopeContext AddItemToScope(this ScriptScopeContext scope, string itemBinding, object item, int index)
{
scope.ScopedParams[ScriptConstants.Index] = index;
return SetItemInScope(itemBinding, item, scope);
}
public static ScriptScopeContext AddItemToScope(this ScriptScopeContext scope, string itemBinding, object item)
{
return SetItemInScope(itemBinding, item, scope);
}
private static ScriptScopeContext SetItemInScope(string itemBinding, object item, ScriptScopeContext newScope)
{
newScope.ScopedParams[itemBinding] = item;
if (item is ScopeVars explodeBindings)
{
foreach (var entry in explodeBindings)
{
newScope.ScopedParams[entry.Key] = entry.Value;
}
}
return newScope;
}
public static T GetValueOrEvaluateBinding<T>(this ScriptScopeContext scope, object valueOrBinding) =>
(T)GetValueOrEvaluateBinding(scope, valueOrBinding, typeof(T));
public static object GetValueOrEvaluateBinding(this ScriptScopeContext scope, object valueOrBinding, Type returnType)
{
if (valueOrBinding is string literal)
{
literal.ParseJsExpression(out var token);
var oValue = token.Evaluate(scope);
return oValue.ConvertTo(returnType);
}
return valueOrBinding.ConvertTo(returnType);
}
public static bool TryGetPage(this ScriptScopeContext scope, string virtualPath, out SharpPage page, out SharpCodePage codePage)
{
if (scope.PageResult.Partials.TryGetValue(virtualPath, out page))
{
codePage = null;
return true;
}
if (!scope.Context.TryGetPage(scope.PageResult.VirtualPath, virtualPath, out page, out codePage))
return false;
codePage?.Init();
if (codePage is IRequiresRequest requiresRequest)
{
if (scope.GetValue(ScriptConstants.Request) is IRequest request)
requiresRequest.Request = request;
}
return true;
}
public static ScriptContext CreateNewContext(this ScriptScopeContext scope, Dictionary<string, object> args)
{
if (args == null)
return new ScriptContext().Init();
var context = new ScriptContext();
if (args.TryGetValue("use", out var oUse))
{
var use = (Dictionary<string, object>) oUse;
if (use.TryGetValue("context", out var oContext) && oContext is bool useContext && useContext)
{
return scope.Context;
}
// Use same ThreadSafe plugin instance to preserve configuration
var plugins = use.TryGetValue("plugins", out var oPlugins)
? ViewUtils.ToStrings("plugins", oPlugins)
: null;
if (plugins != null)
{
foreach (var name in plugins)
{
var plugin = scope.Context.Plugins.FirstOrDefault(x => x.GetType().Name == name);
if (plugin == null)
throw new NotSupportedException($"Plugin '{name}' is not registered in parent context");
context.Plugins.Add(plugin);
}
}
// Use new filter and block instances which cannot be shared between contexts
var methods = use.TryGetValue("methods", out var oMethods)
? ViewUtils.ToStrings("methods", oMethods)
: use.TryGetValue("filters", out var oFilters)
? ViewUtils.ToStrings("filters", oFilters)
: null;
if (methods != null)
{
foreach (var name in methods)
{
var filter = scope.Context.ScriptMethods.FirstOrDefault(x => x.GetType().Name == name);
if (filter == null)
throw new NotSupportedException($"Filter '{name}' is not registered in parent context");
context.ScriptMethods.Add(filter.GetType().CreateInstance<ScriptMethods>());
}
}
var blocks = use.TryGetValue("blocks", out var oBlocks)
? ViewUtils.ToStrings("blocks", oBlocks)
: null;
if (blocks != null)
{
foreach (var name in blocks)
{
var useBlock = scope.Context.ScriptBlocks.FirstOrDefault(x => x.GetType().Name == name);
if (useBlock == null)
throw new NotSupportedException($"Block '{name}' is not registered in parent context");
context.ScriptBlocks.Add(useBlock.GetType().CreateInstance<ScriptBlock>());
}
}
args.Remove(nameof(use));
}
context.Init();
return context;
}
}
}