Nuke.Cola
Loading...
Searching...
No Matches
Cola.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading.Tasks;
5using YamlDotNet.Core.Tokens;
6
7namespace Nuke.Cola;
8
9public static class Cola
10{
11 /// <summary>
12 /// Syntax simplifier when `new()` cannot be used for dictionaries
13 /// </summary>
14 public static Dictionary<Key, Value> MakeDictionary<Key, Value>(params (Key key, Value value)[] items)
15 where Key : notnull
16 => items.ToDictionary(i => i.key, i => i.value);
17
18 /// <summary>
19 /// Merge two dictionaries safely
20 /// </summary>
21 public static IReadOnlyDictionary<Key, Value>? Merge<Key, Value>(
22 this IReadOnlyDictionary<Key, Value>? a,
23 IReadOnlyDictionary<Key, Value>? b
24 ) where Key : notnull
25 {
26 if (a == null) return b;
27 if (b == null) return a;
28
29 var result = a.ToDictionary();
30 foreach (var i in b)
31 {
32 result[i.Key] = i.Value;
33 }
34
35 return result;
36 }
37
38 /// <summary>
39 /// Merge two dictionaries safely
40 /// </summary>
41 public static IDictionary<Key, Value>? MergeMutable<Key, Value>(
42 this IDictionary<Key, Value>? a,
43 IDictionary<Key, Value>? b
44 ) where Key : notnull
45 {
46 if (a == null) return b;
47 if (b == null) return a;
48
49 var result = a.ToDictionary();
50 foreach (var i in b)
51 {
52 result[i.Key] = i.Value;
53 }
54
55 return result;
56 }
57
58 /// <summary>
59 /// Simply invoke a task on an object and return that same object
60 /// </summary>
61 public static T With<T>(this T self, Action<T> operation)
62 {
63 operation(self);
64 return self;
65 }
66
67 /// <summary>
68 /// Simply invoke a task on an object which may be null and return that same object
69 /// </summary>
70 public static T? WithNullable<T>(this T? self, Action<T>? operation)
71 {
72 if (self != null)
73 operation?.Invoke(self);
74 return self;
75 }
76}
static ? T WithNullable< T >(this T? self, Action< T >? operation)
Simply invoke a task on an object which may be null and return that same object.
Definition Cola.cs:70
static ? IDictionary< Key, Value > MergeMutable< Key, Value >(this IDictionary< Key, Value >? a, IDictionary< Key, Value >? b)
Merge two dictionaries safely.
Definition Cola.cs:41
static T With< T >(this T self, Action< T > operation)
Simply invoke a task on an object and return that same object.
Definition Cola.cs:61
static ? IReadOnlyDictionary< Key, Value > Merge< Key, Value >(this IReadOnlyDictionary< Key, Value >? a, IReadOnlyDictionary< Key, Value >? b)
Merge two dictionaries safely.
Definition Cola.cs:21
static Dictionary< Key, Value > MakeDictionary< Key, Value >(params(Key key, Value value)[] items)
Syntax simplifier when new() cannot be used for dictionaries.