Skip to content

Commit

Permalink
Change function name to FlattenMap and move it to its own package
Browse files Browse the repository at this point in the history
Signed-off-by: joshuabezaleel <joshua.bezaleel@gmail.com>
  • Loading branch information
joshuabezaleel committed Feb 22, 2022
1 parent a6ba83e commit a334240
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 122 deletions.
3 changes: 2 additions & 1 deletion pkg/build/buildkit/buildx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"strings"

"get.porter.sh/porter/pkg/build/driverutil"
"get.porter.sh/porter/pkg/config"
"get.porter.sh/porter/pkg/experimental"
"get.porter.sh/porter/pkg/tracing"
Expand Down Expand Up @@ -90,7 +91,7 @@ func (b *Builder) BuildInvocationImage(ctx context.Context, manifest *manifest.M
buildArgs["BUNDLE_DIR"] = build.BUNDLE_DIR

convertedCustomInput := make(map[string]string)
convertedCustomInput, err = convertMap(manifest.Custom)
convertedCustomInput, err = driverutil.FlattenMap(manifest.Custom)
if err != nil {
return err
}
Expand Down
29 changes: 2 additions & 27 deletions pkg/build/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"get.porter.sh/porter/pkg/build"
"get.porter.sh/porter/pkg/build/driverutil"
portercontext "get.porter.sh/porter/pkg/context"
"get.porter.sh/porter/pkg/manifest"
"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -35,7 +36,7 @@ func (b *Builder) BuildInvocationImage(ctx context.Context, manifest *manifest.M
buildArgs["BUNDLE_DIR"] = &build.BUNDLE_DIR

convertedCustomInput := make(map[string]string)
convertedCustomInput, err := convertMap(manifest.Custom)
convertedCustomInput, err := driverutil.FlattenMap(manifest.Custom)
if err != nil {
return err
}
Expand Down Expand Up @@ -111,29 +112,3 @@ func (b *Builder) TagInvocationImage(ctx context.Context, origTag, newTag string
}
return nil
}

func convertMap(mapInput map[string]interface{}) (map[string]string, error) {
out := make(map[string]string)

for key, value := range mapInput {
switch v := value.(type) {
case string:
out[key] = v
case map[string]interface{}:
tmp, err := convertMap(v)
if err != nil {
return nil, err
}
for innerKey, innerValue := range tmp {
out[key+"."+innerKey] = innerValue
}
case map[string]string:
for innerKey, innerValue := range v {
out[key+"."+innerKey] = innerValue
}
default:
return nil, errors.Errorf("Unknown type %#v: %t", v, v)
}
}
return out, nil
}
91 changes: 0 additions & 91 deletions pkg/build/docker/docker_test.go

This file was deleted.

33 changes: 33 additions & 0 deletions pkg/build/driverutil/driverutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package driverutil

import (
"github.com/pkg/errors"
)

// FlattenMap recursively walks through nested map and flattent it
// to one-level map of key-value with string type.
func FlattenMap(mapInput map[string]interface{}) (map[string]string, error) {
out := make(map[string]string)

for key, value := range mapInput {
switch v := value.(type) {
case string:
out[key] = v
case map[string]interface{}:
tmp, err := FlattenMap(v)
if err != nil {
return nil, err
}
for innerKey, innerValue := range tmp {
out[key+"."+innerKey] = innerValue
}
case map[string]string:
for innerKey, innerValue := range v {
out[key+"."+innerKey] = innerValue
}
default:
return nil, errors.Errorf("Unknown type %#v: %t", v, v)
}
}
return out, nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package buildkit
package driverutil

import (
"testing"
Expand All @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestConvertMap(t *testing.T) {
func TestFlattenMap(t *testing.T) {
tt := []struct {
desc string
inp map[string]interface{}
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestConvertMap(t *testing.T) {

for _, tc := range tt {
t.Run(tc.desc, func(t *testing.T) {
out, err := convertMap(tc.inp)
out, err := FlattenMap(tc.inp)
if tc.err {
require.Error(t, err)
return
Expand Down

0 comments on commit a334240

Please sign in to comment.