diff --git a/Program.cs b/Program.cs index 3504626..d1ca609 100644 --- a/Program.cs +++ b/Program.cs @@ -232,14 +232,6 @@ public static List Add(this List patchOperations return patchOperations; } - //public static List Add(this List patchOperations, (T1 value1,T2 value2) tuple) - //{ - // ArgumentNullException.ThrowIfNull(tuple, nameof(tuple)); - // ArgumentNullException.ThrowIfNull(tuple.value1, nameof(tuple.value1)); - // patchOperations.Add(PatchOperation.Add(tuple.value1.ToString(), tuple.value2)); - // return patchOperations; - //} - public static List AddAppend(this List patchOperations, string path, object? value) { patchOperations.Add(PatchOperation.Add($"{path}/`", value)); @@ -300,43 +292,64 @@ public static List AddSet(this List patchOperati public static List AddSet(this List patchOperations, T entity) where T : class { - patchOperations.AddRange(typeof(T).GetProperties().Select(property => PatchOperation.Set(property.Name, property.GetValue(entity)))); + patchOperations.AddRange(entity switch + { + string => Enumerable.Empty(), + _ => typeof(T).GetProperties().Select(property => PatchOperation.Set(property.Name, property.GetValue(entity))) + }); return patchOperations; } public static List AddSet(this List patchOperations, IEnumerable entities) where T : class { - patchOperations.AddRange(entities.SelectMany(entity => typeof(T).GetProperties().Select(property => PatchOperation.Set(property.Name, property.GetValue(entity))))); + patchOperations.AddRange(entities.Where(entity => entity is not string) + .SelectMany(entity => entity.GetType().GetProperties().Select(property => PatchOperation.Set(property.Name, property.GetValue(entity))))); return patchOperations; } public static List AddSet(this List patchOperations, params T[] entities) where T : class { - patchOperations.AddRange(entities.SelectMany(entity => typeof(T).GetProperties().Select(property => PatchOperation.Set(property.Name, property.GetValue(entity))))); + patchOperations.AddRange(entities.Where(entity => entity is not string) + .SelectMany(entity => entity.GetType().GetProperties().Select(property => PatchOperation.Set(property.Name, property.GetValue(entity))))); return patchOperations; } - public static List AddSet(this List patchOperations, object? value, Func? propertyInfoFilter = null) + public static List AddSet(this List patchOperations, object value, Func? propertyInfoFilter = null) { ArgumentNullException.ThrowIfNull(value, nameof(value)); - patchOperations.AddRange(value.GetType() - .GetProperties() - .Where(propertyInfoFilter is not null ? propertyInfoFilter : _ => true) - .Select(property => PatchOperation.Set(property.Name, property.GetValue(value)))); + patchOperations.AddRange(value switch + { + (object item1, object item2) => [PatchOperation.Set(item1.ToString(), item2)], + int or long or float or double or decimal or string or bool or null => Enumerable.Empty(), + _ => value.GetType().GetProperties().Where(propertyInfoFilter is not null ? propertyInfoFilter : _ => true) + .Select(property => PatchOperation.Set(property.Name, property.GetValue(value))) + }); return patchOperations; } public static List AddSet(this List patchOperations, IEnumerable values) { - patchOperations.AddRange(values.SelectMany(item => item.GetType().GetProperties().Select(property => PatchOperation.Set(property.Name, property.GetValue(item))))); + patchOperations.AddRange(values.SelectMany(value => value switch + { + (object item1, object item2) => [PatchOperation.Set(item1.ToString(), item2)], + int or long or float or double or decimal or string or bool or null => Enumerable.Empty(), + _ => value.GetType().GetProperties() + .Select(property => PatchOperation.Set(property.Name, property.GetValue(value))) + })); return patchOperations; } public static List AddSet(this List patchOperations, params object[] values) { - patchOperations.AddRange(values.SelectMany(item => item.GetType().GetProperties().Select(property => PatchOperation.Set(property.Name, property.GetValue(item))))); + patchOperations.AddRange(values.SelectMany(value => value switch + { + (object item1, object item2) => [PatchOperation.Set(item1.ToString(), item2)], + int or long or float or double or decimal or string or bool or null => Enumerable.Empty(), + _ => value.GetType().GetProperties() + .Select(property => PatchOperation.Set(property.Name, property.GetValue(value))) + })); return patchOperations; } }