From 0bd5b22913f888c5b434b0114ae5c3ce7dd1a4c9 Mon Sep 17 00:00:00 2001 From: Brandon Pollack Date: Thu, 13 Feb 2025 13:28:45 +0900 Subject: [PATCH] Use `regex` and `regexall`` from `pulumi-std` Fixes #191 Fixes pulumi/pulumi#18520 --- CHANGELOG_PENDING.md | 3 +- .../builtin_functions/pcl/diagnostics.json | 7 ---- .../programs/builtin_functions/pcl/main.pp | 35 +++++++++++++++---- .../pcl/diagnostics.json | 2 -- .../pcl/dir_1.0.0/files.pp | 7 ++-- .../pcl/dir_1.0.2/files.pp | 7 ++-- pkg/convert/tf.go | 11 +++++- 7 files changed, 50 insertions(+), 22 deletions(-) diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index ac1af19..e66c55b 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -17,7 +17,8 @@ - Implement `contains` through the `pulumi-std` invoke of the same name - Implement `chunklist` through the `pulumi-std` invoke of the same name - Implement `slice` through the `pulumi-std` invoke of the same name +- Implement `regex(all)` through the `pulumi-std` invokes of the same name ### Bug Fixes -- Fix the order of arguments to `substr` \ No newline at end of file +- Fix the order of arguments to `substr` diff --git a/pkg/convert/testdata/programs/builtin_functions/pcl/diagnostics.json b/pkg/convert/testdata/programs/builtin_functions/pcl/diagnostics.json index 95b515e..cc488e9 100644 --- a/pkg/convert/testdata/programs/builtin_functions/pcl/diagnostics.json +++ b/pkg/convert/testdata/programs/builtin_functions/pcl/diagnostics.json @@ -37,13 +37,6 @@ "warning:builtin_functions/main.tf:670,15-41:Function not yet implemented:Function toset not yet implemented, see pulumi/pulumi-converter-terraform#137", "warning:builtin_functions/main.tf:670,11-42:Function not yet implemented:Function one not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", "warning:builtin_functions/main.tf:706,11-26:Function not yet implemented:Function plantimestamp not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", - "warning:builtin_functions/main.tf:742,11-59:Function not yet implemented:Function regex not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", - "warning:builtin_functions/main.tf:745,11-66:Function not yet implemented:Function regex not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", - "warning:builtin_functions/main.tf:748,11-106:Function not yet implemented:Function regex not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", - "warning:builtin_functions/main.tf:751,11-50:Function not yet implemented:Function regex not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", - "warning:builtin_functions/main.tf:757,11-50:Function not yet implemented:Function regexall not yet implemented, see pulumi/pulumi-converter-terraform#191", - "warning:builtin_functions/main.tf:760,18-57:Function not yet implemented:Function regexall not yet implemented, see pulumi/pulumi-converter-terraform#191", - "warning:builtin_functions/main.tf:763,18-49:Function not yet implemented:Function regexall not yet implemented, see pulumi/pulumi-converter-terraform#191", "warning:builtin_functions/main.tf:778,11-29:Function not yet implemented:Function reverse not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", "warning:builtin_functions/main.tf:808,11-67:Function not yet implemented:Function setproduct not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", "warning:builtin_functions/main.tf:811,11-35:Function not yet implemented:Function setproduct not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", diff --git a/pkg/convert/testdata/programs/builtin_functions/pcl/main.pp b/pkg/convert/testdata/programs/builtin_functions/pcl/main.pp index 98f440c..87e2f62 100644 --- a/pkg/convert/testdata/programs/builtin_functions/pcl/main.pp +++ b/pkg/convert/testdata/programs/builtin_functions/pcl/main.pp @@ -1150,29 +1150,50 @@ # Examples for regex output "funcRegex0" { - value = notImplemented("regex(\"[a-z]+\",\"53453453.345345aaabbbccc23454\")") + value = invoke("std:index:regex", { + pattern = "[a-z]+" + string = "53453453.345345aaabbbccc23454" + }).result } output "funcRegex1" { - value = notImplemented("regex(\"(\\\\d\\\\d\\\\d\\\\d)-(\\\\d\\\\d)-(\\\\d\\\\d)\",\"2019-02-01\")") + value = invoke("std:index:regex", { + pattern = "(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)" + string = "2019-02-01" + }).result } output "funcRegex2" { - value = notImplemented("regex(\"^(?:(?P[^:/?#]+):)?(?://(?P[^/?#]*))?\",\"https://terraform.io/docs/\")") + value = invoke("std:index:regex", { + pattern = "^(?:(?P[^:/?#]+):)?(?://(?P[^/?#]*))?" + string = "https://terraform.io/docs/" + }).result } output "funcRegex3" { - value = notImplemented("regex(\"[a-z]+\",\"53453453.34534523454\")") + value = invoke("std:index:regex", { + pattern = "[a-z]+" + string = "53453453.34534523454" + }).result } # Examples for regexall output "funcRegexall0" { - value = notImplemented("regexall(\"[a-z]+\",\"1234abcd5678efgh9\")") + value = invoke("std:index:regexall", { + pattern = "[a-z]+" + string = "1234abcd5678efgh9" + }).result } output "funcRegexall1" { - value = length(notImplemented("regexall(\"[a-z]+\",\"1234abcd5678efgh9\")")) + value = length(invoke("std:index:regexall", { + pattern = "[a-z]+" + string = "1234abcd5678efgh9" + }).result) } output "funcRegexall2" { - value = length(notImplemented("regexall(\"[a-z]+\",\"123456789\")")) > 0 + value = length(invoke("std:index:regexall", { + pattern = "[a-z]+" + string = "123456789" + }).result) > 0 } diff --git a/pkg/convert/testdata/programs/registry_module_multiple_versions/pcl/diagnostics.json b/pkg/convert/testdata/programs/registry_module_multiple_versions/pcl/diagnostics.json index e25543e..171d9f4 100644 --- a/pkg/convert/testdata/programs/registry_module_multiple_versions/pcl/diagnostics.json +++ b/pkg/convert/testdata/programs/registry_module_multiple_versions/pcl/diagnostics.json @@ -3,7 +3,6 @@ "warning:/files.tf:3,23-6,5:Function not yet implemented:Function toset not yet implemented, see pulumi/pulumi-converter-terraform#137", "warning:/files.tf:14,51-107:Function not yet implemented:Function templatefile not yet implemented, see pulumi/pulumi-converter-terraform#192", "warning:/files.tf:20,23-89:Function not yet implemented:Function setunion not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", - "warning:/files.tf:23,45-73:Function not yet implemented:Function regexall not yet implemented, see pulumi/pulumi-converter-terraform#191", "warning:/files.tf:36,24-38:Function not yet implemented:Function tostring not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", "warning:/files.tf:38,19-45,11:Function not yet implemented:Function tomap not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", "warning:/files.tf:52,24-38:Function not yet implemented:Function tostring not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", @@ -12,7 +11,6 @@ "warning:/files.tf:3,23-6,5:Function not yet implemented:Function toset not yet implemented, see pulumi/pulumi-converter-terraform#137", "warning:/files.tf:14,51-107:Function not yet implemented:Function templatefile not yet implemented, see pulumi/pulumi-converter-terraform#192", "warning:/files.tf:20,23-89:Function not yet implemented:Function setunion not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", - "warning:/files.tf:23,45-73:Function not yet implemented:Function regexall not yet implemented, see pulumi/pulumi-converter-terraform#191", "warning:/files.tf:36,24-38:Function not yet implemented:Function tostring not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", "warning:/files.tf:38,19-45,11:Function not yet implemented:Function tomap not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", "warning:/files.tf:52,24-38:Function not yet implemented:Function tostring not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)", diff --git a/pkg/convert/testdata/programs/registry_module_multiple_versions/pcl/dir_1.0.0/files.pp b/pkg/convert/testdata/programs/registry_module_multiple_versions/pcl/dir_1.0.0/files.pp index 9d09e64..468ac0f 100644 --- a/pkg/convert/testdata/programs/registry_module_multiple_versions/pcl/dir_1.0.0/files.pp +++ b/pkg/convert/testdata/programs/registry_module_multiple_versions/pcl/dir_1.0.0/files.pp @@ -11,8 +11,11 @@ templateFileContents = { for p, sp in templateFilePaths : p => notImplemented("templatefile(\"$${var.base_dir}/$${sp}\",var.template_vars)") } staticFileLocalPaths = { for p in staticFilePaths : p => "${baseDir}/${p}" } outputFilePaths = notImplemented("setunion(keys(local.template_file_paths),local.static_file_paths)") -fileSuffixMatches = { for p in outputFilePaths : p => notImplemented("regexall(\"\\\\.[^\\\\.]+\\\\z\",p)") } -fileSuffixes = { for p, ms in fileSuffixMatches : p => length(ms) > 0 ? ms[0] : "" } +fileSuffixMatches = { for p in outputFilePaths : p => invoke("std:index:regexall", { + pattern = "\\.[^\\.]+\\z" + string = p +}).result } +fileSuffixes = { for p, ms in fileSuffixMatches : p => length(ms) > 0 ? ms[0] : "" } myFileTypes = { for p in outputFilePaths : p => invoke("std:index:lookup", { map = fileTypes key = fileSuffixes[p] diff --git a/pkg/convert/testdata/programs/registry_module_multiple_versions/pcl/dir_1.0.2/files.pp b/pkg/convert/testdata/programs/registry_module_multiple_versions/pcl/dir_1.0.2/files.pp index 9d09e64..468ac0f 100644 --- a/pkg/convert/testdata/programs/registry_module_multiple_versions/pcl/dir_1.0.2/files.pp +++ b/pkg/convert/testdata/programs/registry_module_multiple_versions/pcl/dir_1.0.2/files.pp @@ -11,8 +11,11 @@ templateFileContents = { for p, sp in templateFilePaths : p => notImplemented("templatefile(\"$${var.base_dir}/$${sp}\",var.template_vars)") } staticFileLocalPaths = { for p in staticFilePaths : p => "${baseDir}/${p}" } outputFilePaths = notImplemented("setunion(keys(local.template_file_paths),local.static_file_paths)") -fileSuffixMatches = { for p in outputFilePaths : p => notImplemented("regexall(\"\\\\.[^\\\\.]+\\\\z\",p)") } -fileSuffixes = { for p, ms in fileSuffixMatches : p => length(ms) > 0 ? ms[0] : "" } +fileSuffixMatches = { for p in outputFilePaths : p => invoke("std:index:regexall", { + pattern = "\\.[^\\.]+\\z" + string = p +}).result } +fileSuffixes = { for p, ms in fileSuffixMatches : p => length(ms) > 0 ? ms[0] : "" } myFileTypes = { for p in outputFilePaths : p => invoke("std:index:lookup", { map = fileTypes key = fileSuffixes[p] diff --git a/pkg/convert/tf.go b/pkg/convert/tf.go index 67ec3fd..1c24480 100644 --- a/pkg/convert/tf.go +++ b/pkg/convert/tf.go @@ -703,6 +703,16 @@ var tfFunctionStd = map[string]struct { inputs: []string{"limit", "start", "step"}, output: "result", }, + "regex": { + token: "std:index:regex", + inputs: []string{"pattern", "string"}, + output: "result", + }, + "regexall": { + token: "std:index:regexall", + inputs: []string{"pattern", "string"}, + output: "result", + }, "replace": { token: "std:index:replace", inputs: []string{"text", "search", "replace"}, @@ -3666,7 +3676,6 @@ func componentProgramBinderFromAfero(fs afero.Fs) pcl.ComponentProgramBinder { var unimplementedFunctionBugs = map[string]string{ "formatdate": "pulumi/pulumi-converter-terraform#196", - "regexall": "pulumi/pulumi-converter-terraform#191", "templatefile": "pulumi/pulumi-converter-terraform#192", "toset": "pulumi/pulumi-converter-terraform#137", "try": "pulumi/pulumi-converter-terraform#16",