Exemplo n.º 1
0
        public void BaselineTest()
        {
            foreach (byte[] test in new[]
            {
                Properties.Resources.BaselineTest_PathParser,
                Properties.Resources.BaselineTest_PathParser_Extra,
            })
            {
                using (Stream stream = new MemoryStream(test))
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        List <TestData> testData = JsonConvert.DeserializeObject <List <TestData> >(reader.ReadToEnd());

                        foreach (TestData data in testData)
                        {
                            Assert.IsTrue(Enumerable.SequenceEqual(PathParser.GetPathParts(data.Path), data.Parts));
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        internal static PartitionKeyInternal ExtractPartitionKeyValue <T>(T data, PartitionKeyDefinition partitionKeyDefinition, Func <T, JToken> convertToJToken)
        {
            return(PartitionKeyInternal.FromObjectArray(
                       partitionKeyDefinition.Paths.Select(path =>
            {
                string[] parts = PathParser.GetPathParts(path);
                Debug.Assert(parts.Length >= 1, "Partition key component definition path is invalid.");

                JToken token = convertToJToken(data);

                foreach (string part in parts)
                {
                    if (token == null)
                    {
                        break;
                    }

                    token = token[part];
                }

                return token != null ? token.ToObject <object>() : Undefined.Value;
            }).ToArray(),
                       false));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Extracts effective <see cref="PartitionKeyInternal"/> from deserialized document.
        /// </summary>
        /// <remarks>
        /// This code doesn't do any validation, as it assumes that IndexingPolicy is valid, as it is coming from the backend.
        /// Expected format is "/prop1/prop2/?". No array expressions are expected.
        /// </remarks>
        /// <param name="document">Deserialized document to extract partition key value from.</param>
        /// <param name="partitionKeyDefinition">Information about partition key.</param>
        /// <returns>Instance of <see cref="PartitionKeyInternal"/>.</returns>
        public static PartitionKeyInternal ExtractPartitionKeyValue(Document document, PartitionKeyDefinition partitionKeyDefinition)
        {
            if (partitionKeyDefinition == null || partitionKeyDefinition.Paths.Count == 0)
            {
                return(PartitionKeyInternal.Empty);
            }

            if (document.GetType().IsSubclassOf(typeof(Document)))
            {
                return(DocumentAnalyzer.ExtractPartitionKeyValue(document, partitionKeyDefinition, (doc) => JToken.FromObject(doc)));
            }
            else
            {
                return(PartitionKeyInternal.FromObjectArray(
                           partitionKeyDefinition.Paths.Select(path =>
                {
                    string[] parts = PathParser.GetPathParts(path);
                    Debug.Assert(parts.Length >= 1, "Partition key component definition path is invalid.");

                    return document.GetValueByPath <object>(parts, Undefined.Value);
                }).ToArray(),
                           false));
            }
        }