fix: decode params using json tags

This commit is contained in:
Kevin Franklin Kim 2024-07-09 07:12:41 +02:00
parent 9629047b99
commit 598ab3b8fb
No known key found for this signature in database
2 changed files with 24 additions and 6 deletions

18
pkg/sesamy/decode.go Normal file
View File

@ -0,0 +1,18 @@
package sesamy
import (
"github.com/mitchellh/mapstructure"
)
func Decode(input any, output any) error {
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: output,
TagName: "json",
WeaklyTypedInput: true,
IgnoreUntaggedFields: true,
})
if err != nil {
return err
}
return decoder.Decode(input)
}

View File

@ -1,9 +1,5 @@
package sesamy
import (
"github.com/mitchellh/mapstructure"
)
type Event[P any] struct {
// Reserved names: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference?client_type=gtag#reserved_names
Name EventName `json:"name"`
@ -33,6 +29,10 @@ func (e Event[P]) AnyEvent() Event[any] {
}
}
func (e Event[P]) DecodeParams(output any) error {
return mapstructure.Decode(e, output)
func (e Event[P]) Decode(output any) error {
return Decode(e, output)
}
func (e Event[P]) DecodeParams(output any) error {
return Decode(e.Params, output)
}