Skip to content

Repository files navigation

YamlObjectModel

Build Status codecov PowerShell Gallery (with prereleases) PowerShell Gallery License

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, and spec resource 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/spec definitions
  • base classes for YAML/JSON serialization
  • save, reload, and file-loading support

Install

Install-PSResource -Name YamlObjectModel
Import-Module -Name YamlObjectModel

Install-Module can also be used on systems using PowerShellGet.

Resource envelope

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: value2

The 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.

Define a YOM class

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.

Dispatch a resource

$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.

Typed short form

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: value2

Serialize and save

Objects 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.

Compatibility

YamlObjectModel accepts:

  1. Kubernetes-style resource envelopes
  2. legacy kind/spec definitions
  3. typed envelopes without kind when a default type is supplied
  4. legacy raw specs when a default type is supplied

This allows existing modules to migrate without rewriting all serialized objects at once.

Documentation

Build and test

.\build.ps1 -ResolveDependency -Tasks noop
.\build.ps1 -Tasks build
.\build.ps1 -Tasks test

Code of Conduct

This project has adopted this Code of Conduct.

Contributing

Please review the common DSC Community contributing guidelines.

About

Creates a common object model for Yaml de/serialization of custom PowerShell classes.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages