/// <summary>
        /// Initializes a new instance of the <see cref="KeyValuePathSegmentTemplate"/> class.
        /// </summary>
        /// <param name="keyValueSegment">The key value segment to be parsed as a template.</param>
        public KeyValuePathSegmentTemplate(KeyValuePathSegment keyValueSegment)
        {
            if (keyValueSegment == null)
            {
                throw Error.ArgumentNull("keyValueSegment");
            }

            ParameterMappings = BuildParameterMappings(keyValueSegment.Values, keyValueSegment.Value);
        }
        public void TryMatch_SingleKey_ReturnsTrueOnMatch()
        {
            KeyValuePathSegmentTemplate template = new KeyValuePathSegmentTemplate(new KeyValuePathSegment("{ID}"));
            KeyValuePathSegment segment = new KeyValuePathSegment("123");

            // Act
            Dictionary<string, object> values = new Dictionary<string, object>();
            bool result = template.TryMatch(segment, values);

            // Assert
            Assert.True(result);
            Assert.Equal("123", values["ID"]);
        }
        public void TryMatch_DifferentType()
        {
            // Arrange
            DynamicPropertyPathSegment leftSegment = new DynamicPropertyPathSegment("property");
            KeyValuePathSegment rightSegment = new KeyValuePathSegment("value");

            // Act
            Dictionary<string, object> values = new Dictionary<string, object>();
            bool result = leftSegment.TryMatch(rightSegment, values);

            // Assert
            Assert.False(result);
        }
        public void TryMatch_MultiKey_ReturnsTrueOnMatch()
        {
            KeyValuePathSegmentTemplate template = new KeyValuePathSegmentTemplate(new KeyValuePathSegment("FirstName={key1},LastName={key2}"));
            KeyValuePathSegment segment = new KeyValuePathSegment("FirstName=abc,LastName=xyz");

            // Act
            Dictionary<string, object> values = new Dictionary<string, object>();
            bool result = template.TryMatch(segment, values);

            // Assert
            Assert.True(result);
            Assert.Equal("abc", values["key1"]);
            Assert.Equal("xyz", values["key2"]);
        }
Exemplo n.º 5
0
        public void Translate_KeySegment_To_KeyValuePathSegment_Works()
        {
            // Arrange
            IEdmEntityType entityType = _model.FindDeclaredType("System.Web.OData.Routing.RoutingCustomer") as IEdmEntityType;
            IEdmEntitySet  entityset  = _model.FindDeclaredEntitySet("RoutingCustomers");
            KeySegment     segment    = new KeySegment(new[] { new KeyValuePair <string, object>("ID", 42) }, entityType, entityset);

            // Act
            IEnumerable <ODataPathSegment> segments = _translator.Translate(segment);

            // Assert
            ODataPathSegment    pathSegment         = Assert.Single(segments);
            KeyValuePathSegment keyValuePathSegment = Assert.IsType <KeyValuePathSegment>(pathSegment);

            Assert.Equal("42", keyValuePathSegment.Value);
        }
        /// <inheritdoc />
        public override bool TryMatch(ODataPathSegment pathSegment, IDictionary <string, object> values)
        {
            if (pathSegment.SegmentKind == ODataSegmentKinds.Key)
            {
                KeyValuePathSegment keySegment = (KeyValuePathSegment)pathSegment;

                if (keySegment.Segment != null)
                {
                    return(keySegment.Segment.TryMatch(ParameterMappings, values));
                }

                return(TryMatch(ParameterMappings, keySegment.Values, values, null));
            }

            return(false);
        }
        internal static IDictionary <string, string> BuildKeyMappings(KeyValuePathSegment keyValuePathSegment)
        {
            Contract.Assert(keyValuePathSegment != null);

            if (keyValuePathSegment.Segment != null)
            {
                Dictionary <string, string> parameterMappings = new Dictionary <string, string>();
                foreach (var key in keyValuePathSegment.Segment.Keys)
                {
                    string parameterNameInRouteData             = null;
                    UriTemplateExpression uriTemplateExpression = key.Value as UriTemplateExpression;
                    if (uriTemplateExpression != null)
                    {
                        parameterNameInRouteData = uriTemplateExpression.LiteralText;
                    }

                    if (String.IsNullOrEmpty(parameterNameInRouteData))
                    {
                        parameterNameInRouteData = key.Key;
                    }
                    else if (IsRouteParameter(parameterNameInRouteData))
                    {
                        parameterNameInRouteData = parameterNameInRouteData.Substring(1, parameterNameInRouteData.Length - 2);
                        if (String.IsNullOrEmpty(parameterNameInRouteData))
                        {
                            throw new ODataException(
                                      Error.Format(SRResources.EmptyKeyTemplate, keyValuePathSegment.Value));
                        }
                    }
                    else
                    {
                        throw new ODataException(
                                  Error.Format(SRResources.KeyTemplateMustBeInCurlyBraces, keyValuePathSegment.Value));
                    }

                    parameterMappings[key.Key] = parameterNameInRouteData;
                }

                return(parameterMappings);
            }

            return(BuildParameterMappings(keyValuePathSegment.Values, keyValuePathSegment.Value));
        }
