Skip to content

Commit

Permalink
Make FromMap's value type a type parameter
Browse files Browse the repository at this point in the history
And also Squash by default.
  • Loading branch information
bep committed Sep 9, 2022
1 parent dd57143 commit d0b7715
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions model/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ import (

// FromNMap converts m to T.
// See https://pkg.go.dev/github.com/mitchellh/mapstructure#section-readme
func FromMap[T any](m map[string]any) (T, error) {
func FromMap[S, T any](m map[string]S) (T, error) {
var t T
err := mapstructure.WeakDecode(m, &t)
return t, err

config := &mapstructure.DecoderConfig{
Metadata: nil,
Result: &t,
WeaklyTypedInput: true,
Squash: true,
}

decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return t, err
}

return t, decoder.Decode(m)
}

// Error is an error that can be returned from a plugin,
Expand Down

0 comments on commit d0b7715

Please sign in to comment.