From 1c01360467724257653e132e74b7fe0601d62023 Mon Sep 17 00:00:00 2001 From: ewdlop Date: Sat, 23 Mar 2024 18:27:54 -0400 Subject: [PATCH] Add more case handling for add --- Program.cs | 132 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 95 insertions(+), 37 deletions(-) diff --git a/Program.cs b/Program.cs index a6e7cc6..98f90c8 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,5 @@ // See https://aka.ms/new-console-template for more information +using CosmosDBPartialUpdateTypeConverter; using Microsoft.Azure.Cosmos; using Newtonsoft.Json.Linq; using System.Collections; @@ -7,19 +8,47 @@ Console.WriteLine("Hello, World!"); + +PatchOperationList patchOperationList = new() +{ + PatchOperation.Add("age", 42), + PatchOperation.Add("address", new JObject + { + { "city", "Seattle" }, + { "state", "WA" }, + { "postalCode", "98052" } + }), + Enumerable.Range(0, 10).Select(i => new {}), + 1, //ignored + new { Name = "John Doe", Age = 42 }, + ("test","123"), + "",//ignored + {"","" },//ignored + ("","",""),//ignored +}; + +PatchOperationList patchOperationList2 = []; + +PatchOperationList patchOperationList3 = new List(); +PatchOperationList patchOperationList4 = PatchOperation.Move("from", "to"); + +List patchOperations = patchOperationList; +IList patchOperations2 = new PatchOperationList(); + //Not done yet namespace CosmosDBPartialUpdateTypeConverter { - public class PatchOperationList(List _patchOperations) : IList, IReadOnlyList + public class PatchOperationList : IList, IReadOnlyList { + private readonly List _patchOperations = []; - public static explicit operator PatchOperationList(List patchOperations) => new(patchOperations); - public static explicit operator PatchOperationList(PatchOperation[] patchOperations) => new([.. patchOperations]); - public static explicit operator PatchOperationList(PatchOperation patchOperation) => new([patchOperation]); - + public static implicit operator PatchOperationList(List patchOperations) => new(patchOperations); + public static implicit operator PatchOperationList(PatchOperation[] patchOperations) => new([.. patchOperations]); + public static implicit operator PatchOperationList(PatchOperation patchOperation) => new([patchOperation]); + public static implicit operator List(PatchOperationList patchOperation) => patchOperation._patchOperations; public PatchOperationList() : this([]) { } - + public PatchOperationList(IList patchOperations) => _patchOperations = [.. patchOperations]; public PatchOperationList(IEnumerable patchOperations) : this(patchOperations.ToList()) { } public PatchOperationList(PatchOperation patchOperation) : this([patchOperation]) { } @@ -51,13 +80,13 @@ public void Add(JObject value) } public void Add(T entity) - where T : notnull => _patchOperations.Add(entity); + where T : class => _patchOperations.Add(entity); public void Add(IEnumerable entities) - where T : notnull => _patchOperations.Add(entities); + where T : class => _patchOperations.Add(entities); public void Add(params T[] entities) - where T : notnull => _patchOperations.Add(entities); + where T : class => _patchOperations.Add(entities); public void Add(object value, Func? propertyInfoFilter = null) => _patchOperations.Add(value, propertyInfoFilter); @@ -82,13 +111,13 @@ public void AddSet(JObject value) } public void AddSet(T entity) - where T : notnull => _patchOperations.AddSet(entity); + where T : class => _patchOperations.AddSet(entity); public void AddSet(IEnumerable entities) - where T : notnull => _patchOperations.AddSet(entities); + where T : class => _patchOperations.AddSet(entities); public void AddSet(params T[] entities) - where T : notnull => _patchOperations.AddSet(entities); + where T : class => _patchOperations.AddSet(entities); public void AddSet(object value, Func? propertyInfoFilter = null) => _patchOperations.AddSet(value, propertyInfoFilter); @@ -114,10 +143,10 @@ public void AddSet(params T[] entities) public IEnumerator GetEnumerator() => _patchOperations.GetEnumerator(); - IEnumerator IEnumerable.GetEnumerator() => (this as IEnumerable).GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } - public static class PatchOperationListExtension + public static class PatchOperationExtension { public static List Add(this List patchOperations, string path, object? value) { @@ -141,48 +170,77 @@ public static List Add(this List patchOperations } public static List Add(this List patchOperations, T entity) - where T : notnull + where T : class { - patchOperations.AddRange(typeof(T).GetProperties().Select(property => PatchOperation.Add(property.Name, property.GetValue(entity)))); + patchOperations.AddRange(entity switch + { + string => Enumerable.Empty(), + _ => typeof(T).GetProperties().Select(property => PatchOperation.Add(property.Name, property.GetValue(entity))) + }); return patchOperations; } public static List Add(this List patchOperations, IEnumerable entities) - where T : notnull + where T : class { - patchOperations.AddRange(entities.SelectMany(entity => entity.GetType().GetProperties().Select(property => PatchOperation.Add(property.Name, property.GetValue(entity))))); + patchOperations.AddRange(entities.Where(entity => entity is not string) + .SelectMany(entity => entity.GetType().GetProperties().Select(property => PatchOperation.Add(property.Name, property.GetValue(entity))))); return patchOperations; } public static List Add(this List patchOperations, params T[] entities) - where T : notnull + where T : class { - patchOperations.AddRange(entities.SelectMany(entity => entity.GetType().GetProperties().Select(property => PatchOperation.Add(property.Name, property.GetValue(entity))))); + patchOperations.AddRange(entities.Where(entity => entity is not string) + .SelectMany(entity => entity.GetType().GetProperties().Select(property => PatchOperation.Add(property.Name, property.GetValue(entity))))); return patchOperations; } public static List Add(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.Add(property.Name, property.GetValue(value)))); + patchOperations.AddRange(value switch + { + (object item1, object item2) => [ PatchOperation.Add(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.Add(property.Name, property.GetValue(value))) + }); return patchOperations; } public static List Add(this List patchOperations, IEnumerable values) { - patchOperations.AddRange(values.SelectMany(item => item.GetType().GetProperties().Select(property => PatchOperation.Add(property.Name, property.GetValue(item))))); + patchOperations.AddRange(values.SelectMany(value => value switch + { + (object item1, object item2) => [PatchOperation.Add(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.Add(property.Name, property.GetValue(value))) + })); return patchOperations; } public static List Add(this List patchOperations, params object[] values) { - patchOperations.AddRange(values.SelectMany(item => item.GetType().GetProperties().Select(property => PatchOperation.Add(property.Name, property.GetValue(item))))); + patchOperations.AddRange(values.SelectMany(value => value switch + { + (object item1, object item2) => [PatchOperation.Add(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.Add(property.Name, property.GetValue(value))) + })); 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)); @@ -241,23 +299,23 @@ public static List AddSet(this List patchOperati } public static List AddSet(this List patchOperations, T entity) - where T : notnull + where T : class { patchOperations.AddRange(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 : notnull + where T : class { - patchOperations.AddRange(entities.SelectMany(entity => entity.GetType().GetProperties().Select(property => PatchOperation.Set(property.Name, property.GetValue(entity))))); + patchOperations.AddRange(entities.SelectMany(entity => typeof(T).GetProperties().Select(property => PatchOperation.Set(property.Name, property.GetValue(entity))))); return patchOperations; } public static List AddSet(this List patchOperations, params T[] entities) - where T : notnull + where T : class { - patchOperations.AddRange(entities.SelectMany(entity => entity.GetType().GetProperties().Select(property => PatchOperation.Set(property.Name, property.GetValue(entity))))); + patchOperations.AddRange(entities.SelectMany(entity => typeof(T).GetProperties().Select(property => PatchOperation.Set(property.Name, property.GetValue(entity))))); return patchOperations; } @@ -305,21 +363,21 @@ public PatchOperationListBuilder WithAdd(JObject value) } public PatchOperationListBuilder WithAdd(T entity) - where T : notnull + where T : class { _patchOperations.Add(entity); return this; } public PatchOperationListBuilder WithAdd(IEnumerable entities) - where T : notnull + where T : class { _patchOperations.Add(entities); return this; } public PatchOperationListBuilder WithAdd(params T[] entities) - where T : notnull + where T : class { _patchOperations.Add(entities); return this; @@ -375,21 +433,21 @@ public PatchOperationListBuilder WithSet(JObject value) } public PatchOperationListBuilder WithSet(T entity) - where T : notnull + where T : class { _patchOperations.AddSet(entity); return this; } public PatchOperationListBuilder WithSet(IEnumerable entities) - where T : notnull + where T : class { _patchOperations.AddSet(entities); return this; } public PatchOperationListBuilder WithSet(params T[] entities) - where T : notnull + where T : class { _patchOperations.AddSet(entities); return this;