Created
December 9, 2018 11:01
-
-
Save AndrewNewcomb/7270c16c085fe6c9354fdad240d87e38 to your computer and use it in GitHub Desktop.
Powershell script that given a json file creates files with each file having a different property or element set to null.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Given a json file creates files with each file having a different property or element set to null. | |
# Useage: | |
# Nullify -sourceFile "C:\afolder\afile.json" -targetPath "C:\anotherfolder" | |
# | |
Function Nullify { | |
Param( | |
[Parameter(Mandatory=$true, HelpMessage="The source file.")][string]$sourceFile, ` | |
[Parameter(Mandatory=$true, HelpMessage="The target path to write the output files to.")][string]$targetPath ) | |
Function Fuzz{ | |
Param($entireHashTable, $nullifyNodeNumber) | |
Function New-Counter ($increment=1) | |
{ | |
$count=0; | |
{ | |
$script:count += $increment | |
$count | |
}.GetNewClosure() | |
} | |
$incrementNodeCounter = New-Counter | |
Function FuzzIf{ | |
Param([int]$nodeNumber) | |
if($nodeNumber -eq $nullifyNodeNumber){ | |
Write-Debug "WANT TO NULLIFY THIS ONE" | |
$true | |
} else { | |
$false | |
} | |
} | |
Function ProcessCollection { | |
Param($collection) | |
if($collection.Keys.Count -gt 0) { | |
$clonedKeys = $collection.Keys.Clone() | |
$clonedKeys | ForEach-Object { | |
$itemKey = $_ | |
$itemValue = $collection[$_] | |
$nodeNumber = & $incrementNodeCounter | |
if($itemValue -eq $null){ | |
Write-Debug "$nodeNumber - $itemKey : NULL" | |
} else { | |
if(FuzzIf $nodeNumber -eq $true){ | |
$collection[$itemKey] = $null | |
Write-Debug "$nodeNumber ............. $itemKey" | |
} else { | |
$valueType = $itemValue.GetType() | |
if($valueType -like 'System.Collections*'){ | |
Write-Debug "$nodeNumber - $itemKey : recurseA" | |
ProcessCollection $itemValue | |
} elseif($valueType -like '*]') { | |
Write-Debug "$nodeNumber - $itemKey : recurseB" | |
ProcessObject $itemValue | |
} else { | |
Write-Debug "$nodeNumber - $itemKey : $itemValue" | |
} | |
} | |
} | |
} | |
} | |
} | |
Function ProcessObject { | |
Param($collection) | |
#$collectionType = $collection.GetType() | |
for($i = 0; $i -lt $collection.Length; $i++) { | |
$nodeNumber = & $incrementNodeCounter | |
$item = $collection[$i] | |
if($item -eq $null){ | |
# no op as is already null | |
} else { | |
$valueType = $item.GetType() | |
if(FuzzIf $nodeNumber){ | |
$collection[$i] = $null | |
Write-Debug "$nodeNumber ............. $($collection[$i])" | |
} else { | |
if($valueType -like 'System.Collections*'){ | |
Write-Debug "$item : recurseC" | |
ProcessCollection $item | |
} elseif($valueType -like '*]') { | |
Write-Debug "$item : recurseD" | |
ProcessObject $item | |
} else { | |
Write-Debug "$nodeNumber - $item" | |
} | |
} | |
} | |
} | |
} | |
Function StartFuzz { | |
Param($hashTable) | |
$nodeNumber = 0 | |
ProcessCollection $hashTable 0 | |
$resultJson = $hashTable | ConvertTo-Json -Depth 10 | |
$resultJson | |
} | |
$result = StartFuzz $entireHashTable | |
$nodeNumber = & $incrementNodeCounter | |
Write-Debug "Nodes: $nodeNumber" | |
if($nullifyNodeNumber -le 0){($nodeNumber-1)} | |
else {$result} | |
} | |
Function JsonToHashTable { | |
Param($json) | |
Add-Type -AssemblyName System.Web.Extensions | |
(New-Object System.Web.Script.Serialization.JavaScriptSerializer).Deserialize($json, [System.Collections.Hashtable]) | |
} | |
Function PadToFit{ | |
Param ($index, $max) | |
$indexAsString = "$index" | |
$maxAsString = "$max" | |
$indexAsString.PadLeft($maxAsString.Length, '0') | |
} | |
Function GetSuffix { | |
Param ($startdatetime, $index, $max) | |
$suffix = $startdatetime.ToString('yyMMdd.HHmmss') | |
if($max -eq 1){ | |
"$suffix" | |
} else { | |
$paddedIndex = PadToFit $index $max | |
"$suffix.$paddedIndex" | |
} | |
} | |
Function GetFileName { | |
Param ($sourceFileNamePart, $sourceFileExtension, $suffix) | |
"$sourceFileNamePart.$suffix$sourceFileExtension" | |
} | |
$json = Get-Content -Path $sourceFile -Raw | |
$startAt = Get-Date | |
$sourceFileNamePart = [System.IO.Path]::GetFileNameWithoutExtension($sourceFile) | |
$sourceFileExtension = [System.IO.Path]::GetExtension($sourceFile) | |
$numNodes = Fuzz (JsonToHashTable($json)) 0 | |
Write-Host "Found $numNodes nodes" | |
for($n=1; $n -le $numNodes; $n++){ | |
$fuzzed = Fuzz (JsonToHashTable($json)) $n | |
$suffix = GetSuffix $startAt $n $numNodes | |
$targetFileName = GetFileName $sourceFileNamePart $sourceFileExtension $suffix | |
$targetFile = Join-Path -Path $targetPath -ChildPath $targetFileName | |
Write-Host $targetFile | |
Set-Content -Path $targetFile -Value $fuzzed | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment