-
Notifications
You must be signed in to change notification settings - Fork 29
/
Resolve-JsonPointer.ps1
180 lines (157 loc) · 5.83 KB
/
Resolve-JsonPointer.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<#
.SYNOPSIS
Returns matching JSON Pointer paths, given a JSON Pointer path with wildcards.
.INPUTS
System.String containing JSON, or
System.Collections.Hashtable parsed from JSON, or
System.Management.Automation.PSObject parsed from JSON.
.OUTPUTS
System.String of the full JSON Pointer matched.
.FUNCTIONALITY
Json
.LINK
https://www.rfc-editor.org/rfc/rfc6901
.LINK
ConvertFrom-Json
.EXAMPLE
'{a:1}' |Resolve-JsonPointer.ps1 /*
/a
.EXAMPLE
'[8, 7, 6]' |Resolve-JsonPointer.ps1 /-
/3
.EXAMPLE
Resolve-JsonPointer.ps1 /powershell.*.preset -Path ./.vscode/settings.json
/powershell.codeFormatting.preset
.EXAMPLE
'{"a":1, "b": {"ZZ/ZZ": {"AD~BC": 7}}}' |Resolve-JsonPointer.ps1 /*/ZZ?ZZ/AD?BC
/b/ZZ~1ZZ/AD~0BC
.EXAMPLE
'{"a":1, "b": {"ZZ/ZZ": {"AD~BC": 7}}}' |Resolve-JsonPointer.ps1 /[bc]/ZZ?ZZ
/b/ZZ~1ZZ
.EXAMPLE
'{"a":1, "b": {"ZZ/ZZ": {"AD~BC": [7, 8, 9]}}}' |ConvertFrom-Json |Resolve-JsonPointer.ps1 /?/ZZ*/*BC/-
/b/ZZ~1ZZ/AD~0BC/3
.EXAMPLE
Resolve-JsonPointer.ps1 /* -Path .\test\data\sample-openapi.json -IncludePath
Path Pointer
---- -------
A:\Scripts\test\data\sample-openapi.json /openapi
A:\Scripts\test\data\sample-openapi.json /info
A:\Scripts\test\data\sample-openapi.json /tags
A:\Scripts\test\data\sample-openapi.json /paths
A:\Scripts\test\data\sample-openapi.json /components
#>
#Requires -Version 7
[CmdletBinding()][OutputType([string])] Param(
<#
The full path name of the property to get, as a JSON Pointer, modified to support wildcards:
~0 = ~ ~1 = / ~2 = ? ~3 = * ~4 = [
#>
[Parameter(Position=0)][Alias('Name')][AllowEmptyString()][ValidatePattern('\A(?:|/(?:[^~]|~[0-4])*)\z')]
[string] $JsonPointer = '',
# The JSON (string or parsed object/hashtable) to get the value from.
[Parameter(ParameterSetName='InputObject',ValueFromPipeline=$true)] $InputObject,
# A JSON file to update.
[Parameter(ParameterSetName='Path',Mandatory=$true)][string] $Path,
# Indicates that the source file path should be included in the output, if available.
[switch] $IncludePath
)
Begin
{
[string[]] $jsonpath = switch($JsonPointer) { '' {,@()}
default {,@($_ -replace '\A/' -replace '~4','[[]' -replace '~3','[*]' -replace '~2','[?]' -split '/' -replace '~1','/' -replace '~0','~')} }
function Resolve-Segment
{
[CmdletBinding()] Param(
[Parameter(Position=0)] $InputObject,
[Parameter(Position=1)][string] $Segment,
[Parameter(Position=2)][string[]] $Segments,
[Parameter(Position=3)][string] $JsonPointer
)
$pointer = "$JsonPointer/$($segment -replace '~','~0' -replace '/','~1')"
if($InputObject -is [array])
{
if($Segment -eq '-') {return Resolve-Pointer -InputObject $null -Segments $Segments -JsonPointer "$JsonPointer/$($InputObject.Count)"}
elseif(![int]::TryParse($Segment,[ref]$Segment)) {return}
elseif($InputObject.Length -le $Segment) {return}
else {return Resolve-Pointer -InputObject ($InputObject[$Segment]) -Segments $Segments -JsonPointer $pointer}
}
elseif($InputObject -is [Collections.IDictionary])
{
if(!$InputObject.ContainsKey($Segment)) {return}
else {return Resolve-Pointer -InputObject ($InputObject[$Segment]) -Segments $Segments -JsonPointer $pointer}
}
else
{
return $InputObject.PSObject.Properties.Match($Segment) |
Select-Object -ExpandProperty Value |
Resolve-Pointer -Segments $Segments -JsonPointer $pointer
}
}
function Resolve-Wildcard
{
[CmdletBinding()] Param(
[Parameter(Position=0)] $InputObject,
[Parameter(Position=1)][string] $Segment,
[Parameter(Position=2)][string[]] $Segments,
[Parameter(Position=3)][string] $JsonPointer
)
if($InputObject -is [array])
{
return 0..($InputObject.Length-1) -like $segment |ForEach-Object {
$pointer = "$JsonPointer/$_"
Resolve-Pointer -InputObject $InputObject[$_] -Segments $Segments -JsonPointer $pointer}
}
elseif($InputObject -is [Collections.IDictionary])
{
return $InputObject.Keys -like $segment |ForEach-Object {
$pointer = "$JsonPointer/$($_ -replace '~','~0' -replace '/','~1')"
Resolve-Pointer -InputObject $InputObject[$_] -Segments $Segments -JsonPointer $pointer}
}
else
{
return $InputObject.PSObject.Properties.Match($segment) |ForEach-Object {
$pointer = "$JsonPointer/$($_.Name -replace '~','~0' -replace '/','~1')"
Resolve-Pointer -InputObject $_.Value -Segments $Segments -JsonPointer $pointer}
}
}
filter Resolve-Pointer
{
[CmdletBinding()] Param(
[Parameter(ValueFromPipeline=$true)] $InputObject,
[string[]] $Segments,
[string] $JsonPointer = ''
)
if(!$Segments.Count) {return $JsonPointer}
$segment,$Segments = $Segments
if($null -eq $Segments) {[string[]]$Segments = @()}
if($segment -match '[?*[]') {Resolve-Wildcard -InputObject $InputObject -Segment $segment -Segments $Segments -JsonPointer $JsonPointer}
else {Resolve-Segment -InputObject $InputObject -Segment $segment -Segments $Segments -JsonPointer $JsonPointer}
}
}
Process
{
if($Path)
{
if($IncludePath)
{
return Resolve-Path -Path $Path -PipelineVariable file |
Get-Content -Raw |
ForEach-Object {Resolve-Pointer -InputObject ($_ |ConvertFrom-Json -AsHashtable) -Segments $jsonpath} |
ForEach-Object {[pscustomobject]@{Path=$file.Path; Pointer=$_}}
}
else
{
return Resolve-Path -Path $Path |
Get-Content -Raw |
ForEach-Object {Resolve-Pointer -InputObject ($_ |ConvertFrom-Json -AsHashtable) -Segments $jsonpath}
}
}
if($null -eq $InputObject) {return}
if($InputObject -is [string])
{
return Resolve-Pointer -InputObject ($InputObject |ConvertFrom-Json -AsHashtable) -Segments $jsonpath
}
if(!$jsonpath.Length) {return $JsonPointer}
return Resolve-Pointer -InputObject $InputObject -Segments $jsonpath
}