diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-07-24 14:39:40 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-24 14:39:40 -0500 |
| commit | 58bc401dab5d7eb3bc6cb07a22e0ee9c97ddf4bd (patch) | |
| tree | 64072f5eaa896335cc04de8ffe84fa79a50a2be7 /src/slskd/Common/Extensions.cs | |
| parent | fc7800409dfbe0cf26a72dd1de17cbae23d0cb35 (diff) | |
| parent | 78b3b953d9c36c2cc2939ad00881b0cbf388865a (diff) | |
| download | slskd-58bc401dab5d7eb3bc6cb07a22e0ee9c97ddf4bd.tar.gz | |
Merge pull request #125 from slskd/options
Refactor configuration components to allow for run-time updates
Diffstat (limited to 'src/slskd/Common/Extensions.cs')
| -rw-r--r-- | src/slskd/Common/Extensions.cs | 60 |
1 files changed, 56 insertions, 4 deletions
diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index a7a89a71..3e4f6e68 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -19,12 +19,10 @@ namespace slskd { using System; using System.Collections.Generic; - using System.ComponentModel.DataAnnotations; using System.IO; using System.Linq; - using System.Security.Cryptography; - using System.Text; - using slskd.Validation; + using System.Reflection; + using System.Text.Json; /// <summary> /// Extensions. @@ -32,6 +30,60 @@ namespace slskd public static class Extensions { /// <summary> + /// Serializes this object to json. + /// </summary> + /// <param name="obj">The object to serialize.</param> + /// <returns>A string containing the serialized object.</returns> + public static string ToJson(this object obj) => JsonSerializer.Serialize(obj); + + /// <summary> + /// Deserializes this string from json to an object of type <typeparamref name="T"/>. + /// </summary> + /// <typeparam name="T">The type to which to deserialize the string.</typeparam> + /// <param name="str">The string to deserialize.</param> + /// <returns>The new object deserialzied from the string.</returns> + public static T ToObject<T>(this string str) => JsonSerializer.Deserialize<T>(str); + + /// <summary> + /// Deeply compares this object with the specified object and returns a list of properties that are different. + /// </summary> + /// <param name="left">The left side of the comparison.</param> + /// <param name="right">The right side of the comparison.</param> + /// <param name="parentFqn">The root path for recursive calls.</param> + /// <returns>A list of differences between the two objects.</returns> + public static IEnumerable<(PropertyInfo Property, string FQN, object Left, object Right)> DiffWith(this object left, object right, string parentFqn = null) + { + if (left?.GetType() != right?.GetType()) + { + throw new InvalidCastException($"Unable to diff types {left?.GetType()} and {right?.GetType()}"); + } + + var differences = new List<(PropertyInfo Property, string FQN, object Left, object Right)>(); + + foreach (var prop in left?.GetType().GetProperties()) + { + var leftVal = prop.GetValue(left); + var rightVal = prop.GetValue(right); + var propType = prop.PropertyType; + var fqn = string.IsNullOrEmpty(parentFqn) ? prop.Name : string.Join(".", parentFqn, prop.Name); + + if (propType.IsPrimitive || Nullable.GetUnderlyingType(propType) != null || new[] { typeof(string), typeof(decimal) }.Contains(propType)) + { + if (!Equals(leftVal, rightVal)) + { + differences.Add((prop, fqn, leftVal, rightVal)); + } + } + else + { + differences.AddRange(DiffWith(leftVal, rightVal, fqn)); + } + } + + return differences; + } + + /// <summary> /// Returns the directory from the given path, regardless of separator format. /// </summary> /// <param name="path">The path.</param> |
