From 53bb94a5db3730dd7c48efd5562b72673e22f0fb Mon Sep 17 00:00:00 2001 From: Mattias Nordqvist Date: Tue, 16 Jan 2024 18:51:34 +0100 Subject: [PATCH 1/5] use indices in BindAll, if you'd like --- Results/BindAll.cs | 224 ++++++++++++++++++++++++++++++++++++++++---- Results/BindEach.cs | 44 --------- 2 files changed, 207 insertions(+), 61 deletions(-) diff --git a/Results/BindAll.cs b/Results/BindAll.cs index f1e5ed9..256dfb9 100644 --- a/Results/BindAll.cs +++ b/Results/BindAll.cs @@ -23,6 +23,27 @@ public static Result BindAll(this Result> source, Func BindAll(this Result> source, Func> next) + { + if (source.Success) + { + var results = new List>(); + var index = 0; + foreach (var s in source.Value) + { + results.Add(next(s, index)); + index++; + } + return results.Any(x => !x.Success) + ? UnitResult.Error(results.SelectMany(x => x.Errors)) + : UnitResult.Ok; + } + else + { + return Result.Error(source.Errors); + } + } + public static async Task> BindAll(this Result> source, Func>> next) { if (source.Success) @@ -42,6 +63,27 @@ public static async Task> BindAll(this Result> so } } + public static async Task> BindAll(this Result> source, Func>> next) + { + if (source.Success) + { + var results = new List>(); + var index = 0; + foreach (var s in source.Value) + { + results.Add(await next(s, index)); + index++; + } + return results.Any(x => !x.Success) + ? UnitResult.Error(results.SelectMany(x => x.Errors)) + : UnitResult.Ok; + } + else + { + return Result.Error(source.Errors); + } + } + /// /// Does not short circuit. Returns all errors or all results /// @@ -65,35 +107,117 @@ public static Result> BindAll(this Result> BindAll(this IEnumerable source, Func> next) + public static Result> BindAll(this Result> source, Func> next) + { + if (source.Success) + { + var results = new List>(); + var index = 0; + foreach (var s in source.Value) + { + results.Add(next(s, index)); + index++; + } + return results.Any(x => !x.Success) + ? Result>.Error(results.SelectMany(x => x.Errors)) + : Result>.Ok(results.Select(x => x.Value).ToList()); + } + else + { + return Result>.Error(source.Errors); + } + } + + /// + /// Does not short circuit. Returns all errors or all results + /// + public static async Task>> BindAll(this Result> source, Func>> next) { - var results = new List>(); + if (source.Success) + { + var results = new List>(); - foreach (var s in source) + foreach (var s in source.Value) + { + results.Add(await next(s)); + } + return results.Any(x => !x.Success) + ? Result>.Error(results.SelectMany(x => x.Errors)) + : Result>.Ok(results.Select(x => x.Value).ToList()); + } + else { - results.Add(next(s)); + return Result>.Error(source.Errors); } - return results.Any(x => !x.Success) - ? Result>.Error(results.SelectMany(x => x.Errors)) - : Result>.Ok(results.Select(x => x.Value).ToList()); } - [Obsolete("Does not take a Result as input. Use one of the regular extensions that takes a result as input. (Do a .Return() on your non-result object first)")] - public static async Task>> BindAll(this IEnumerable source, Func>> next) + public static async Task>> BindAll(this Result> source, Func>> next) + { + if (source.Success) + { + var results = new List>(); + var index = 0; + foreach (var s in source.Value) + { + results.Add(await next(s, index)); + index++; + } + return results.Any(x => !x.Success) + ? Result>.Error(results.SelectMany(x => x.Errors)) + : Result>.Ok(results.Select(x => x.Value).ToList()); + } + else + { + return Result>.Error(source.Errors); + } + } + + ////// Same as above, but from tasks + + + /// + /// Does not short circuit. Returns all errors or one unit + /// + public static async Task> BindAll(this Task>> source, Func> next) { - var results = new List>(); + if ((await source).Success) + { + var results = new List>(); + foreach (var s in (await source).Value) + { + results.Add(next(s)); + } + return results.Any(x => !x.Success) + ? UnitResult.Error(results.SelectMany(x => x.Errors)) + : UnitResult.Ok; + } + else + { + return Result.Error((await source).Errors); + } + } - foreach (var s in source) + public static async Task> BindAll(this Task>> source, Func> next) + { + if ((await source).Success) { - results.Add(await next(s)); + var results = new List>(); + var index = 0; + foreach (var s in (await source).Value) + { + results.Add(next(s, index)); + index++; + } + return results.Any(x => !x.Success) + ? UnitResult.Error(results.SelectMany(x => x.Errors)) + : UnitResult.Ok; + } + else + { + return Result.Error((await source).Errors); } - return results.Any(x => !x.Success) - ? Result>.Error(results.SelectMany(x => x.Errors)) - : Result>.Ok(results.Select(x => x.Value).ToList()); } - // with tasks public static async Task> BindAll(this Task>> source, Func>> next) { if ((await source).Success) @@ -113,6 +237,27 @@ public static async Task> BindAll(this Task> BindAll(this Task>> source, Func>> next) + { + if ((await source).Success) + { + var results = new List>(); + var index = 0; + foreach (var s in (await source).Value) + { + results.Add(await next(s, index)); + index++; + } + return results.Any(x => !x.Success) + ? UnitResult.Error(results.SelectMany(x => x.Errors)) + : UnitResult.Ok; + } + else + { + return Result.Error((await source).Errors); + } + } + /// /// Does not short circuit. Returns all errors or all results /// @@ -136,6 +281,30 @@ public static async Task>> BindAll(this Task>> BindAll(this Task>> source, Func> next) + { + if ((await source).Success) + { + var results = new List>(); + var index = 0; + foreach (var s in (await source).Value) + { + results.Add(next(s, index)); + index++; + } + return results.Any(x => !x.Success) + ? Result>.Error(results.SelectMany(x => x.Errors)) + : Result>.Ok(results.Select(x => x.Value).ToList()); + } + else + { + return Result>.Error((await source).Errors); + } + } + + /// + /// Does not short circuit. Returns all errors or all results + /// public static async Task>> BindAll(this Task>> source, Func>> next) { if ((await source).Success) @@ -155,4 +324,25 @@ public static async Task>> BindAll(this Task>.Error((await source).Errors); } } + + public static async Task>> BindAll(this Task>> source, Func>> next) + { + if ((await source).Success) + { + var results = new List>(); + var index = 0; + foreach (var s in (await source).Value) + { + results.Add(await next(s, index)); + index++; + } + return results.Any(x => !x.Success) + ? Result>.Error(results.SelectMany(x => x.Errors)) + : Result>.Ok(results.Select(x => x.Value).ToList()); + } + else + { + return Result>.Error((await source).Errors); + } + } } diff --git a/Results/BindEach.cs b/Results/BindEach.cs index a3c672a..7cb7fcb 100644 --- a/Results/BindEach.cs +++ b/Results/BindEach.cs @@ -1,50 +1,6 @@ namespace DotNetThoughts.Results; public static partial class Extensions { - /// - /// Short circuits on failure - /// - [Obsolete("Does not take a Result as input. Use one of the regular extensions that takes a result as input. (Do a .Return() on your non-result object first)")] - public static Result BindEach(this IEnumerable source, Func> next) - { - foreach (var s in source) - { - var result = next(s); - if (!result.Success) return result; - } - return UnitResult.Ok; - } - - /// - /// Short circuits on failure - /// - [Obsolete("Does not take a Result as input. Use one of the regular extensions that takes a result as input. (Do a .Return() on your non-result object first)")] - public static async Task> BindEach(this IEnumerable source, Func>> next) - { - foreach (var s in source) - { - var result = await next(s); - if (!result.Success) return result; - } - return UnitResult.Ok; - } - - /// - /// Short circuits on failure - /// - [Obsolete("Does not take a Result as input. Use one of the regular extensions that takes a result as input. (Do a .Return() on your non-result object first)")] - public static Result> BindEach(this IEnumerable source, Func> next) - { - var result = new List(); - foreach (var s in source) - { - var r = next(s); - if (!r.Success) return Result>.Error(r.Errors); - result.Add(r.Value); - } - return Result>.Ok(result); - } - /// /// Short circuits on failure /// From 8e69cecd0e5e3950d16ff21bb955b7b03563e774 Mon Sep 17 00:00:00 2001 From: Mattias Nordqvist Date: Tue, 16 Jan 2024 18:51:47 +0100 Subject: [PATCH 2/5] add longer tuples to Or --- Results/Or.cs | 125 ++++++++++++++++++++++++-------------------------- 1 file changed, 61 insertions(+), 64 deletions(-) diff --git a/Results/Or.cs b/Results/Or.cs index 209bce6..1d02559 100644 --- a/Results/Or.cs +++ b/Results/Or.cs @@ -8,68 +8,65 @@ /// public static partial class Extensions { - public static Result<(T, T2)> OrResult(Result a, Result b) => a.Or(b); - public static Result<(T, T2, T3)> OrResult(Result a, Result b, Result c) => a.Or(b).Or(c); - public static Result<(T, T2, T3, T4)> OrResult(Result a, Result b, Result c, Result d) => a.Or(b).Or(c).Or(d); - public static Result<(T, T2, T3, T4, T5)> OrResult(Result a, Result b, Result c, Result d, Result e) => a.Or(b).Or(c).Or(d).Or(e); - public static Result<(T, T2, T3, T4, T5, T6)> OrResult(Result a, Result b, Result c, Result d, Result e, Result f) => a.Or(b).Or(c).Or(d).Or(e).Or(f); - public static Result<(T, T2, T3, T4, T5, T6, T7)> OrResult(Result a, Result b, Result c, Result d, Result e, Result f, Result g) => a.Or(b).Or(c).Or(d).Or(e).Or(f).Or(g); - public static Result<(T, T2, T3, T4, T5, T6, T7, T8)> OrResult(Result a, Result b, Result c, Result d, Result e, Result f, Result g, Result h) => a.Or(b).Or(c).Or(d).Or(e).Or(f).Or(g).Or(h); - public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9)> OrResult(Result a, Result b, Result c, Result d, Result e, Result f, Result g, Result h, Result i) => a.Or(b).Or(c).Or(d).Or(e).Or(f).Or(g).Or(h).Or(i); - public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10)> OrResult(Result a, Result b, Result c, Result d, Result e, Result f, Result g, Result h, Result i, Result j) => a.Or(b).Or(c).Or(d).Or(e).Or(f).Or(g).Or(h).Or(i).Or(j); - public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)> OrResult(Result a, Result b, Result c, Result d, Result e, Result f, Result g, Result h, Result i, Result j, Result k) => a.Or(b).Or(c).Or(d).Or(e).Or(f).Or(g).Or(h).Or(i).Or(j).Or(k); - public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)> OrResult(Result a, Result b, Result c, Result d, Result e, Result f, Result g, Result h, Result i, Result j, Result k, Result l) => a.Or(b).Or(c).Or(d).Or(e).Or(f).Or(g).Or(h).Or(i).Or(j).Or(k).Or(l); - - public static Result<(T, T2)> Or(this Result a, Result b) => !a.Success || !b.Success - ? Result<(T, T2)>.Error(a.Errors.Concat(b.Errors)) - : Result<(T, T2)>.Ok((a.Value, b.Value)); - public static async Task> Or(this Task> a, Result b) => !(await a).Success || !b.Success - ? Result<(T, T2)>.Error((await a).Errors.Concat(b.Errors)) - : Result<(T, T2)>.Ok(((await a).Value, b.Value)); - public static async Task> Or(this Task> a, Task> b) => !(await a).Success || !(await b).Success - ? Result<(T, T2)>.Error((await a).Errors.Concat((await b).Errors)) - : Result<(T, T2)>.Ok(((await a).Value, (await b).Value)); - - public static Result<(T, T2, T3)> Or(this Result<(T, T2)> a, Result b) => !a.Success || !b.Success - ? Result<(T, T2, T3)>.Error(a.Errors.Concat(b.Errors)) - : Result<(T, T2, T3)>.Ok((a.Value.Item1, a.Value.Item2, b.Value)); - public static async Task> Or(this Task> a, Task> b) => !(await a).Success || !(await b).Success - ? Result<(T, T2, T3)>.Error((await a).Errors.Concat((await b).Errors)) - : Result<(T, T2, T3)>.Ok(((await a).Value.Item1, (await a).Value.Item2, (await b).Value)); - - public static Result<(T, T2, T3, T4)> Or(this Result<(T, T2, T3)> a, Result b) => !a.Success || !b.Success - ? Result<(T, T2, T3, T4)>.Error(a.Errors.Concat(b.Errors)) - : Result<(T, T2, T3, T4)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, b.Value)); - - public static Result<(T, T2, T3, T4, T5)> Or(this Result<(T, T2, T3, T4)> a, Result b) => !a.Success || !b.Success - ? Result<(T, T2, T3, T4, T5)>.Error(a.Errors.Concat(b.Errors)) - : Result<(T, T2, T3, T4, T5)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, b.Value)); - - public static Result<(T, T2, T3, T4, T5, T6)> Or(this Result<(T, T2, T3, T4, T5)> a, Result b) => !a.Success || !b.Success - ? Result<(T, T2, T3, T4, T5, T6)>.Error(a.Errors.Concat(b.Errors)) - : Result<(T, T2, T3, T4, T5, T6)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, b.Value)); - - public static Result<(T, T2, T3, T4, T5, T6, T7)> Or(this Result<(T, T2, T3, T4, T5, T6)> a, Result b) => !a.Success || !b.Success - ? Result<(T, T2, T3, T4, T5, T6, T7)>.Error(a.Errors.Concat(b.Errors)) - : Result<(T, T2, T3, T4, T5, T6, T7)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, a.Value.Item6, b.Value)); - - public static Result<(T, T2, T3, T4, T5, T6, T7, T8)> Or(this Result<(T, T2, T3, T4, T5, T6, T7)> a, Result b) => !a.Success || !b.Success - ? Result<(T, T2, T3, T4, T5, T6, T7, T8)>.Error(a.Errors.Concat(b.Errors)) - : Result<(T, T2, T3, T4, T5, T6, T7, T8)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, a.Value.Item6, a.Value.Item7, b.Value)); - - public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9)> Or(this Result<(T, T2, T3, T4, T5, T6, T7, T8)> a, Result b) => !a.Success || !b.Success - ? Result<(T, T2, T3, T4, T5, T6, T7, T8, T9)>.Error(a.Errors.Concat(b.Errors)) - : Result<(T, T2, T3, T4, T5, T6, T7, T8, T9)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, a.Value.Item6, a.Value.Item7, a.Value.Item8, b.Value)); - - public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10)> Or(this Result<(T, T2, T3, T4, T5, T6, T7, T8, T9)> a, Result b) => !a.Success || !b.Success - ? Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10)>.Error(a.Errors.Concat(b.Errors)) - : Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, a.Value.Item6, a.Value.Item7, a.Value.Item8, a.Value.Item9, b.Value)); - - public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)> Or(this Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10)> a, Result b) => !a.Success || !b.Success - ? Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)>.Error(a.Errors.Concat(b.Errors)) - : Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, a.Value.Item6, a.Value.Item7, a.Value.Item8, a.Value.Item9, a.Value.Item10, b.Value)); - - public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)> Or(this Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)> a, Result b) => !a.Success || !b.Success - ? Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)>.Error(a.Errors.Concat(b.Errors)) - : Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, a.Value.Item6, a.Value.Item7, a.Value.Item8, a.Value.Item9, a.Value.Item10, a.Value.Item11, b.Value)); + public static Result<(T, T2)> OrResult(Result a, Result b) => a.Or(b); + public static Result<(T, T2, T3)> OrResult(Result a, Result b, Result c) => a.Or(b).Or(c); + public static Result<(T, T2, T3, T4)> OrResult(Result a, Result b, Result c, Result d) => a.Or(b).Or(c).Or(d); + public static Result<(T, T2, T3, T4, T5)> OrResult(Result a, Result b, Result c, Result d, Result e) => a.Or(b).Or(c).Or(d).Or(e); + public static Result<(T, T2, T3, T4, T5, T6)> OrResult(Result a, Result b, Result c, Result d, Result e, Result f) => a.Or(b).Or(c).Or(d).Or(e).Or(f); + public static Result<(T, T2, T3, T4, T5, T6, T7)> OrResult(Result a, Result b, Result c, Result d, Result e, Result f, Result g) => a.Or(b).Or(c).Or(d).Or(e).Or(f).Or(g); + public static Result<(T, T2, T3, T4, T5, T6, T7, T8)> OrResult(Result a, Result b, Result c, Result d, Result e, Result f, Result g, Result h) => a.Or(b).Or(c).Or(d).Or(e).Or(f).Or(g).Or(h); + public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9)> OrResult(Result a, Result b, Result c, Result d, Result e, Result f, Result g, Result h, Result i) => a.Or(b).Or(c).Or(d).Or(e).Or(f).Or(g).Or(h).Or(i); + public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10)> OrResult(Result a, Result b, Result c, Result d, Result e, Result f, Result g, Result h, Result i, Result j) => a.Or(b).Or(c).Or(d).Or(e).Or(f).Or(g).Or(h).Or(i).Or(j); + public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)> OrResult(Result a, Result b, Result c, Result d, Result e, Result f, Result g, Result h, Result i, Result j, Result k) => a.Or(b).Or(c).Or(d).Or(e).Or(f).Or(g).Or(h).Or(i).Or(j).Or(k); + public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)> OrResult(Result a, Result b, Result c, Result d, Result e, Result f, Result g, Result h, Result i, Result j, Result k, Result l) => a.Or(b).Or(c).Or(d).Or(e).Or(f).Or(g).Or(h).Or(i).Or(j).Or(k).Or(l); + public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)> Or(Result a, Result b, Result c, Result d, Result e, Result f, Result g, Result h, Result i, Result j, Result k, Result l, Result m) => a.Or(b).Or(c).Or(d).Or(e).Or(f).Or(g).Or(h).Or(i).Or(j).Or(k).Or(l).Or(m); + public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)> Or(Result a, Result b, Result c, Result d, Result e, Result f, Result g, Result h, Result i, Result j, Result k, Result l, Result m, Result n) => a.Or(b).Or(c).Or(d).Or(e).Or(f).Or(g).Or(h).Or(i).Or(j).Or(k).Or(l).Or(m).Or(n); + public static Result<(T, T2)> Or(this Result a, Result b) => !a.Success || !b.Success + ? Result<(T, T2)>.Error(a.Errors.Concat(b.Errors)) + : Result<(T, T2)>.Ok((a.Value, b.Value)); + public static async Task> Or(this Task> a, Result b) => !(await a).Success || !b.Success + ? Result<(T, T2)>.Error((await a).Errors.Concat(b.Errors)) + : Result<(T, T2)>.Ok(((await a).Value, b.Value)); + public static async Task> Or(this Task> a, Task> b) => !(await a).Success || !(await b).Success + ? Result<(T, T2)>.Error((await a).Errors.Concat((await b).Errors)) + : Result<(T, T2)>.Ok(((await a).Value, (await b).Value)); + public static Result<(T, T2, T3)> Or(this Result<(T, T2)> a, Result b) => !a.Success || !b.Success + ? Result<(T, T2, T3)>.Error(a.Errors.Concat(b.Errors)) + : Result<(T, T2, T3)>.Ok((a.Value.Item1, a.Value.Item2, b.Value)); + public static async Task> Or(this Task> a, Task> b) => !(await a).Success || !(await b).Success + ? Result<(T, T2, T3)>.Error((await a).Errors.Concat((await b).Errors)) + : Result<(T, T2, T3)>.Ok(((await a).Value.Item1, (await a).Value.Item2, (await b).Value)); + public static Result<(T, T2, T3, T4)> Or(this Result<(T, T2, T3)> a, Result b) => !a.Success || !b.Success + ? Result<(T, T2, T3, T4)>.Error(a.Errors.Concat(b.Errors)) + : Result<(T, T2, T3, T4)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, b.Value)); + public static Result<(T, T2, T3, T4, T5)> Or(this Result<(T, T2, T3, T4)> a, Result b) => !a.Success || !b.Success + ? Result<(T, T2, T3, T4, T5)>.Error(a.Errors.Concat(b.Errors)) + : Result<(T, T2, T3, T4, T5)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, b.Value)); + public static Result<(T, T2, T3, T4, T5, T6)> Or(this Result<(T, T2, T3, T4, T5)> a, Result b) => !a.Success || !b.Success + ? Result<(T, T2, T3, T4, T5, T6)>.Error(a.Errors.Concat(b.Errors)) + : Result<(T, T2, T3, T4, T5, T6)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, b.Value)); + public static Result<(T, T2, T3, T4, T5, T6, T7)> Or(this Result<(T, T2, T3, T4, T5, T6)> a, Result b) => !a.Success || !b.Success + ? Result<(T, T2, T3, T4, T5, T6, T7)>.Error(a.Errors.Concat(b.Errors)) + : Result<(T, T2, T3, T4, T5, T6, T7)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, a.Value.Item6, b.Value)); + public static Result<(T, T2, T3, T4, T5, T6, T7, T8)> Or(this Result<(T, T2, T3, T4, T5, T6, T7)> a, Result b) => !a.Success || !b.Success + ? Result<(T, T2, T3, T4, T5, T6, T7, T8)>.Error(a.Errors.Concat(b.Errors)) + : Result<(T, T2, T3, T4, T5, T6, T7, T8)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, a.Value.Item6, a.Value.Item7, b.Value)); + public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9)> Or(this Result<(T, T2, T3, T4, T5, T6, T7, T8)> a, Result b) => !a.Success || !b.Success + ? Result<(T, T2, T3, T4, T5, T6, T7, T8, T9)>.Error(a.Errors.Concat(b.Errors)) + : Result<(T, T2, T3, T4, T5, T6, T7, T8, T9)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, a.Value.Item6, a.Value.Item7, a.Value.Item8, b.Value)); + public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10)> Or(this Result<(T, T2, T3, T4, T5, T6, T7, T8, T9)> a, Result b) => !a.Success || !b.Success + ? Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10)>.Error(a.Errors.Concat(b.Errors)) + : Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, a.Value.Item6, a.Value.Item7, a.Value.Item8, a.Value.Item9, b.Value)); + public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)> Or(this Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10)> a, Result b) => !a.Success || !b.Success + ? Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)>.Error(a.Errors.Concat(b.Errors)) + : Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, a.Value.Item6, a.Value.Item7, a.Value.Item8, a.Value.Item9, a.Value.Item10, b.Value)); + public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)> Or(this Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)> a, Result b) => !a.Success || !b.Success + ? Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)>.Error(a.Errors.Concat(b.Errors)) + : Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, a.Value.Item6, a.Value.Item7, a.Value.Item8, a.Value.Item9, a.Value.Item10, a.Value.Item11, b.Value)); + public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)> Or(this Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)> a, Result b) => !a.Success || !b.Success + ? Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)>.Error(a.Errors.Concat(b.Errors)) + : Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, a.Value.Item6, a.Value.Item7, a.Value.Item8, a.Value.Item9, a.Value.Item10, a.Value.Item11, a.Value.Item12, b.Value)); + public static Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)> Or(this Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)> a, Result b) => !a.Success || !b.Success + ? Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)>.Error(a.Errors.Concat(b.Errors)) + : Result<(T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)>.Ok((a.Value.Item1, a.Value.Item2, a.Value.Item3, a.Value.Item4, a.Value.Item5, a.Value.Item6, a.Value.Item7, a.Value.Item8, a.Value.Item9, a.Value.Item10, a.Value.Item11, a.Value.Item12, a.Value.Item13, b.Value)); } From fb464098ff34378c673e7ddbe2a13f52a81a86cf Mon Sep 17 00:00:00 2001 From: Mattias Nordqvist Date: Tue, 16 Jan 2024 18:56:29 +0100 Subject: [PATCH 3/5] ErrorBase retains data that you add manually --- .../DotNetThoughts.Results.Tests.csproj | 2 +- Results/DotNetThoughts.Results.csproj | 4 ++-- Results/ErrorBase.cs | 17 +++++++++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Results.Tests/DotNetThoughts.Results.Tests.csproj b/Results.Tests/DotNetThoughts.Results.Tests.csproj index 4291623..ad5a721 100644 --- a/Results.Tests/DotNetThoughts.Results.Tests.csproj +++ b/Results.Tests/DotNetThoughts.Results.Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net8.0 enable enable diff --git a/Results/DotNetThoughts.Results.csproj b/Results/DotNetThoughts.Results.csproj index 58cfc45..5d98814 100644 --- a/Results/DotNetThoughts.Results.csproj +++ b/Results/DotNetThoughts.Results.csproj @@ -1,10 +1,10 @@ - net7.0 + net8.0 enable enable - 1.4.3 + 1.5.0 True MIT diff --git a/Results/ErrorBase.cs b/Results/ErrorBase.cs index 7f04b7d..b5f717b 100644 --- a/Results/ErrorBase.cs +++ b/Results/ErrorBase.cs @@ -47,13 +47,26 @@ public string Type /// public string Message { get; protected set; } + private Dictionary _data = []; + /// - /// Scans the inheriting type for all properties and returns them as a dictionary where property name is key, and the property value is the value. + /// In addition to data you add yourself, this also contains all properties on the inheriting type with property name as key and property value as value. + /// If you add a key to this dictionary, where the inheriting type has a property with the same name as your key, the value of that dictionary entry will be overwritten by the value of the property. /// /// + public Dictionary Data { get => GetData(); } + [Pure] - public Dictionary GetData() => GetType() + private Dictionary GetData() + { + var propValues = GetType() .GetProperties() .Where(p => p.DeclaringType != typeof(IError) && p.DeclaringType != typeof(ErrorBase)) .ToDictionary(d => d.Name, d => d.GetValue(this)); + foreach (var prop in propValues) + { + _data[prop.Key] = prop.Value; + } + return _data; + } } \ No newline at end of file From 540f95cdd31631e0340e58e549beb81c96171dd5 Mon Sep 17 00:00:00 2001 From: Mattias Nordqvist Date: Tue, 16 Jan 2024 18:59:14 +0100 Subject: [PATCH 4/5] oops --- Results/ErrorBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Results/ErrorBase.cs b/Results/ErrorBase.cs index b5f717b..0724895 100644 --- a/Results/ErrorBase.cs +++ b/Results/ErrorBase.cs @@ -57,7 +57,7 @@ public string Type public Dictionary Data { get => GetData(); } [Pure] - private Dictionary GetData() + public Dictionary GetData() { var propValues = GetType() .GetProperties() From 6c79827a4b041c91e5f7e42f0dd83b138d4710e0 Mon Sep 17 00:00:00 2001 From: Mattias Nordqvist Date: Tue, 16 Jan 2024 19:00:33 +0100 Subject: [PATCH 5/5] updated nugets --- Results.Tests/DotNetThoughts.Results.Tests.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Results.Tests/DotNetThoughts.Results.Tests.csproj b/Results.Tests/DotNetThoughts.Results.Tests.csproj index ad5a721..37ca95c 100644 --- a/Results.Tests/DotNetThoughts.Results.Tests.csproj +++ b/Results.Tests/DotNetThoughts.Results.Tests.csproj @@ -8,9 +8,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive