private/New-SfMetadataModel.ps1
|
function New-SfMetadataModel { <# .SYNOPSIS Creates a metadata model skeleton, pre-initialized from the data dictionary schema. #> [CmdletBinding()] param ( # Path to the directory containing the schema CSV files. # Defaults to the module's own static/data-dictionary-schema directory. [string] $SchemaPath, # Path to the directory containing the validation list TXT files. # Defaults to the module's own static/data-dictionary-validations directory. [string] $ValidationsPath ) $ErrorActionPreference = 'Stop' [bool] $IsDebug = $DebugPreference -ne 'SilentlyContinue' [bool] $IsVerbose = $VerbosePreference -ne 'SilentlyContinue' # --- resolve default paths relative to module root if (-not $SchemaPath) { $SchemaPath = Join-Path $Script:ModuleRoot 'static/data-dictionary-schema' } if (-not $ValidationsPath) { $ValidationsPath = Join-Path $Script:ModuleRoot 'static/data-dictionary-validations' } Write-Debug "SchemaPath: $SchemaPath" Write-Debug "ValidationsPath: $ValidationsPath" # --- load schema: one entry per known table name [hashtable] $Schema = @{} foreach ($File in (Get-ChildItem -Path (Join-Path $SchemaPath '*') -Include '*.csv')) { [string] $TableName = $File.BaseName [array] $ColumnDefs = Import-Csv -Path $File.FullName -Header 'Type', 'Name', 'Validation' [PSCustomObject[]] $Columns = foreach ($Col in $ColumnDefs) { [string[]] $ValidValues = $null if (-not [string]::IsNullOrEmpty($Col.Validation) -and $Col.Validation -ne '-') { [string] $ValidationFile = Join-Path $ValidationsPath "$($Col.Validation).txt" [string[]] $ValidValues = Get-Content -Path $ValidationFile | ForEach-Object { ($_ -replace '#.*$', '').Trim() } | Where-Object { $_ -ne '' } } [PSCustomObject]@{ Type = $Col.Type Name = $Col.Name ValidValues = $ValidValues } } $Schema[$TableName] = $Columns } # --- initialize Tables: one empty array per known table, in alphabetical order [hashtable] $Tables = @{} foreach ($TableName in ($Schema.Keys | Sort-Object)) { $Tables[$TableName] = @() } # --- load static metadata types map [string] $MetadataTypesPath = Join-Path $Script:ModuleRoot 'static/metadata-types.json' Write-Debug "MetadataTypesPath: $MetadataTypesPath" [hashtable] $MetadataTypes = @{} if (Test-Path $MetadataTypesPath) { [array] $MetadataTypesList = Get-Content $MetadataTypesPath | ConvertFrom-Json foreach ($Mdt in $MetadataTypesList) { $MetadataTypes[$Mdt.MetadataType] = $Mdt } } $Model = [PSCustomObject]@{ # --- schema: column definitions per table name Schema = $Schema # --- tables: imported rows per table name, keyed by table name e.g. '_Fields' Tables = $Tables # --- static metadata types reference MetadataTypes = $MetadataTypes } return $Model } |