public override void ConvertToJavascript(JavascriptConversionContext context) { var methodCallExpression = context.Node as MethodCallExpression; if (methodCallExpression?.Method.Name != "Load") { return; } if (methodCallExpression.Method.DeclaringType != typeof(RavenQuery) && (methodCallExpression.Object == null || methodCallExpression.Object.Type != typeof(IDocumentSession))) { return; } HasLoad = true; Arg = methodCallExpression.Arguments[0]; IsEnumerable = Arg.Type.IsArray || LinqMethodsSupport.IsCollection(Arg.Type); if (IsEnumerable && methodCallExpression.Object?.Type == typeof(IDocumentSession)) { throw new NotSupportedException("Using IDocumentSession.Load(IEnumerable<string> ids) inside a query is not supported. " + "You should use RavenQuery.Load(IEnumerable<string> ids) instead"); } context.PreventDefault(); if (DoNotTranslate) { return; } var writer = context.GetWriter(); using (writer.Operation(methodCallExpression)) { writer.Write("load("); context.Visitor.Visit(Arg); writer.Write(")"); } }
public override void ConvertToJavascript(JavascriptConversionContext context) { if (!(context.Node is ConstantExpression nodeAsConst)) { return; } var writer = context.GetWriter(); using (writer.Operation(nodeAsConst)) { if (nodeAsConst.Value == null) { context.PreventDefault(); writer.Write("null"); return; } if (nodeAsConst.Type == typeof(bool)) { context.PreventDefault(); var val = (bool)nodeAsConst.Value ? "true" : "false"; using (writer.Operation(nodeAsConst)) { writer.Write(val); } return; } if (nodeAsConst.Type == typeof(char)) { context.PreventDefault(); writer.Write("\""); writer.Write(nodeAsConst.Value); writer.Write("\""); return; } if (nodeAsConst.Type.IsArray || LinqMethodsSupport.IsCollection(nodeAsConst.Type)) { var arr = nodeAsConst.Value as object[] ?? (nodeAsConst.Value as IEnumerable <object>)?.ToArray(); if (arr == null) { return; } context.PreventDefault(); writer.Write("["); for (var i = 0; i < arr.Length; i++) { if (i != 0) { writer.Write(", "); } if (arr[i] is string || arr[i] is char) { writer.Write("\""); writer.Write(arr[i]); writer.Write("\""); } else { writer.Write(arr[i]); } } writer.Write("]"); } } }