feat: add decode params

This commit is contained in:
Kevin Franklin Kim 2024-05-27 12:51:53 +02:00
parent d9cee831e8
commit 3bd292ec56
No known key found for this signature in database

View File

@ -1,5 +1,9 @@
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"`
@ -7,6 +11,10 @@ type Event[P any] struct {
Params P `json:"params,omitempty"`
}
// ------------------------------------------------------------------------------------------------
// ~ Constructor
// ------------------------------------------------------------------------------------------------
func NewEvent[P any](name EventName, params P) Event[P] {
return Event[P]{
Name: name,
@ -14,9 +22,17 @@ func NewEvent[P any](name EventName, params P) Event[P] {
}
}
// ------------------------------------------------------------------------------------------------
// ~ Public methods
// ------------------------------------------------------------------------------------------------
func (e Event[P]) AnyEvent() Event[any] {
return Event[any]{
Name: e.Name,
Params: e.Params,
}
}
func (e Event[P]) DecodeParams(output any) error {
return mapstructure.Decode(e, output)
}