Skip to content

Commit

Permalink
fix(utils): do not use a pointer for maps
Browse files Browse the repository at this point in the history
  • Loading branch information
FalcoSuessgott committed Jun 13, 2024
1 parent d0d4b02 commit f1d3e20
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (o *importOptions) parseInput(input []byte) (map[string]interface{}, error)

func (o *importOptions) writeSecrets(secrets map[string]interface{}) error {
transformedMap := make(map[string]interface{})
utils.TransformMap("", secrets, &transformedMap)
utils.TransformMap(secrets, transformedMap, "")

for p, m := range transformedMap {
secrets, ok := m.(map[string]interface{})
Expand Down
2 changes: 1 addition & 1 deletion cmd/snapshot_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (o *snapshotRestoreOptions) restoreSecrets(source string) error {

func (o *snapshotRestoreOptions) writeSecrets(secrets map[string]interface{}, v *vault.Vault, ns, rootPath string) error {
transformedMap := make(map[string]interface{})
utils.TransformMap("", secrets, &transformedMap)
utils.TransformMap(secrets, transformedMap, "")

for p, m := range transformedMap {
secrets, ok := m.(map[string]interface{})
Expand Down
2 changes: 1 addition & 1 deletion pkg/printer/secret/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (p *Printer) buildMarkdownTable(secrets map[string]interface{}) ([]string,
headers := []string{}

m := make(map[string]interface{})
utils.TransformMap("", secrets, &m)
utils.TransformMap(secrets, m, "")

for _, k := range utils.SortMapKeys(m) {
v := utils.ToMapStringInterface(m[k])
Expand Down
2 changes: 1 addition & 1 deletion pkg/printer/secret/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (

func (p *Printer) printPolicy(secrets map[string]interface{}) error {
transformMap := make(map[string]interface{})
utils.TransformMap("", secrets, &transformMap)
utils.TransformMap(secrets, transformMap, "")

capMap := make(map[string]*vault.Capability)

Expand Down
13 changes: 7 additions & 6 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ type Keys []string

// TransformMap takes a multi leveled map and returns a map with its combined paths
// as the keys and the map as its value. Also see TestTransformMap().
func TransformMap(p string, m map[string]interface{}, s *map[string]interface{}) {
for k, v := range m {
subMap, ok := v.(map[string]interface{})
func TransformMap(a, b map[string]interface{}, key string) {
for k, v := range a {
// if its a map -> go deeper
m, ok := v.(map[string]interface{})
if ok {
TransformMap(path.Join(p, k), subMap, s)
} else {
(*s)[p] = m
TransformMap(m, b, path.Join(key, k))
} else { // otherwise add the key and value to the map
b[key] = a
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestTransformMap(t *testing.T) {
for _, tc := range testCases {
res := make(map[string]interface{})

TransformMap("", tc.m, &res)
TransformMap(tc.m, res, "")

assert.Equal(t, tc.expected, res, tc.expected)
}
Expand Down

0 comments on commit f1d3e20

Please sign in to comment.