Skip to content

Commit

Permalink
Make the similar change to the methods that use set oeprations
Browse files Browse the repository at this point in the history
  • Loading branch information
ewdlop committed Mar 23, 2024
1 parent 30c4761 commit a6a076f
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,6 @@ public static List<PatchOperation> Add(this List<PatchOperation> patchOperations
return patchOperations;
}

//public static List<PatchOperation> Add<T1,T2>(this List<PatchOperation> 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<PatchOperation> AddAppend(this List<PatchOperation> patchOperations, string path, object? value)
{
patchOperations.Add(PatchOperation.Add($"{path}/`", value));
Expand Down Expand Up @@ -300,43 +292,64 @@ public static List<PatchOperation> AddSet(this List<PatchOperation> patchOperati
public static List<PatchOperation> AddSet<T>(this List<PatchOperation> 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<PatchOperation>(),
_ => typeof(T).GetProperties().Select(property => PatchOperation.Set(property.Name, property.GetValue(entity)))
});
return patchOperations;
}

public static List<PatchOperation> AddSet<T>(this List<PatchOperation> patchOperations, IEnumerable<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<PatchOperation> AddSet<T>(this List<PatchOperation> 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<PatchOperation> AddSet(this List<PatchOperation> patchOperations, object? value, Func<PropertyInfo, bool>? propertyInfoFilter = null)
public static List<PatchOperation> AddSet(this List<PatchOperation> patchOperations, object value, Func<PropertyInfo, bool>? 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<PatchOperation>(),
_ => value.GetType().GetProperties().Where(propertyInfoFilter is not null ? propertyInfoFilter : _ => true)
.Select(property => PatchOperation.Set(property.Name, property.GetValue(value)))
});
return patchOperations;
}

public static List<PatchOperation> AddSet(this List<PatchOperation> patchOperations, IEnumerable<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<PatchOperation>(),
_ => value.GetType().GetProperties()
.Select(property => PatchOperation.Set(property.Name, property.GetValue(value)))
}));
return patchOperations;
}

public static List<PatchOperation> AddSet(this List<PatchOperation> 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<PatchOperation>(),
_ => value.GetType().GetProperties()
.Select(property => PatchOperation.Set(property.Name, property.GetValue(value)))
}));
return patchOperations;
}
}
Expand Down

0 comments on commit a6a076f

Please sign in to comment.