Skip to content

Commit 5b70767

Browse files
authored
chore: refactor nprintf (#654)
Extracted from #622 Also ran goimports for clean up (triggered by stray imports based test failure)
2 parents 0844b8c + e144ee0 commit 5b70767

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

equinix/provider_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ func (t *testAccConfig) build() string {
288288
return t.config
289289
}
290290

291+
// nprintf returns a string with all the placeholders replaced by the values from the params map
292+
//
293+
// Deprecated: nprintf is shared between NE resource tests and has been
294+
// centralized ahead of those NE resources moving to separate packages.
295+
// Use github.com/equinix/terraform-provider-equinix/internal/nprintf.NPrintf instead
291296
func nprintf(format string, params map[string]interface{}) string {
292297
for key, val := range params {
293298
var strVal string

internal/nprintf/nprintf.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package nprintf
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strings"
7+
)
8+
9+
// NPrintf is a helper function to replace placeholders in a string with values from a map
10+
func NPrintf(format string, params map[string]interface{}) string {
11+
for key, val := range params {
12+
var strVal string
13+
switch val.(type) {
14+
case []string:
15+
r := regexp.MustCompile(`" "`)
16+
strVal = r.ReplaceAllString(fmt.Sprintf("%q", val), `", "`)
17+
default:
18+
strVal = fmt.Sprintf("%v", val)
19+
}
20+
format = strings.Replace(format, "%{"+key+"}", strVal, -1)
21+
}
22+
return format
23+
}

0 commit comments

Comments
 (0)