/// <summary> /// Analyzes the specified <paramref name="lambda"/> for selection and updates /// <paramref name="resource"/>. /// </summary> /// <param name="lambda">Lambda expression to analyze.</param> /// <param name="resource">Resource expression to update.</param> /// <param name="context">Context of expression to analyze.</param> private static void AnalyzeResourceExpression(LambdaExpression lambda, ResourceExpression resource, DataServiceContext context) { SelectExpandPathBuilder pb = new SelectExpandPathBuilder(); ProjectionAnalyzer.Analyze(lambda, pb, context); resource.Projection = new ProjectionQueryOptionExpression(lambda.Body.Type, lambda, pb.ProjectionPaths.ToList()); resource.ExpandPaths = pb.ExpandPaths.Union(resource.ExpandPaths, StringComparer.Ordinal).ToList(); resource.RaiseUriVersion(pb.UriVersion); }
/// <summary>Initializes a new <see cref="EntityProjectionAnalyzer"/> instance.</summary> /// <param name="pb">Path-tracking object.</param> /// <param name="type">Type being member-init'ed.</param> /// <param name="context">Context of expression to analyze.</param> private EntityProjectionAnalyzer(SelectExpandPathBuilder pb, Type type, DataServiceContext context) { Debug.Assert(pb != null, "pb != null"); Debug.Assert(type != null, "type != null"); this.builder = pb; this.type = type; this.context = context; }
public void SingleExpansionBecomesExpand() { SelectExpandPathBuilder pathBuilder = new SelectExpandPathBuilder(); ParameterExpression pe = Expression.Parameter((typeof(TestEntity))); pathBuilder.PushParamExpression(pe); PropertyInfo navPropPropertyInfo = typeof(TestEntity).GetProperties().Single(x => x.PropertyType == typeof(SubTestEntity1)); pathBuilder.StartNewPath(); pathBuilder.AppendPropertyToPath(navPropPropertyInfo, null, dsc); pathBuilder.ExpandPaths.Single().Should().Be("NavProp1"); }
public void SingleProjectionBecomesSelect() { PropertyInfo testProperty1Info = typeof(TestEntity).GetProperties().Single(x => x.Name == "TestProperty1"); SelectExpandPathBuilder pathBuilder = new SelectExpandPathBuilder(); ParameterExpression pe = Expression.Parameter(typeof(TestEntity)); pathBuilder.PushParamExpression(pe); pathBuilder.StartNewPath(); pathBuilder.AppendPropertyToPath(testProperty1Info, null, this.dsc); pathBuilder.ProjectionPaths.Single().Should().Be("TestProperty1"); }
public void MultipleSingleLevelExpansionsBecomeExpandClauses() { SelectExpandPathBuilder pathBuilder = new SelectExpandPathBuilder(); ParameterExpression pe = Expression.Parameter(typeof(TestEntity)); pathBuilder.PushParamExpression(pe); foreach (PropertyInfo propertyInfo in typeof(TestEntity).GetProperties().Where(x => x.PropertyType != typeof(string))) { pathBuilder.StartNewPath(); pathBuilder.AppendPropertyToPath(propertyInfo, null, dsc); } pathBuilder.ExpandPaths.Should().HaveCount(2); pathBuilder.ExpandPaths.Should().Contain(x => x == "NavProp1") .And.Contain(x => x == "NavProp2"); }
/// <summary> /// Analyzes the specified expression with an entity-projection or /// non-entity-projection analyzer. /// </summary> /// <param name="mie">Expression to analyze.</param> /// <param name="pb">Path box where select and expand paths are tracked.</param> /// <param name="context">Context of expression to analyze.</param> private static void Analyze(MemberInitExpression mie, SelectExpandPathBuilder pb, DataServiceContext context) { Debug.Assert(mie != null, "mie != null"); Debug.Assert(pb != null, "pb != null"); bool knownEntityType = ClientTypeUtil.TypeOrElementTypeIsEntity(mie.Type); if (knownEntityType) { EntityProjectionAnalyzer.Analyze(mie, pb, context); } else { NonEntityProjectionAnalyzer.Analyze(mie, pb, context); } }
private static void Analyze(LambdaExpression e, SelectExpandPathBuilder pb, DataServiceContext context) { bool knownEntityType = ClientTypeUtil.TypeOrElementTypeIsEntity(e.Body.Type); ParameterExpression pe = e.Parameters.Last(); bool isEntityParameter = ClientTypeUtil.TypeOrElementTypeIsEntity(pe.Type); if (isEntityParameter) { pb.PushParamExpression(pe); } if (!knownEntityType) { NonEntityProjectionAnalyzer.Analyze(e.Body, pb, context); } else { switch (e.Body.NodeType) { case ExpressionType.MemberInit: EntityProjectionAnalyzer.Analyze((MemberInitExpression)e.Body, pb, context); break; case ExpressionType.New: throw new NotSupportedException(Strings.ALinq_CannotConstructKnownEntityTypes); case ExpressionType.Constant: throw new NotSupportedException(Strings.ALinq_CannotCreateConstantEntity); default: // ExpressionType.MemberAccess as a top-level expression is correctly // processed here, as the lambda isn't being member-initialized. NonEntityProjectionAnalyzer.Analyze(e.Body, pb, context); break; } } if (isEntityParameter) { pb.PopParamExpression(); } }
internal static void Analyze(Expression e, SelectExpandPathBuilder pb, DataServiceContext context) { var nepa = new NonEntityProjectionAnalyzer(pb, e.Type, context); MemberInitExpression mie = e as MemberInitExpression; if (mie != null) { foreach (MemberBinding mb in mie.Bindings) { MemberAssignment ma = mb as MemberAssignment; if (ma != null) { nepa.Visit(ma.Expression); } } } else { nepa.Visit(e); } }
private static void Analyze(LambdaExpression e, SelectExpandPathBuilder pb, DataServiceContext context) { bool knownEntityType = ClientTypeUtil.TypeOrElementTypeIsEntity(e.Body.Type); ParameterExpression pe = e.Parameters.Last(); bool isEntityParameter = ClientTypeUtil.TypeOrElementTypeIsEntity(pe.Type); if(isEntityParameter) { pb.PushParamExpression(pe); } if (!knownEntityType) { NonEntityProjectionAnalyzer.Analyze(e.Body, pb, context); } else { switch (e.Body.NodeType) { case ExpressionType.MemberInit: EntityProjectionAnalyzer.Analyze((MemberInitExpression)e.Body, pb, context); break; case ExpressionType.New: throw new NotSupportedException(Strings.ALinq_CannotConstructKnownEntityTypes); case ExpressionType.Constant: throw new NotSupportedException(Strings.ALinq_CannotCreateConstantEntity); default: // ExpressionType.MemberAccess as a top-level expression is correctly // processed here, as the lambda isn't being member-initialized. NonEntityProjectionAnalyzer.Analyze(e.Body, pb, context); break; } } if (isEntityParameter) { pb.PopParamExpression(); } }
public void PushingANewParameterExpressionResetsTheBasePath() { SelectExpandPathBuilder pathBuilder = new SelectExpandPathBuilder(); ParameterExpression pe1 = Expression.Parameter(typeof(TestEntity), "pe1"); ParameterExpression pe2 = Expression.Parameter(typeof(SubTestEntity1), "pe2"); PropertyInfo navPropInfo = typeof(TestEntity).GetProperties().Single(x => x.Name == "NavProp1"); PropertyInfo subNavPropInfo = typeof(SubTestEntity1).GetProperties().Single(x => x.Name == "NavProp3"); PropertyInfo subTestProperty = typeof(SubTestEntity2).GetProperties().Single(); pathBuilder.PushParamExpression(pe1); pathBuilder.StartNewPath(); pathBuilder.AppendPropertyToPath(navPropInfo, null, dsc); pathBuilder.AppendPropertyToPath(subNavPropInfo, null, dsc); pathBuilder.PushParamExpression(pe2); pathBuilder.StartNewPath(); pathBuilder.AppendPropertyToPath(subTestProperty, null, dsc); pathBuilder.ExpandPaths.Single().Should().Be("NavProp1($expand=NavProp3($select=SubTestProperty))"); }
public void TypeIsPrePendedAsATypeSegment() { SelectExpandPathBuilder pathBuilder = new SelectExpandPathBuilder(); ParameterExpression pe = Expression.Parameter(typeof(TestEntity)); PropertyInfo testProperty1Info = typeof(TestEntity).GetProperties().Single(x => x.Name == "TestProperty1"); pathBuilder.PushParamExpression(pe); pathBuilder.StartNewPath(); pathBuilder.AppendPropertyToPath(testProperty1Info, typeof(TestEntity), dsc); pathBuilder.ProjectionPaths.Single().Should().Be("AstoriaUnitTests.Tests.ALinq.SelectExpandPathBuilderTests+TestEntity/TestProperty1"); }
private NonEntityProjectionAnalyzer(SelectExpandPathBuilder pb, Type type, DataServiceContext context) { this.builder = pb; this.type = type; this.context = context; }
public void SelectingLowerLevelPropertyIsTranslatedIntoExpandOption() { SelectExpandPathBuilder pathBuilder = new SelectExpandPathBuilder(); ParameterExpression pe = Expression.Parameter(typeof(TestEntity)); PropertyInfo navPropInfo = typeof(TestEntity).GetProperties().Single(x => x.Name == "NavProp1"); PropertyInfo subNavPropInfo = typeof(SubTestEntity1).GetProperties().Single(x => x.Name == "NavProp3"); PropertyInfo subTestProperty = typeof(SubTestEntity2).GetProperties().Single(); pathBuilder.PushParamExpression(pe); pathBuilder.StartNewPath(); pathBuilder.AppendPropertyToPath(navPropInfo, null, dsc); pathBuilder.AppendPropertyToPath(subNavPropInfo, null, dsc); pathBuilder.AppendPropertyToPath(subTestProperty, null, dsc); pathBuilder.ExpandPaths.Single().Should().Be("NavProp1($expand=NavProp3($select=SubTestProperty))"); }
/// <summary>Analyzes the specified member-init expression.</summary> /// <param name="mie">Expression to analyze.</param> /// <param name="pb">Path-tracking object to store analysis in.</param> /// <param name="context">Context of expression to analyze.</param> internal static void Analyze(MemberInitExpression mie, SelectExpandPathBuilder pb, DataServiceContext context) { Debug.Assert(mie != null, "mie != null"); var epa = new EntityProjectionAnalyzer(pb, mie.Type, context); MemberAssignmentAnalysis targetEntityPath = null; foreach (MemberBinding mb in mie.Bindings) { MemberAssignment ma = mb as MemberAssignment; epa.Visit(ma.Expression); if (ma != null) { var analysis = MemberAssignmentAnalysis.Analyze(pb.ParamExpressionInScope, ma.Expression); if (analysis.IncompatibleAssignmentsException != null) { throw analysis.IncompatibleAssignmentsException; } // Note that an "empty" assignment on the binding is not checked/handled, // because the funcletizer would have turned that into a constant // in the tree, the visit earlier in this method would have thrown // an exception at finding a constant in an entity projection. // // We do account however for failing to find a reference off the // parameter entry to detect errors like this: new ET() { Ref = e } // Here it looks like the new ET should be the parent of 'e', but // there is nothing in scope that represents that. // // This also explains while error messages might be a bit misleading // in this case (because they reference a constant when the user // hasn't included any). Type targetType = ClientTypeUtil.GetMemberType(ma.Member); Expression[] lastExpressions = analysis.GetExpressionsBeyondTargetEntity(); if (lastExpressions.Length == 0) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjectionToEntity(targetType, ma.Expression)); } MemberExpression lastExpression = lastExpressions[lastExpressions.Length - 1] as MemberExpression; Debug.Assert( !analysis.MultiplePathsFound, "!analysis.MultiplePathsFound -- the initilizer has been visited, and cannot be empty, and expressions that can combine paths should have thrown exception during initializer analysis"); #if DEBUG Debug.Assert( lastExpression != null, "lastExpression != null -- the initilizer has been visited, and cannot be empty, and the only expressions that are allowed can be formed off the parameter, so this is always correlatd"); #endif analysis.CheckCompatibleAssignments(mie.Type, ref targetEntityPath); // For DataServiceStreamLink, the last expression will be a constant expression. Hence we won't be comparing name checks and entity checks for those type of bindings if (lastExpression != null) { if (lastExpression.Member.Name != ma.Member.Name) { throw new NotSupportedException(Strings.ALinq_PropertyNamesMustMatchInProjections(lastExpression.Member.Name, ma.Member.Name)); } // Unless we're initializing an entity, we should not traverse into the parameter in scope. bool targetIsEntity = ClientTypeUtil.TypeOrElementTypeIsEntity(targetType); bool sourceIsEntity = ClientTypeUtil.TypeOrElementTypeIsEntity(lastExpression.Type); if (sourceIsEntity && !targetIsEntity) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(targetType, ma.Expression)); } } } } }
public void ExpansionThroughMultipleNavPropsIsExpressedAsExpandOptions() { SelectExpandPathBuilder pathBuilder = new SelectExpandPathBuilder(); ParameterExpression pe = Expression.Parameter(typeof(TestEntity)); PropertyInfo navPropInfo = typeof(TestEntity).GetProperties().Single(x => x.Name == "NavProp1"); PropertyInfo subNavPropInfo = typeof(SubTestEntity1).GetProperties().Single(x => x.Name == "NavProp3"); pathBuilder.PushParamExpression(pe); pathBuilder.StartNewPath(); pathBuilder.AppendPropertyToPath(navPropInfo, null, dsc); pathBuilder.AppendPropertyToPath(subNavPropInfo, null, dsc); pathBuilder.ExpandPaths.Single().Should().Be("NavProp1($expand=NavProp3)"); }