public static bool MatchBindingPath(IEdmPathExpression bindingPath, IList <ODataSegmentTemplate> parsedSegments)
        {
            List <string> paths = bindingPath.PathSegments.ToList();

            // If binding path only includes navigation property name, it matches.
            if (paths.Count == 1)
            {
                return(true);
            }

            int pathIndex = paths.Count - 2; // Skip the last segment which is navigation property name.

            // Match from tail to head.
            for (int segmentIndex = parsedSegments.Count - 1; segmentIndex >= 0; segmentIndex--)
            {
                ODataSegmentTemplate segment = parsedSegments[segmentIndex];

                PropertySegmentTemplate   propertySegment   = segment as PropertySegmentTemplate;
                NavigationSegmentTemplate navigationSegment = segment as NavigationSegmentTemplate;
                // Containment navigation property or complex property in binding path.
                if (propertySegment != null ||
                    (navigationSegment != null && navigationSegment.Segment.NavigationSource is IEdmContainedEntitySet))
                {
                    string pathName = propertySegment != null ?
                                      propertySegment.Property.Name :
                                      navigationSegment.NavigationProperty.Name;

                    if (pathIndex < 0 || string.CompareOrdinal(paths[pathIndex], pathName) != 0)
                    {
                        return(false);
                    }

                    pathIndex--;
                }
                else if (segment is CastSegmentTemplate)
                {
                    CastSegmentTemplate cast = (CastSegmentTemplate)segment;
                    // May need match type if the binding path contains type cast.
                    if (pathIndex >= 0 && paths[pathIndex].Contains(".", System.StringComparison.Ordinal))
                    {
                        if (string.CompareOrdinal(paths[pathIndex], cast.CastType.AsElementType().FullTypeName()) != 0)
                        {
                            return(false);
                        }

                        pathIndex--;
                    }
                }
                else if (segment is EntitySetSegmentTemplate ||
                         segment is SingletonSegmentTemplate ||
                         navigationSegment != null)
                {
                    // Containment navigation property in first if statement for NavigationPropertySegment.
                    break;
                }
            }

            // Return true if all the segments in binding path have been matched.
            return(pathIndex == -1 ? true : false);
        }
        public void TryTranslateCastSegmentTemplate_ThrowsArgumentNull_Context()
        {
            // Arrange
            EdmEntityType       baseType = new EdmEntityType("NS", "base");
            EdmEntityType       subType  = new EdmEntityType("NS", "sub", baseType);
            CastSegmentTemplate segment  = new CastSegmentTemplate(subType, baseType, null);

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(() => segment.TryTranslate(null), "context");
        }
        public void GetTemplatesCastSegmentTemplate_ReturnsTemplates()
        {
            // Assert
            EdmEntityType       baseType = new EdmEntityType("NS", "base");
            EdmEntityType       subType  = new EdmEntityType("NS", "sub", baseType);
            CastSegmentTemplate segment  = new CastSegmentTemplate(subType, baseType, null);

            // Act & Assert
            IEnumerable <string> templates = segment.GetTemplates();
            string template = Assert.Single(templates);

            Assert.Equal("/NS.sub", template);
        }
Example #4
0
        public void CommonCastProperties_ReturnsAsExpected()
        {
            // Assert
            EdmEntityType       baseType = new EdmEntityType("NS", "base");
            EdmEntityType       subType  = new EdmEntityType("NS", "sub", baseType);
            CastSegmentTemplate template = new CastSegmentTemplate(subType, baseType, null);

            // Act & Assert
            Assert.Equal(ODataSegmentKind.Cast, template.Kind);
            Assert.Equal("NS.sub", template.Literal);
            Assert.True(template.IsSingle);
            Assert.Same(subType, template.EdmType);
            Assert.Null(template.NavigationSource);
        }
        public void CtorCastSegmentTemplate_SetsProperties()
        {
            // Arrange & Act
            EdmEntityType       baseType = new EdmEntityType("NS", "base");
            EdmEntityType       subType  = new EdmEntityType("NS", "sub", baseType);
            CastSegmentTemplate segment  = new CastSegmentTemplate(subType, baseType, null);

            // Assert
            Assert.Same(subType, segment.CastType);
            Assert.Same(baseType, segment.ExpectedType);
            Assert.NotNull(segment.TypeSegment);

            // Act & Assert
            CastSegmentTemplate segment1 = new CastSegmentTemplate(segment.TypeSegment);

            Assert.Same(segment.TypeSegment, segment1.TypeSegment);
        }
        public void TranslateCastSegmentTemplate_ReturnsODataTypeSegment()
        {
            // Arrange
            EdmEntityType                 baseType = new EdmEntityType("NS", "base");
            EdmEntityType                 subType  = new EdmEntityType("NS", "sub", baseType);
            CastSegmentTemplate           template = new CastSegmentTemplate(subType, baseType, null);
            ODataTemplateTranslateContext context  = new ODataTemplateTranslateContext();

            // Act
            ODataPathSegment actual = template.Translate(context);

            // Assert
            Assert.NotNull(actual);
            TypeSegment typeSegment = Assert.IsType <TypeSegment>(actual);

            Assert.Same(subType, typeSegment.EdmType);
        }
