generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent extra JSON from being stored in actions. (#1880)
* Prevent extra JSON from being stored in actions. * Make mapstructure safe in all uses. * Simplify test
- Loading branch information
Showing
6 changed files
with
161 additions
and
103 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
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,20 @@ | ||
package safemapstructure | ||
|
||
import ( | ||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
func Decode(input interface{}, output interface{}) error { | ||
config := &mapstructure.DecoderConfig{ | ||
Metadata: nil, | ||
Result: output, | ||
MatchName: func(a string, b string) bool { return a == b }, // Only match exactly | ||
} | ||
|
||
decoder, err := mapstructure.NewDecoder(config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return decoder.Decode(input) | ||
} |
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,52 @@ | ||
package safemapstructure | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDecodeNoMatchesCase(t *testing.T) { | ||
type Test struct { | ||
Test string `mapstructure:"test"` | ||
OtherTest string `mapstructure:"other_test"` | ||
} | ||
|
||
input := map[string]interface{}{ | ||
"tEst": "incorrect", | ||
"Test": "incorrect", | ||
"Other_test": "incorrect", | ||
"other_tEst": "incorrect", | ||
} | ||
|
||
var output Test | ||
err := Decode(input, &output) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, "", output.Test) | ||
require.Equal(t, "", output.OtherTest) | ||
} | ||
|
||
func TestDecodeHasMatch(t *testing.T) { | ||
type Test struct { | ||
Test string `mapstructure:"test"` | ||
OtherTest string `mapstructure:"other_test"` | ||
} | ||
|
||
input := map[string]interface{}{ | ||
"tEst": "incorrect", | ||
"test": "correct1", | ||
"other_test": "correct2", | ||
"other_tEst": "incorrect", | ||
} | ||
|
||
// Do it a bunch of times since map order is randomized | ||
for i := 0; i < 100; i++ { | ||
var output Test | ||
err := Decode(input, &output) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, "correct1", output.Test) | ||
require.Equal(t, "correct2", output.OtherTest) | ||
} | ||
} |