Major refactor - PSCustomObjects and powershell classes #199
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is an attempt to move away from the thin-wrapper mode we've been operating under for the past 10 years (time flies). In this PR we add a number of important things:
Testand another calledtestin your YAML, only one key ca be stored in aPSCustomObject.PSCustomObjects
PSCustomObjects get you most of what class based models can, but without having to create a bunch of classes and model each and every property inside of them (although it is a good idea if you really want to be type safe and have proper round-tripping).
Here is a quick example of how the module works now with PSCustomObjects.
Define a Yaml:
Convert to PSCustomObject:
Print out the object:
Convert back to yaml:
Check types of some of the keys. Bellow you can see that both the port which has a scalar style of
Plainand themax-connectionsthat has a scalar style ofDoubleQuoted, are cast toint32.You will notice a couple of important things:
To work with PSCustomObjects, we also added some helper commandlets that allow you to change comments and scalar styles:
Now if we serialize again:
You can also get the value of the comment:
Powershell Classes
Most statically typed languages have some way to model the yaml so that you can reliably unmarshal the data in their proper types, save metadata and round-trip back to yaml. We can do something similar in powershell as well.
To that end, we have a number of new base classes and attributes we can use:
-Asflag ofConvertFrom-Yamlneed to inherit from this classThere is one quirk with classes I want to address early on. We define these base classes in the
PowerShellYamlnamespace. Once you import thepowershell-yamlmodule, that namespace becomes available.To use a namespace in PowerShell, you will use the
usingstanza. The problem is that anyusingstanzas you use, need to be the first thing in any script. So there is a chicken and egg situation happening here. As a result, we need to define our classes in a separate file and dot-source them.For example, first we define our
classes.ps1:Then we define our
test.ps1:If you print out the value:
To change the comment on a key:
Now if we serialize back to yaml:
There are a lot more knobs and features, most of which can be seen in action in the
examplesfolder.