< Summary

Class:Azure.Data.AppConfiguration.ConfigurationServiceSerializer
Assembly:Azure.Data.AppConfiguration
File(s):C:\Git\azure-sdk-for-net\sdk\appconfiguration\Azure.Data.AppConfiguration\src\ConfigurationServiceSerializer.cs
Covered lines:90
Uncovered lines:3
Coverable lines:93
Total lines:210
Line coverage:96.7% (90 of 93)
Covered branches:37
Total branches:40
Branch coverage:92.5% (37 of 40)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
WriteSetting(...)-100%100%
SerializeRequestBody(...)-100%100%
WriteRequestBody(...)-100%100%
ReadSetting(...)-100%100%
ReadSetting(...)-100%100%
ReadPropertyValue(...)-90.91%90.91%
DeserializeSettingAsync()-100%100%
DeserializeSetting(...)-100%100%
ParseBatchAsync()-100%100%
ParseBatch(...)-100%100%
ParseSettingBatch(...)-100%100%
TryGetNextAfterValue(...)-90.91%75%

File(s)

C:\Git\azure-sdk-for-net\sdk\appconfiguration\Azure.Data.AppConfiguration\src\ConfigurationServiceSerializer.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Buffers;
 6using System.Globalization;
 7using System.IO;
 8using System.Text.Json;
 9using System.Threading;
 10using System.Threading.Tasks;
 11
 12namespace Azure.Data.AppConfiguration
 13{
 14    internal static class ConfigurationServiceSerializer
 15    {
 16        public static void WriteSetting(Utf8JsonWriter writer, ConfigurationSetting setting)
 17        {
 418            writer.WriteStartObject();
 419            writer.WriteString("key", setting.Key);
 420            writer.WriteString("label", setting.Label);
 21
 422            if (setting.IsReadOnly != default)
 23            {
 424                writer.WriteBoolean("locked", setting.IsReadOnly.Value);
 25            }
 26
 427            if (setting.LastModified != default)
 28            {
 429                writer.WriteString("last_modified", setting.LastModified.Value.ToString(CultureInfo.InvariantCulture));
 30            }
 31
 432            WriteRequestBody(writer, setting);
 433            writer.WriteEndObject();
 434        }
 35
 36        public static ReadOnlyMemory<byte> SerializeRequestBody(ConfigurationSetting setting)
 37        {
 28838            var writer = new Core.ArrayBufferWriter<byte>();
 28839            using var json = new Utf8JsonWriter(writer);
 28840            json.WriteStartObject();
 28841            WriteRequestBody(json, setting);
 28842            json.WriteEndObject();
 28843            json.Flush();
 28844            return writer.WrittenMemory;
 28845        }
 46
 47        private static void WriteRequestBody(Utf8JsonWriter writer, ConfigurationSetting setting)
 48        {
 29249            writer.WriteString("value", setting.Value);
 29250            writer.WriteString("content_type", setting.ContentType);
 29251            if (setting.Tags != null)
 52            {
 29253                writer.WriteStartObject("tags");
 143254                foreach (System.Collections.Generic.KeyValuePair<string, string> tag in setting.Tags)
 55                {
 42456                    writer.WriteString(tag.Key, tag.Value);
 57                }
 58
 29259                writer.WriteEndObject();
 60            }
 61
 29262            if (setting.ETag != default)
 63            {
 2064                writer.WriteString("etag", setting.ETag.ToString());
 65            }
 29266        }
 67
 68        public static ConfigurationSetting ReadSetting(ref Utf8JsonReader reader)
 69        {
 470            using JsonDocument json = JsonDocument.ParseValue(ref reader);
 471            JsonElement root = json.RootElement;
 472            return ReadSetting(root);
 473        }
 74
 75        private static ConfigurationSetting ReadSetting(JsonElement root)
 76        {
 77            // TODO (pri 2): make the deserializer version resilient
 439278            var setting = new ConfigurationSetting();
 7848079            foreach (JsonProperty property in root.EnumerateObject())
 80            {
 3484881                ReadPropertyValue(setting, property);
 82            }
 439283            return setting;
 84        }
 85
 86        private static void ReadPropertyValue(ConfigurationSetting setting, JsonProperty property)
 87        {
 3484888            if (property.NameEquals("content_type"))
 89            {
 438490                setting.ContentType = property.Value.GetString();
 91            }
 3046492            else if (property.NameEquals("etag"))
 93            {
 432094                setting.ETag = new ETag(property.Value.GetString());
 95            }
 2614496            else if (property.NameEquals("key"))
 97            {
 439298                setting.Key = property.Value.GetString();
 99            }
 21752100            else if (property.NameEquals("label"))
 101            {
 4388102                setting.Label = property.Value.GetString();
 103            }
 17364104            else if (property.NameEquals("last_modified"))
 105            {
 4288106                if (property.Value.ValueKind == JsonValueKind.Null)
 107                {
 0108                    setting.LastModified = null;
 109                }
 110                else
 111                {
 4288112                    setting.LastModified = DateTimeOffset.Parse(property.Value.GetString(), CultureInfo.InvariantCulture
 113                }
 114            }
 13076115            else if (property.NameEquals("locked"))
 116            {
 4308117                if (property.Value.ValueKind == JsonValueKind.Null)
 118                {
 0119                    setting.IsReadOnly = null;
 120                }
 121                else
 122                {
 4308123                    setting.IsReadOnly = property.Value.GetBoolean();
 124                }
 125            }
 8768126            else if (property.NameEquals("tags"))
 127            {
 9984128                foreach (JsonProperty element in property.Value.EnumerateObject())
 129                {
 608130                    setting.Tags.Add(element.Name, element.Value.GetString());
 131                }
 132            }
 4384133            else if (property.NameEquals("value"))
 134            {
 4384135                setting.Value = property.Value.GetString();
 136            }
 8768137        }
 138
 139        public static async Task<ConfigurationSetting> DeserializeSettingAsync(Stream content, CancellationToken cancell
 140        {
 158141            using (JsonDocument json = await JsonDocument.ParseAsync(content, default, cancellation).ConfigureAwait(fals
 142            {
 158143                JsonElement root = json.RootElement;
 158144                return ReadSetting(root);
 145            }
 158146        }
 147
 148        public static ConfigurationSetting DeserializeSetting(Stream content)
 149        {
 202150            using JsonDocument json = JsonDocument.Parse(content, default);
 202151            JsonElement root = json.RootElement;
 202152            return ReadSetting(root);
 202153        }
 154
 155        public static async Task<SettingBatch> ParseBatchAsync(Response response, CancellationToken cancellation)
 156        {
 54157            Stream content = response.ContentStream;
 54158            using (JsonDocument json = await JsonDocument.ParseAsync(content, cancellationToken: cancellation).Configure
 159            {
 54160                return ParseSettingBatch(response, json);
 161            }
 54162        }
 163
 164        public static SettingBatch ParseBatch(Response response)
 165        {
 54166            Stream content = response.ContentStream;
 54167            using (JsonDocument json = JsonDocument.Parse(content))
 168            {
 54169                return ParseSettingBatch(response, json);
 170            }
 54171        }
 172
 173        private static SettingBatch ParseSettingBatch(Response response, JsonDocument json)
 174        {
 108175            TryGetNextAfterValue(ref response, out string nextBatchUri);
 176
 108177            JsonElement itemsArray = json.RootElement.GetProperty("items");
 108178            int length = itemsArray.GetArrayLength();
 108179            ConfigurationSetting[] settings = new ConfigurationSetting[length];
 180
 108181            int i = 0;
 8272182            foreach (JsonElement item in itemsArray.EnumerateArray())
 183            {
 4028184                settings[i++] = ReadSetting(item);
 185            }
 186
 108187            return new SettingBatch(settings, nextBatchUri);
 188        }
 189
 190        private const string Link = "Link";
 191        private const string After = "after=";
 192        private static bool TryGetNextAfterValue(ref Response response, out string afterValue)
 193        {
 108194            afterValue = default;
 108195            if (!response.Headers.TryGetValue(Link, out var headerValue))
 68196                return false;
 197
 198            // the headers value is something like this: "</kv?after={token}>; rel=\"next\""
 40199            var afterIndex = headerValue.IndexOf(After, StringComparison.Ordinal);
 40200            if (afterIndex < 0)
 0201                return false;
 202
 40203            int beginingToken = afterIndex + After.Length;
 40204            int endToken = headerValue.IndexOf(">", StringComparison.Ordinal);
 40205            int tokenLenght = endToken - beginingToken;
 40206            afterValue = headerValue.Substring(beginingToken, tokenLenght);
 40207            return true;
 208        }
 209    }
 210}