|
| 1 | +# PSFHashtables |
| 2 | + |
| 3 | +[Back to utility](http://psframework.org/documentation/documents/psframework/utility.html) |
| 4 | + |
| 5 | +## Synopsis |
| 6 | + |
| 7 | +PSFramework Hashtables - `[PsfHashtable]` - are an extension / improvement over the default `[Hashtable]`. |
| 8 | +They can do everything a regular hashtable can do, but they can also offer alternative options on what to do, when asking for a key _not_ already included in the hashtable. |
| 9 | + |
| 10 | +## Description |
| 11 | + |
| 12 | +The `[PsfHashtable]` data-type was added as a means to make hashtables more powerful and flexible, in many cases replacing the need for switch-statements. |
| 13 | +This can help simplify the code layout significantly. |
| 14 | + |
| 15 | +> Example: The simple hashtable |
| 16 | +
|
| 17 | +```powershell |
| 18 | +$organizations = @{ |
| 19 | + LondonHR = 'London' |
| 20 | + LondonSales = 'London' |
| 21 | + LondonDev = 'London' |
| 22 | + NewYorkHR = 'New York' |
| 23 | + NewYorkSales = 'New York' |
| 24 | + NewYorkDev = 'New York' |
| 25 | +} |
| 26 | +
|
| 27 | +$users | Select-Object Name, Mail, @{ |
| 28 | + Name = 'Users' |
| 29 | + Expression = { |
| 30 | + if ($organizations[$_.Organization]) { |
| 31 | + $organizations[$_.Organization] |
| 32 | + } |
| 33 | + else { $_.Organization} |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +> Example: PsfHashtable |
| 39 | +
|
| 40 | +```powershell |
| 41 | +$organizations = [PsfHashtable]@{ |
| 42 | + LondonHR = 'London' |
| 43 | + LondonSales = 'London' |
| 44 | + LondonDev = 'London' |
| 45 | + NewYorkHR = 'New York' |
| 46 | + NewYorkSales = 'New York' |
| 47 | + NewYorkDev = 'New York' |
| 48 | +} |
| 49 | +# If key not known: Return key |
| 50 | +$organizations.EnablePassthru() |
| 51 | +
|
| 52 | +$users | Select-Object Name, Mail, @{ |
| 53 | + Name = 'Users' |
| 54 | + Expression = { $organizations[$_.Organization] } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Special Features |
| 59 | + |
| 60 | +So, after luring you in with a simple example, what all can the `[PsfHashtable]` do for you? |
| 61 | + |
| 62 | +### PassThru |
| 63 | + |
| 64 | +As shown in the previous example, a `[PsfHashtable]` can be configured to pass through the key, if it is not yet known: |
| 65 | + |
| 66 | +```powershell |
| 67 | +$mapping = [PsfHashtable]@{ |
| 68 | + Answer = 42 |
| 69 | + Conspiracy = 23 |
| 70 | +} |
| 71 | +$mapping.EnablePassthru() |
| 72 | +
|
| 73 | +$mapping["Answer"] # 42 |
| 74 | +$mapping.Answer # 42 |
| 75 | +$mapping["Foo"] # Foo |
| 76 | +$mapping.Foo # <nothing> |
| 77 | +``` |
| 78 | + |
| 79 | +> Note: One limitation with extending hashtables: The features on unknown keys _only_ work, when using the _index_ notation (e.g. `$mapping["Foo"]`), not the _property_ notation (e.g. `$mapping.Foo`). |
| 80 | +
|
| 81 | +### Default Value |
| 82 | + |
| 83 | +Rather than passing through the key, you can also provide a default value to return: |
| 84 | + |
| 85 | +```powershell |
| 86 | +$mapping = [PsfHashtable]@{ |
| 87 | + Answer = 42 |
| 88 | + Conspiracy = 23 |
| 89 | +} |
| 90 | +$mapping.SetDefaultValue(1) |
| 91 | +
|
| 92 | +$mapping["Answer"] # 42 |
| 93 | +$mapping.Answer # 42 |
| 94 | +$mapping["Foo"] # 1 |
| 95 | +$mapping.Foo # <nothing> |
| 96 | +``` |
| 97 | + |
| 98 | +> Note: One limitation with extending hashtables: The features on unknown keys _only_ work, when using the _index_ notation (e.g. `$mapping["Foo"]`), not the _property_ notation (e.g. `$mapping.Foo`). |
| 99 | +
|
| 100 | +### Dynamic Value |
| 101 | + |
| 102 | +A third alternative is to provide a custom scriptblock that gets executed on unknown keys: |
| 103 | + |
| 104 | +```powershell |
| 105 | +$mapping = [PsfHashtable]@{ |
| 106 | + Answer = 42 |
| 107 | + Conspiracy = 23 |
| 108 | +} |
| 109 | +$mapping.SetCalculator({ $_ * 2 }) |
| 110 | +
|
| 111 | +$mapping["Answer"] # 42 |
| 112 | +$mapping.Answer # 42 |
| 113 | +$mapping["Foo"] # FooFoo |
| 114 | +$mapping.Foo # <nothing> |
| 115 | +``` |
| 116 | + |
| 117 | +> Note: One limitation with extending hashtables: The features on unknown keys _only_ work, when using the _index_ notation (e.g. `$mapping["Foo"]`), not the _property_ notation (e.g. `$mapping.Foo`). |
| 118 | +
|
| 119 | +[Back to utility](http://psframework.org/documentation/documents/psframework/utility.html) |
0 commit comments