Skip to content

Commit a617fb2

Browse files
committed
fix: generator sonar
1 parent b08a3bf commit a617fb2

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

pkg/generator/generator.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"regexp"
98
"strconv"
109
"strings"
1110
"sync"
@@ -308,8 +307,8 @@ func (c *GenVars) exportVars(exportMap ParsedMap) {
308307

309308
for k, v := range exportMap {
310309
// NOTE: \n line ending is not totally cross platform
311-
_type := fmt.Sprintf("%T", v)
312-
switch _type {
310+
t := fmt.Sprintf("%T", v)
311+
switch t {
313312
case "string":
314313
c.outString = append(c.outString, fmt.Sprintf("export %s='%s'", c.normalizeKey(k), v))
315314
default:
@@ -320,12 +319,10 @@ func (c *GenVars) exportVars(exportMap ParsedMap) {
320319

321320
// normalizeKeys returns env var compatible key
322321
func (c *GenVars) normalizeKey(k string) string {
323-
// TODO: include a more complete regex of vaues to replace
324-
r := regexp.MustCompile(`[\s\@\!]`).ReplaceAll([]byte(k), []byte(""))
325-
r = regexp.MustCompile(`[\-]`).ReplaceAll(r, []byte("_"))
326-
// Double underscore replace key separator
327-
r = regexp.MustCompile(`[`+c.config.keySeparator+`]`).ReplaceAll(r, []byte("__"))
328-
return strings.ToUpper(string(r))
322+
// the order of replacer pairs matters less
323+
// as the Replace builds a node tree without overlapping matches
324+
replacer := strings.NewReplacer([]string{" ", "", "@", "", "!", "", "-", "_", c.config.keySeparator, "__"}...)
325+
return strings.ToUpper(replacer.Replace(k))
329326
}
330327

331328
// FlushToFile saves contents to file provided

0 commit comments

Comments
 (0)