< Summary

Class:Azure.DigitalTwins.Core.Serialization.UpdateOperationsUtility
Assembly:Azure.DigitalTwins.Core
File(s):C:\Git\azure-sdk-for-net\sdk\digitaltwins\Azure.DigitalTwins.Core\src\Serialization\UpdateOperationsUtility.cs
Covered lines:25
Uncovered lines:0
Coverable lines:25
Total lines:92
Line coverage:100% (25 of 25)
Covered branches:0
Total branches:0

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor()-100%100%
AppendAddOp(...)-100%100%
AppendReplaceOp(...)-100%100%
AppendRemoveOp(...)-100%100%
Serialize()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\digitaltwins\Azure.DigitalTwins.Core\src\Serialization\UpdateOperationsUtility.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System.Collections.Generic;
 5using System.Text.Json;
 6
 7namespace Azure.DigitalTwins.Core.Serialization
 8{
 9    /// <summary>
 10    /// A utility to create the application/json-patch+json operations payload required for update operations.
 11    /// </summary>
 12    /// <remarks>
 13    /// For more samples, see <see href="https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/digitaltwins/Azure.D
 14    /// </remarks>
 15    /// <example>
 16    /// <code snippet="Snippet:DigitalTwinsSampleUpdateComponent">
 17    /// // Update Component1 by replacing the property ComponentProp1 value,
 18    /// // using an optional utility to build the payload.
 19    /// var componentUpdateUtility = new UpdateOperationsUtility();
 20    /// componentUpdateUtility.AppendReplaceOp(&quot;/ComponentProp1&quot;, &quot;Some new value&quot;);
 21    /// string updatePayload = componentUpdateUtility.Serialize();
 22    /// await client.UpdateComponentAsync(basicDtId, &quot;Component1&quot;, updatePayload);
 23    /// Console.WriteLine($&quot;Updated component for digital twin &apos;{basicDtId}&apos;.&quot;);
 24    /// </code>
 25    /// </example>
 26    public class UpdateOperationsUtility
 27    {
 28        private const string Op = "op";
 29        private const string Add = "add";
 30        private const string Replace = "replace";
 31        private const string Remove = "remove";
 32        private const string Path = "path";
 33        private const string Value = "value";
 34
 1435        private readonly List<Dictionary<string, object>> _ops = new List<Dictionary<string, object>>();
 36
 37        /// <summary>
 38        /// Include an add operation.
 39        /// </summary>
 40        /// <param name="path">The path to the property to be added.</param>
 41        /// <param name="value">The value to update to.</param>
 42        public void AppendAddOp(string path, object value)
 43        {
 644            var op = new Dictionary<string, object>
 645            {
 646                { Op, Add },
 647                { Path, path },
 648                { Value, value },
 649            };
 650            _ops.Add(op);
 651        }
 52
 53        /// <summary>
 54        /// Include a replace operation.
 55        /// </summary>
 56        /// <param name="path">The path to the property to be updated.</param>
 57        /// <param name="value">The value to update to.</param>
 58        public void AppendReplaceOp(string path, object value)
 59        {
 1460            var op = new Dictionary<string, object>
 1461            {
 1462                { Op, Replace },
 1463                { Path, path },
 1464                { Value, value },
 1465            };
 1466            _ops.Add(op);
 1467        }
 68
 69        /// <summary>
 70        /// Include a remove operation.
 71        /// </summary>
 72        /// <param name="path">The path to the property to be added.</param>
 73        public void AppendRemoveOp(string path)
 74        {
 675            var op = new Dictionary<string, object>
 676            {
 677                { Op, Remove },
 678                { Path, path },
 679            };
 680            _ops.Add(op);
 681        }
 82
 83        /// <summary>
 84        /// Serialize the constructed payload as json.
 85        /// </summary>
 86        /// <returns>A string of the json payload.</returns>
 87        public string Serialize()
 88        {
 1489            return JsonSerializer.Serialize(_ops);
 90        }
 91    }
 92}