YamlObjectModel is a PowerShell framework for building typed object models that serialize to and from YAML or JSON.
It provides:
- Kubernetes-style
apiVersion,kind,metadata, andspecresource envelopes - module-qualified dynamic type dispatch
- nested typed-object resolution
- typed short-form loading when the caller knows the expected type
- backward compatibility with legacy raw specs and
kind/specdefinitions - base classes for YAML/JSON serialization
- save, reload, and file-loading support
Install-PSResource -Name YamlObjectModel
Import-Module -Name YamlObjectModelInstall-Module can also be used on systems using PowerShellGet.
An independently serialized YOM resource can be represented as:
apiVersion: example.synedgy.com/v1alpha1
kind: MyModule\MyResource
metadata:
Name: example
Labels:
environment: test
spec:
Property1: value1
Property2: value2The fields have distinct responsibilities:
| Field | Purpose |
|---|---|
apiVersion |
Version of the serialized resource contract |
kind |
Module-qualified class, function, or static dispatch action |
metadata |
Identity, labels, annotations, and discovery information |
spec |
Constructor input for the selected type |
apiVersion and metadata are optional. When unset, serialization emits the
legacy-compatible kind/spec shape.
using namespace System.Collections
using module YamlObjectModel
class MyResource : YOMSaveableBase
{
[string] $Property1
[string] $Property2
MyResource()
{
}
MyResource([IDictionary] $Definition)
{
$this.ResolveSpec($Definition)
}
}Modules can expose their classes through type accelerators, allowing a
module-qualified kind to import the module and construct the corresponding
type.
$resource = [YOMApiDispatcher]::DispatchSpec([ordered] @{
apiVersion = 'example.synedgy.com/v1alpha1'
kind = 'MyModule\MyResource'
metadata = [ordered] @{
Name = 'example'
}
spec = [ordered] @{
Property1 = 'value1'
Property2 = 'value2'
}
})The dispatcher imports MyModule when necessary, invokes the selected
constructor with spec, and preserves the resource envelope on the resulting
object.
When a command already knows the expected type, the input can omit kind:
apiVersion: example.synedgy.com/v1alpha1
metadata:
Name: example
spec:
Property1: value1
Property2: value2$resource = Get-YOMObject `
-Path .\resource.yml `
-DefaultType 'MyModule\MyResource'Legacy raw-spec typed input remains supported:
Property1: value1
Property2: value2Objects deriving from YOMBase provide:
$resource.ToYaml()
$resource.ToJson()
$resource.ToString()Objects deriving from YOMSaveableBase also provide:
$resource.SaveTo('.\resource.yml')
$resource.Save()
$resource.Reload()SavedAtPath is runtime state and is not added to the portable spec.
YamlObjectModel accepts:
- Kubernetes-style resource envelopes
- legacy
kind/specdefinitions - typed envelopes without
kindwhen a default type is supplied - legacy raw specs when a default type is supplied
This allows existing modules to migrate without rewriting all serialized objects at once.
.\build.ps1 -ResolveDependency -Tasks noop
.\build.ps1 -Tasks build
.\build.ps1 -Tasks testThis project has adopted this Code of Conduct.
Please review the common DSC Community contributing guidelines.