Example #7
0
 public static ODataTypeCastSegment ConvertTo(this CastSegmentTemplate cast)
 {
     // So far, only support the entity type cast
     return(new ODataTypeCastSegment(cast.ExpectedType as IEdmEntityType));
 }
Example #8
0
        public static ODataPath Translate(this ODataPathTemplate pathTemplate)
        {
            if (pathTemplate.Count == 0)
            {
                // It's service root, so far, let's skip it.
                return(null);
            }

            IList <ODataSegment> newSegments = new List <ODataSegment>();

            foreach (var segment in pathTemplate)
            {
                switch (segment.Kind)
                {
                case ODataSegmentKind.Metadata:
                    newSegments.Add(new ODataMetadataSegment());
                    break;

                case ODataSegmentKind.EntitySet:
                    EntitySetSegmentTemplate entitySet = (EntitySetSegmentTemplate)segment;
                    newSegments.Add(entitySet.ConvertTo());
                    break;

                case ODataSegmentKind.Singleton:
                    SingletonSegmentTemplate singleton = (SingletonSegmentTemplate)segment;
                    newSegments.Add(singleton.ConvertTo());
                    break;

                case ODataSegmentKind.Key:
                    KeySegmentTemplate key = (KeySegmentTemplate)segment;
                    newSegments.Add(key.ConvertTo());
                    break;

                case ODataSegmentKind.Cast:
                    CastSegmentTemplate cast = (CastSegmentTemplate)segment;
                    newSegments.Add(cast.ConvertTo());
                    break;

                case ODataSegmentKind.Property:
                    // TODO:
                    return(null);

                //PropertySegmentTemplate property = (PropertySegmentTemplate)segment;
                //newSegments.Add(property.ConvertTo());
                //break;

                case ODataSegmentKind.Navigation:
                    NavigationSegmentTemplate navigation = (NavigationSegmentTemplate)segment;
                    newSegments.Add(navigation.ConvertTo());
                    break;

                case ODataSegmentKind.Function:
                    FunctionSegmentTemplate function = (FunctionSegmentTemplate)segment;
                    newSegments.Add(function.ConvertTo());
                    break;

                case ODataSegmentKind.Action:
                    ActionSegmentTemplate action = (ActionSegmentTemplate)segment;
                    newSegments.Add(action.ConvertTo());
                    break;

                case ODataSegmentKind.FunctionImport:
                    FunctionImportSegmentTemplate functionImport = (FunctionImportSegmentTemplate)segment;
                    newSegments.Add(functionImport.ConvertTo());
                    break;

                case ODataSegmentKind.ActionImport:
                    ActionImportSegmentTemplate actionImport = (ActionImportSegmentTemplate)segment;
                    newSegments.Add(actionImport.ConvertTo());
                    break;

                case ODataSegmentKind.Value:
                    return(null);

                //ValueSegmentTemplate value = (ValueSegmentTemplate)segment;
                //newSegments.Add(value.ConvertTo());
                //break;

                case ODataSegmentKind.Ref:
                    return(null);

                //KeySegmentTemplate key = (KeySegmentTemplate)segment;
                //newSegments.Add(key.ConvertTo());
                //break;

                case ODataSegmentKind.NavigationLink:
                    return(null);

                //NavigationLinkSegmentTemplate navigationLink = (NavigationLinkSegmentTemplate)segment;
                //newSegments.Add(navigationLink.ConvertTo());
                //break;

                case ODataSegmentKind.Count:
                    CountSegmentTemplate count = (CountSegmentTemplate)segment;
                    newSegments.Add(count.ConvertTo());
                    break;

                case ODataSegmentKind.PathTemplate:
                    return(null);

                //KeySegmentTemplate key = (KeySegmentTemplate)segment;
                //newSegments.Add(key.ConvertTo());
                //break;

                case ODataSegmentKind.Dynamic:
                    return(null);

                //KeySegmentTemplate key = (KeySegmentTemplate)segment;
                //newSegments.Add(key.ConvertTo());
                //break;

                default:
                    throw new NotSupportedException();
                }
            }

            return(new ODataPath(newSegments));
        }