From 7abae1f2627abb89123941d258fccf8f3dfb39fb Mon Sep 17 00:00:00 2001 From: Pratik Chowdhury Date: Thu, 18 Nov 2021 05:12:47 -0800 Subject: [PATCH 01/10] feat: simplify and switch to new JSONPath 1. Extract multiple versions 2. Reverse versions, especially if regex is absent 3. Support returning single value even if multiple versions were found This is probably required with regex mode on 4. Simplify data extraction using JValue over JObject/JArray combination. Saves a few function calls --- lib/json.ps1 | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/json.ps1 b/lib/json.ps1 index f3b6c97f1a..67d9fbecc7 100644 --- a/lib/json.ps1 +++ b/lib/json.ps1 @@ -92,31 +92,33 @@ function ConvertToPrettyJson { } } -function json_path([String] $json, [String] $jsonpath, [Hashtable] $substitutions) { +function json_path([String] $json, [String] $jsonpath, [Hashtable] $substitutions, [bool] $reverse, [bool] $returnsingle) { Add-Type -Path "$psscriptroot\..\supporting\validator\bin\Newtonsoft.Json.dll" if ($null -ne $substitutions) { $jsonpath = substitute $jsonpath $substitutions ($jsonpath -like "*=~*") } try { - $obj = [Newtonsoft.Json.Linq.JObject]::Parse($json) + $obj = [Newtonsoft.Json.Linq.JValue]::Parse($json) } catch [Newtonsoft.Json.JsonReaderException] { - try { - $obj = [Newtonsoft.Json.Linq.JArray]::Parse($json) - } catch [Newtonsoft.Json.JsonReaderException] { - return $null - } + return $null } - try { - try { - $result = $obj.SelectToken($jsonpath, $true) - } catch [Newtonsoft.Json.JsonException] { - return $null + $result = $obj.SelectTokens($jsonpath, $true) + if ($reverse) { + # Return versions in reverse order + $result = [System.Linq.Enumerable]::Reverse($result) } - return $result.ToString() - } catch [System.Management.Automation.MethodInvocationException] { + if ($returnsingle) { + # Extract First value + $result = [System.Linq.Enumerable]::First($result) + # Convert first value to string + $result = $result.ToString() + } else { + $result = "$([String]::Join("\n",$result))" + } + return $result + } catch [Exception] { write-host -f DarkRed $_ - return $null } return $null From 6f6049118a7ddfb63b9a2b51045fb13a695e574b Mon Sep 17 00:00:00 2001 From: Pratik Chowdhury Date: Thu, 18 Nov 2021 05:14:36 -0800 Subject: [PATCH 02/10] feat(checkver): add support for updated json_path --- bin/checkver.ps1 | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bin/checkver.ps1 b/bin/checkver.ps1 index ea4a92cac6..918cc38ecb 100644 --- a/bin/checkver.ps1 +++ b/bin/checkver.ps1 @@ -213,7 +213,14 @@ while ($in_progress -gt 0) { } if ($jsonpath) { - $ver = json_path $page $jsonpath + # Return only a single value if regex is absent + $json_path_return_single = [String]::IsNullOrEmpty($regex) + # If reverse is ON and regex is ON, + # Then reverse would have no effect because regex handles reverse + # on its own + # So in this case we have to disable reverse + $json_path_reverse = $reverse -and [String]::IsNullOrEmpty($regex) + $ver = json_path $page $jsonpath $null $json_path_reverse $json_path_return_single if (!$ver) { $ver = json_path_legacy $page $jsonpath } From feeeb1fd567cc2a1c97a613e5b1511bb2de1df15 Mon Sep 17 00:00:00 2001 From: Pratik Chowdhury Date: Fri, 19 Nov 2021 22:10:23 -0800 Subject: [PATCH 03/10] chore(json.ps1):Rename to single Co-authored-by: Hsiao-nan Cheung --- bin/checkver.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/checkver.ps1 b/bin/checkver.ps1 index 918cc38ecb..d7fab24084 100644 --- a/bin/checkver.ps1 +++ b/bin/checkver.ps1 @@ -214,7 +214,7 @@ while ($in_progress -gt 0) { if ($jsonpath) { # Return only a single value if regex is absent - $json_path_return_single = [String]::IsNullOrEmpty($regex) + $single = [String]::IsNullOrEmpty($regex) # If reverse is ON and regex is ON, # Then reverse would have no effect because regex handles reverse # on its own From 650b3a05197e41292ecbeaa40a2558adc19370c4 Mon Sep 17 00:00:00 2001 From: Pratik Chowdhury Date: Fri, 19 Nov 2021 22:10:45 -0800 Subject: [PATCH 04/10] chore(json.ps1):Rename reverse Co-authored-by: Hsiao-nan Cheung --- bin/checkver.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/checkver.ps1 b/bin/checkver.ps1 index d7fab24084..32a6f10c38 100644 --- a/bin/checkver.ps1 +++ b/bin/checkver.ps1 @@ -219,8 +219,8 @@ while ($in_progress -gt 0) { # Then reverse would have no effect because regex handles reverse # on its own # So in this case we have to disable reverse - $json_path_reverse = $reverse -and [String]::IsNullOrEmpty($regex) - $ver = json_path $page $jsonpath $null $json_path_reverse $json_path_return_single + $reverse = $reverse -and $single + $ver = json_path $page $jsonpath $null $reverse $single if (!$ver) { $ver = json_path_legacy $page $jsonpath } From e59750cb36ce312c50c33d70f9f91c77988bb55d Mon Sep 17 00:00:00 2001 From: Pratik Chowdhury Date: Fri, 19 Nov 2021 22:11:05 -0800 Subject: [PATCH 05/10] chore(json.ps1):rename Co-authored-by: Hsiao-nan Cheung --- lib/json.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/json.ps1 b/lib/json.ps1 index 67d9fbecc7..cdb34af718 100644 --- a/lib/json.ps1 +++ b/lib/json.ps1 @@ -92,7 +92,7 @@ function ConvertToPrettyJson { } } -function json_path([String] $json, [String] $jsonpath, [Hashtable] $substitutions, [bool] $reverse, [bool] $returnsingle) { +function json_path([String] $json, [String] $jsonpath, [Hashtable] $substitutions, [Boolean] $reverse, [Boolean] $single) { Add-Type -Path "$psscriptroot\..\supporting\validator\bin\Newtonsoft.Json.dll" if ($null -ne $substitutions) { $jsonpath = substitute $jsonpath $substitutions ($jsonpath -like "*=~*") From c55c1e1911d2db5a922bc3b361076fbbb324d8bd Mon Sep 17 00:00:00 2001 From: Pratik Chowdhury Date: Fri, 19 Nov 2021 22:11:17 -0800 Subject: [PATCH 06/10] Update lib/json.ps1 Co-authored-by: Hsiao-nan Cheung --- lib/json.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/json.ps1 b/lib/json.ps1 index cdb34af718..68cbad1730 100644 --- a/lib/json.ps1 +++ b/lib/json.ps1 @@ -108,7 +108,7 @@ function json_path([String] $json, [String] $jsonpath, [Hashtable] $substitution # Return versions in reverse order $result = [System.Linq.Enumerable]::Reverse($result) } - if ($returnsingle) { + if ($single) { # Extract First value $result = [System.Linq.Enumerable]::First($result) # Convert first value to string From 71809bbedfe8aec40ad946e0f6c582ae3df10a1e Mon Sep 17 00:00:00 2001 From: Pratik Chowdhury Date: Fri, 19 Nov 2021 22:11:37 -0800 Subject: [PATCH 07/10] Update lib/json.ps1 Co-authored-by: Hsiao-nan Cheung --- lib/json.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/json.ps1 b/lib/json.ps1 index 68cbad1730..faf3883cc8 100644 --- a/lib/json.ps1 +++ b/lib/json.ps1 @@ -114,7 +114,7 @@ function json_path([String] $json, [String] $jsonpath, [Hashtable] $substitution # Convert first value to string $result = $result.ToString() } else { - $result = "$([String]::Join("\n",$result))" + $result = "$([String]::Join('\n', $result))" } return $result } catch [Exception] { From 9124efc74a5daa68c176bf2d77ed48e1a6746cde Mon Sep 17 00:00:00 2001 From: Pratik Chowdhury Date: Fri, 19 Nov 2021 22:11:52 -0800 Subject: [PATCH 08/10] Update lib/json.ps1 Co-authored-by: Hsiao-nan Cheung --- lib/json.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/json.ps1 b/lib/json.ps1 index faf3883cc8..0405ff5bfa 100644 --- a/lib/json.ps1 +++ b/lib/json.ps1 @@ -118,7 +118,7 @@ function json_path([String] $json, [String] $jsonpath, [Hashtable] $substitution } return $result } catch [Exception] { - write-host -f DarkRed $_ + Write-Host $_ -ForegroundColor DarkRed } return $null From 8699fc9bc8c2ab4405cc2ee00c497aa27419f7ba Mon Sep 17 00:00:00 2001 From: Pratik Chowdhury Date: Mon, 22 Nov 2021 05:21:40 -0800 Subject: [PATCH 09/10] Update bin/checkver.ps1 Co-authored-by: Hsiao-nan Cheung --- bin/checkver.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/checkver.ps1 b/bin/checkver.ps1 index 32a6f10c38..0f22b75c9a 100644 --- a/bin/checkver.ps1 +++ b/bin/checkver.ps1 @@ -219,8 +219,7 @@ while ($in_progress -gt 0) { # Then reverse would have no effect because regex handles reverse # on its own # So in this case we have to disable reverse - $reverse = $reverse -and $single - $ver = json_path $page $jsonpath $null $reverse $single + $ver = json_path $page $jsonpath $null ($reverse -and $noregex) $noregex if (!$ver) { $ver = json_path_legacy $page $jsonpath } From eaf4a195aa442af750017dd1f04e1c55c104e56a Mon Sep 17 00:00:00 2001 From: Pratik Chowdhury Date: Mon, 22 Nov 2021 05:21:46 -0800 Subject: [PATCH 10/10] Update bin/checkver.ps1 Co-authored-by: Hsiao-nan Cheung --- bin/checkver.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/checkver.ps1 b/bin/checkver.ps1 index 0f22b75c9a..8afb8c5df7 100644 --- a/bin/checkver.ps1 +++ b/bin/checkver.ps1 @@ -214,7 +214,7 @@ while ($in_progress -gt 0) { if ($jsonpath) { # Return only a single value if regex is absent - $single = [String]::IsNullOrEmpty($regex) + $noregex = [String]::IsNullOrEmpty($regex) # If reverse is ON and regex is ON, # Then reverse would have no effect because regex handles reverse # on its own