This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
config: Support converting string configs to []byte #85
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ce6c727
config: Support converting string configs to []byte
EngHabu c34cb44
Use base64 encoding to match yaml.unmarshal behavior
EngHabu e8bd4cc
support string array since it seems mapstructure does that
EngHabu e380438
Fix unit test value
EngHabu c16fc47
Increase patch coverage
EngHabu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the purpose of the not equal test here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I can make it equal 5, res...