add metadata decode unit test
Signed-off-by: Bernd Verst <4535280+berndverst@users.noreply.github.com>
This commit is contained in:
parent
393f24280b
commit
bdcf747143
|
@ -128,12 +128,13 @@ func GetMetadataProperty(props map[string]string, keys ...string) (val string, o
|
|||
|
||||
// DecodeMetadata decodes metadata into a struct
|
||||
// This is an extension of mitchellh/mapstructure which also supports decoding durations
|
||||
func DecodeMetadata(input map[string]string, result interface{}) error {
|
||||
func DecodeMetadata(input interface{}, result interface{}) error {
|
||||
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
||||
DecodeHook: mapstructure.ComposeDecodeHookFunc(
|
||||
toTimeDurationHookFunc()),
|
||||
Metadata: nil,
|
||||
Result: &result,
|
||||
Metadata: nil,
|
||||
Result: result,
|
||||
WeaklyTypedInput: true,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -14,7 +14,9 @@ limitations under the License.
|
|||
package metadata
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -91,3 +93,34 @@ func TestTryGetContentType(t *testing.T) {
|
|||
assert.Equal(t, true, ok)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMetadataDecode(t *testing.T) {
|
||||
t.Run("Test metadata decoding", func(t *testing.T) {
|
||||
type testMetadata struct {
|
||||
Mystring string `json:"mystring"`
|
||||
Myduration Duration `json:"myduration"`
|
||||
Myinteger int `json:"myinteger,string"`
|
||||
Myfloat64 float64 `json:"myfloat64,string"`
|
||||
Mybool *bool `json:"mybool,omitempty"`
|
||||
}
|
||||
|
||||
var m testMetadata
|
||||
|
||||
testData := make(map[string]string)
|
||||
testData["mystring"] = "test"
|
||||
testData["myduration"] = "3s"
|
||||
testData["myinteger"] = "1"
|
||||
testData["myfloat64"] = "1.1"
|
||||
testData["mybool"] = "true"
|
||||
|
||||
err := DecodeMetadata(testData, &m)
|
||||
fmt.Println(testData)
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, true, *m.Mybool)
|
||||
assert.Equal(t, "test", m.Mystring)
|
||||
assert.Equal(t, 1, m.Myinteger)
|
||||
assert.Equal(t, 1.1, m.Myfloat64)
|
||||
assert.Equal(t, Duration{Duration: 3 * time.Second}, m.Myduration)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue