forked from flyteorg/flyte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: Support converting string configs to []byte (flyteorg#85)
* config: Support converting string configs to []byte Signed-off-by: Haytham Abuelfutuh <[email protected]> * Use base64 encoding to match yaml.unmarshal behavior Signed-off-by: Haytham Abuelfutuh <[email protected]> * support string array since it seems mapstructure does that Signed-off-by: Haytham Abuelfutuh <[email protected]> * Fix unit test value Signed-off-by: Haytham Abuelfutuh <[email protected]> * Increase patch coverage Signed-off-by: Haytham Abuelfutuh <[email protected]>
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package viper | ||
|
||
import ( | ||
"encoding/base64" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_stringToByteArray(t *testing.T) { | ||
t.Run("Expected types", func(t *testing.T) { | ||
input := "hello world" | ||
base64Encoded := base64.StdEncoding.EncodeToString([]byte(input)) | ||
res, err := stringToByteArray(reflect.TypeOf(base64Encoded), reflect.TypeOf([]byte{}), base64Encoded) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte(input), res) | ||
}) | ||
|
||
t.Run("Expected types - array string", func(t *testing.T) { | ||
input := []string{"hello world"} | ||
base64Encoded := base64.StdEncoding.EncodeToString([]byte(input[0])) | ||
res, err := stringToByteArray(reflect.TypeOf(input), reflect.TypeOf([]byte{}), []string{base64Encoded}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte(input[0]), res) | ||
}) | ||
|
||
t.Run("Expected types - invalid encoding", func(t *testing.T) { | ||
input := []string{"hello world"} | ||
_, err := stringToByteArray(reflect.TypeOf(input), reflect.TypeOf([]byte{}), []string{"invalid base64"}) | ||
assert.Error(t, err) | ||
}) | ||
|
||
t.Run("Expected types - empty array string", func(t *testing.T) { | ||
input := []string{"hello world"} | ||
res, err := stringToByteArray(reflect.TypeOf(input), reflect.TypeOf([]byte{}), []string{}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []string{}, res) | ||
}) | ||
|
||
t.Run("Unexpected types", func(t *testing.T) { | ||
input := 5 | ||
res, err := stringToByteArray(reflect.TypeOf(input), reflect.TypeOf([]byte{}), input) | ||
assert.NoError(t, err) | ||
assert.NotEqual(t, []byte("hello"), res) | ||
}) | ||
|
||
t.Run("Unexpected types", func(t *testing.T) { | ||
input := 5 | ||
res, err := stringToByteArray(reflect.TypeOf(input), reflect.TypeOf(""), input) | ||
assert.NoError(t, err) | ||
assert.NotEqual(t, []byte("hello"), res) | ||
}) | ||
} |