Beispiel #1
0
        protected override Extraction ExtractFromJArray(
            JArrayContext jArrayContext)
        {
            var rangeIndexComposition = _rangeIndex.Compose(
                jArrayContext.Input,
                jArrayContext.Path);

            var normalizedRangeIndex = NormalizedArrayRangeIndex.Normalize(
                rangeIndex: rangeIndexComposition,
                arrayLength: jArrayContext.Value.Count);

            var array = normalizedRangeIndex
                        .AllIndexes()
                        .Where(index => index >= 0 && index < jArrayContext.Value.Count)
                        .Aggregate(
                seed: new JArray(),
                (acc, index) =>
            {
                acc.Add(jArrayContext.Value[index]);
                return(acc);
            });

            var path = GetPath(
                jArrayContext.Path);

            return(new CorrectExtraction(
                       path,
                       array));
        }
Beispiel #2
0
 private static List <NormalizedArrayIndex> TryGetOutOfRangeIndexes(
     NormalizedArrayPickIndex normalizedPickIndex,
     JArrayContext jArrayContext)
 {
     return(normalizedPickIndex
            .AllIndexes()
            .Where(index => IsIndexOutOfRange(
                       index.NormalizedValue,
                       jArrayContext))
            .ToList());
 }
Beispiel #3
0
 private IndexOutOfRange IndexesOutOfRange(
     JArrayContext jArrayContext,
     List <NormalizedArrayIndex> outOfRangeIndexes)
 {
     return(new IndexOutOfRange(
                path: GetPath(
                    jArrayContext.Path),
                previousPath: jArrayContext.Path,
                jArray: jArrayContext.Value,
                wrongIndexes: outOfRangeIndexes
                .Select(index => index.OriginalValue)));
 }
Beispiel #4
0
        protected override Extraction ExtractFromJArray(
            JArrayContext jArrayContext)
        {
            var indexes = _pickIndexComposer.Compose(
                jArrayContext.Input,
                jArrayContext.Path);

            var normalizedPickIndex = NormalizedArrayPickIndex.Normalize(
                indexes: indexes,
                arrayLength: jArrayContext.Value.Count);

            var outOfRangeIndexes = TryGetOutOfRangeIndexes(
                normalizedPickIndex,
                jArrayContext);

            if (outOfRangeIndexes.Any())
            {
                return(IndexesOutOfRange(
                           jArrayContext,
                           outOfRangeIndexes));
            }

            var array = normalizedPickIndex
                        .AllIndexes()
                        .Aggregate(
                seed: new JArray(),
                (acc, index) =>
            {
                acc.Add(jArrayContext.Value[index.NormalizedValue]);
                return(acc);
            });

            if (indexes.Length == 1)
            {
                return(new CorrectExtraction(
                           GetPath(
                               jArrayContext.Path),
                           array[0]));
            }

            return(new CorrectExtraction(
                       GetPath(
                           jArrayContext.Path),
                       array));
        }
Beispiel #5
0
 protected abstract Extraction ExtractFromJArray(
     JArrayContext jArrayContext);
Beispiel #6
0
 private static bool IsIndexOutOfRange(
     int index,
     JArrayContext jArrayContext)
 {
     return(index < 0 || index >= jArrayContext.Value.Count);
 }