< Summary

Class:Azure.Core.Http.Multipart.KeyValueAccumulator
Assembly:Azure.Storage.Blobs.Batch
File(s):C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Blobs.Batch\src\Shared\KeyValueAccumulator.cs
Covered lines:10
Uncovered lines:16
Coverable lines:26
Total lines:91
Line coverage:38.4% (10 of 26)
Covered branches:6
Total branches:18
Branch coverage:33.3% (6 of 18)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
Append(...)-31.58%30%
get_HasValues()-0%100%
get_KeyCount()-100%50%
get_ValueCount()-100%100%
GetResults()-50%33.33%

File(s)

C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Blobs.Batch\src\Shared\KeyValueAccumulator.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4// Copied from https://github.com/aspnet/AspNetCore/tree/master/src/Http/WebUtilities/src
 5
 6using System;
 7using System.Collections.Generic;
 8
 9#pragma warning disable IDE0008 // Use explicit type
 10#pragma warning disable IDE0018 // Inline declaration
 11#pragma warning disable IDE0034 // default can be simplified
 12
 13namespace Azure.Core.Http.Multipart
 14{
 15    internal struct KeyValueAccumulator
 16    {
 17        private Dictionary<string, StringValues> _accumulator;
 18        private Dictionary<string, List<string>> _expandingAccumulator;
 19
 20        public void Append(string key, string value)
 21        {
 254022            if (_accumulator == null)
 23            {
 127224                _accumulator = new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase);
 25            }
 26
 27            StringValues values;
 254028            if (_accumulator.TryGetValue(key, out values))
 29            {
 030                if (values.Count == 0)
 31                {
 32                    // Marker entry for this key to indicate entry already in expanding list dictionary
 033                    _expandingAccumulator[key].Add(value);
 34                }
 035                else if (values.Count == 1)
 36                {
 37                    // Second value for this key
 038                    _accumulator[key] = new string[] { values[0], value };
 39                }
 40                else
 41                {
 42                    // Third value for this key
 43                    // Add zero count entry and move to data to expanding list dictionary
 044                    _accumulator[key] = default(StringValues);
 45
 046                    if (_expandingAccumulator == null)
 47                    {
 048                        _expandingAccumulator = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
 49                    }
 50
 51                    // Already 3 entries so use starting allocated as 8; then use List's expansion mechanism for more
 052                    var list = new List<string>(8);
 053                    var array = values.ToArray();
 54
 055                    list.Add(array[0]);
 056                    list.Add(array[1]);
 057                    list.Add(value);
 58
 059                    _expandingAccumulator[key] = list;
 60                }
 61            }
 62            else
 63            {
 64                // First value for this key
 254065                _accumulator[key] = new StringValues(value);
 66            }
 67
 254068            ValueCount++;
 254069        }
 70
 071        public bool HasValues => ValueCount > 0;
 72
 254073        public int KeyCount => _accumulator?.Count ?? 0;
 74
 508075        public int ValueCount { get; private set; }
 76
 77        public Dictionary<string, StringValues> GetResults()
 78        {
 127279            if (_expandingAccumulator != null)
 80            {
 81                // Coalesce count 3+ multi-value entries into _accumulator dictionary
 082                foreach (var entry in _expandingAccumulator)
 83                {
 084                    _accumulator[entry.Key] = new StringValues(entry.Value.ToArray());
 85                }
 86            }
 87
 127288            return _accumulator ?? new Dictionary<string, StringValues>(0, StringComparer.OrdinalIgnoreCase);
 89        }
 90    }
 91}