Exemplo n.º 8
0
        private static IReadOnlyDictionary<string, object> GetPathKeyValues(
            KeyValuePathSegment keySegment,
            IEdmEntityType entityType)
        {
            var result = new Dictionary<string, object>();
            var keys = entityType.Key();

            // TODO GitHubIssue#42 : Improve key parsing logic
            // this parsing implementation does not allow key values to contain commas
            // Depending on the WebAPI to make KeyValuePathSegment.Values collection public
            // (or have the parsing logic public).
            string[] values = keySegment.Value.Split(EntityKeySeparator);
            if (values.Length > 1)
            {
                foreach (string value in values)
                {
                    // Split key name and key value
                    var keyValues = value.Split(EntityKeyNameValueSeparator);
                    if (keyValues.Length != 2)
                    {
                        throw new InvalidOperationException(Resources.IncorrectKeyFormat);
                    }

                    // Validate the key name
                    if (!keys.Select(k => k.Name).Contains(keyValues[0]))
                    {
                        throw new InvalidOperationException(
                            string.Format(
                            CultureInfo.InvariantCulture,
                            Resources.KeyNotValidForEntityType,
                            keyValues[0],
                            entityType.Name));
                    }

                    result.Add(keyValues[0], ODataUriUtils.ConvertFromUriLiteral(keyValues[1], ODataVersion.V4));
                }
            }
            else
            {
                // We just have the single key value
                // Validate it has exactly one key
                if (keys.Count() > 1)
                {
                    throw new InvalidOperationException(Resources.MultiKeyValuesExpected);
                }

                var keyName = keys.First().Name;
                result.Add(keyName, ODataUriUtils.ConvertFromUriLiteral(keySegment.Value, ODataVersion.V4));
            }

            return result;
        }
        internal static IDictionary<string, string> BuildKeyMappings(KeyValuePathSegment keyValuePathSegment)
        {
            Contract.Assert(keyValuePathSegment != null);

            if (keyValuePathSegment.Segment != null)
            {
                Dictionary<string, string> parameterMappings = new Dictionary<string, string>();
                foreach (var key in keyValuePathSegment.Segment.Keys)
                {
                    string parameterNameInRouteData = null;
                    UriTemplateExpression uriTemplateExpression = key.Value as UriTemplateExpression;
                    if (uriTemplateExpression != null)
                    {
                        parameterNameInRouteData = uriTemplateExpression.LiteralText;
                    }

                    if (String.IsNullOrEmpty(parameterNameInRouteData))
                    {
                        parameterNameInRouteData = key.Key;
                    }
                    else if (IsRouteParameter(parameterNameInRouteData))
                    {
                        parameterNameInRouteData = parameterNameInRouteData.Substring(1, parameterNameInRouteData.Length - 2);
                        if (String.IsNullOrEmpty(parameterNameInRouteData))
                        {
                            throw new ODataException(
                                Error.Format(SRResources.EmptyKeyTemplate, keyValuePathSegment.Value));
                        }
                    }
                    else
                    {
                        throw new ODataException(
                            Error.Format(SRResources.KeyTemplateMustBeInCurlyBraces, keyValuePathSegment.Value));
                    }

                    parameterMappings[key.Key] = parameterNameInRouteData;
                }

                return parameterMappings;
            }

            return BuildParameterMappings(keyValuePathSegment.Values, keyValuePathSegment.Value);
        }