Skip to content

Commit b566199

Browse files
authored
Add support for Enums as optional properties (#23)
1 parent 3b29425 commit b566199

File tree

8 files changed

+529
-5
lines changed

8 files changed

+529
-5
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8+
### Added
9+
10+
- `Resource.Base`
11+
- Add optional feature flag to handle using Enums as optional properties.
12+
This requires setting the starting value of the Enum to 1 so it is
13+
initialized as 0. Fixes [Issue #22](https://github.com/dsccommunity/DscResource.Base/issues/22).
14+
15+
To use, set `$this.FeatureOptionalEnums = $true` in your class constructor.
16+
- `Clear-ZeroedEnumPropertyValue`
17+
- Added private function to remove enums with a zero value from a hashtable.
18+
819
### Changed
920

21+
- `ResourceBase`
22+
- Add feature flag to allow use of enums on optional properties.
1023
- `RequiredModules`
1124
- Add PlatyPS
1225
- `Resolve-Dependency`

Resolve-Dependency.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
AllowPrerelease = $false
44
WithYAML = $true # Will also bootstrap PowerShell-Yaml to read other config files
55

6-
#UseModuleFast = $true
6+
UseModuleFast = $true
77
#ModuleFastVersion = '0.1.2'
88
#ModuleFastBleedingEdge = $true
99

source/Classes/010.ResourceBase.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class ResourceBase
1919
# Property for derived class to set properties that should not be enforced.
2020
hidden [System.String[]] $ExcludeDscProperties = @()
2121

22+
# Property for derived class to enable Enums to be used as optional properties. The usable Enum values should start at value 1.
23+
hidden [System.Boolean] $FeatureOptionalEnums = $false
24+
2225
# Default constructor
2326
ResourceBase()
2427
{
@@ -227,6 +230,11 @@ class ResourceBase
227230
# Get the desired state, all assigned properties that has an non-null value.
228231
$desiredState = $this | Get-DscProperty -Attribute @('Key', 'Mandatory', 'Optional') -HasValue
229232

233+
if ($this.FeatureOptionalEnums)
234+
{
235+
$desiredState = $desiredState | Clear-ZeroedEnumPropertyValue
236+
}
237+
230238
$CompareDscParameterState = @{
231239
CurrentValues = $currentState
232240
DesiredValues = $desiredState
@@ -250,6 +258,11 @@ class ResourceBase
250258
# Get the properties that has a non-null value and is not of type Read.
251259
$desiredState = $this | Get-DscProperty -Attribute @('Key', 'Mandatory', 'Optional') -HasValue
252260

261+
if ($this.FeatureOptionalEnums)
262+
{
263+
$desiredState = $desiredState | Clear-ZeroedEnumPropertyValue
264+
}
265+
253266
$this.AssertProperties($desiredState)
254267
}
255268

source/Enum/1.Ensure.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
enum Ensure
77
{
8-
Present
8+
Present = 1
99
Absent
1010
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<#
2+
.SYNOPSIS
3+
Removes any properties from a hashable which have values that are
4+
type [System.Enum] and have an [System.Int32] value of 0.
5+
6+
.DESCRIPTION
7+
Removes any properties from a hashable which have values that are
8+
type [System.Enum] and have an [System.Int32] value of 0.
9+
10+
.PARAMETER InputObject
11+
The hashtable to be checked.
12+
13+
.EXAMPLE
14+
Clear-ZeroedEnumPropertyValue -InputObject $ht
15+
16+
.OUTPUTS
17+
[System.Collections.Hashtable]
18+
#>
19+
20+
function Clear-ZeroedEnumPropertyValue
21+
{
22+
[CmdletBinding()]
23+
[OutputType([System.Collections.Hashtable])]
24+
param (
25+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
26+
[System.Collections.Hashtable]
27+
$InputObject
28+
)
29+
30+
process
31+
{
32+
$result = @{}
33+
34+
foreach ($property in $InputObject.Keys)
35+
{
36+
$value = $InputObject.$property
37+
if ($value.IsEnum -and [System.Int32]$value.value__ -eq 0)
38+
{
39+
continue
40+
}
41+
42+
$result.$property = $value
43+
}
44+
45+
return $result
46+
}
47+
}

tests/Unit/Classes/Reason.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ BeforeDiscovery {
1010
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
1111
{
1212
# Redirect all streams to $null, except the error stream (stream 2)
13-
& "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null
13+
& "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null
1414
}
1515

1616
# If the dependencies has not been resolved, this will throw an error.

0 commit comments

Comments
 (0)