Skip to content

Commit 6d550d5

Browse files
author
James Brundage
committed
Updating Rest Transpiler (adding -UriParameterHelp and -UriParameterType) (#114)
1 parent cee342a commit 6d550d5

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

Transpilers/Rest.psx.ps1

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ $InvokeCommand = 'Invoke-RestMethod',
8989
[string]
9090
$InvokeParameterVariable = 'InvokeParams',
9191

92+
# A dictionary of help for uri parameters.
93+
[Alias('UrlParameterHelp')]
94+
[Collections.IDictionary]
95+
$UriParameterHelp,
96+
97+
# A dictionary of URI parameter types.
98+
[Alias('UrlParameterType')]
99+
[Collections.IDictionary]
100+
$UriParameterType,
101+
92102
# A dictionary or list of parameters for the body.
93103
[PSObject]
94104
$BodyParameter,
@@ -165,13 +175,30 @@ process {
165175
# The name of the parameter will be in the named capture ${Variable}.
166176
$parameterName = $match.Groups["Variable"].Value
167177
# The parameter type will be a string
168-
$parameterType = '[string]'
178+
$parameterType = if ($UriParameterType.$parameterName) {
179+
if ($UriParameterType.$parameterName -as [type]) {
180+
$UriParameterType.$parameterName
181+
}
182+
} else {
183+
'[string]'
184+
}
169185
# and we'll need to put it in the proper parameter set.
170186
$parameterAttribute = "[Parameter($(
171187
if (-not $match.Groups["IsOptional"].Value) {'Mandatory'}
172188
),ValueFromPipelineByPropertyName,ParameterSetName='$endpoint')]"
173189
# Combine these three pieces to create the parameter attribute.
174190
$uriParameters[$parameterName] = @(
191+
if ($UriParameterHelp -and $UriParameterHelp.$parameterName) {
192+
if ($UriParameterHelp.$parameterName -notmatch '^\<{0,1}\#' ) {
193+
if ($UriParameterHelp.$parameterName -match '[\r\n]') {
194+
"<# " + $UriParameterHelp.$parameterName + "#>"
195+
} else {
196+
"# " + $UriParameterHelp.$parameterName
197+
}
198+
} else {
199+
$UriParameterHelp.$parameterName
200+
}
201+
}
175202
$parameterAttribute
176203
$parameterType
177204
'$' + $parameterName
@@ -200,7 +227,7 @@ process {
200227
if ($uriParamBlock.Ast.ParamBlock.Parameters) {
201228
# Carry on the begin block from this command (this is a neat trick)
202229
[scriptblock]::Create($myCmd.ScriptBlock.Ast.BeginBlock.Extent.ToString())
203-
} else { {} }
230+
} else { { begin { $myCmd = $MyInvocation.MyCommand }} }
204231

205232
# Next, collect the names of bodyParameters, queryParameters, and uriParameters.
206233
$bodyParameterNames =
@@ -215,6 +242,10 @@ process {
215242
$RestScript = @(
216243
# Start with the underlying script block
217244
$ScriptBlock
245+
246+
# Then include the begin block from this command (or declare myCmd)
247+
$myBeginBlock
248+
218249
# Then declare the initial variables.
219250
[scriptblock]::Create((@"
220251
process {
@@ -249,9 +280,7 @@ process {
249280
# If we had any uri parameters
250281
if ($uriParameters.Count) {
251282
# Add the uri parameter block
252-
$uriParamBlock
253-
# And add the begin block from this script
254-
$myBeginBlock
283+
$uriParamBlock
255284
# Then add a bit to process {} to extract out the URL
256285
{
257286
process {
@@ -314,9 +343,13 @@ process {
314343
$QueryParams = [Ordered]@{}
315344
foreach ($QueryParameterName in $QueryParameterNames) {
316345
if ($PSBoundParameters.ContainsKey($QueryParameterName)) {
317-
$QueryParams[$QueryParameterName] = $PSBoundParameters[$QueryParameterName]
346+
$QueryParams[$QueryParameterName] = $PSBoundParameters[$QueryParameterName]
318347
}
319348
}
349+
}
350+
}
351+
{
352+
process {
320353
if ($invokerCommandinfo.Parameters['QueryParameter'] -and
321354
$invokerCommandinfo.Parameters['QueryParameter'].ParameterType -eq [Collections.IDictionary]) {
322355
$invokerCommandinfo.QueryParameter = $QueryParams

0 commit comments

Comments
 (0)