<commit_before>package mailgun

import (
	"fmt"
	"reflect"
	"time"

	"github.com/mailgun/mailgun-go/v4/events"
	"github.com/mailru/easyjson"
)

// All events returned by the EventIterator conform to this interface
type Event interface {
	easyjson.Unmarshaler
	easyjson.Marshaler
	GetName() string
	SetName(name string)
	GetTimestamp() time.Time
	SetTimestamp(time.Time)
	GetID() string
	SetID(id string)
}

// A list of all JSON event types returned by the /events API
var EventNames = map[string]func() Event{
	"accepted":                 new_(events.Accepted{}),
	"clicked":                  new_(events.Clicked{}),
	"complained":               new_(events.Complained{}),
	"delivered":                new_(events.Delivered{}),
	"failed":                   new_(events.Failed{}),
	"opened":                   new_(events.Opened{}),
	"rejected":                 new_(events.Rejected{}),
	"stored":                   new_(events.Stored{}),
	"unsubscribed":             new_(events.Unsubscribed{}),
	"list_member_uploaded":     new_(events.ListMemberUploaded{}),
	"list_member_upload_error": new_(events.ListMemberUploadError{}),
	"list_uploaded":            new_(events.ListUploaded{}),
}

// new_ is a universal event "constructor".
func new_(e interface{}) func() Event {
	typ := reflect.TypeOf(e)
	return func() Event {
		return reflect.New(typ).Interface().(Event)
	}
}

func parseResponse(raw []byte) ([]Event, error) {
	var resp events.Response
	if err := easyjson.Unmarshal(raw, &resp); err != nil {
		return nil, fmt.Errorf("failed to un-marshall event.Response: %s", err)
	}

	var result []Event
	for _, value := range resp.Items {
		event, err := ParseEvent(value)
		if err != nil {
			return nil, fmt.Errorf("while parsing event: %s", err)
		}
		result = append(result, event)
	}
	return result, nil
}

// Given a slice of events.RawJSON events return a slice of Event for each parsed event
func ParseEvents(raw []events.RawJSON) ([]Event, error) {
	var result []Event
	for _, value := range raw {
		event, err := ParseEvent(value)
		if err != nil {
			return nil, fmt.Errorf("while parsing event: %s", err)
		}
		result = append(result, event)
	}
	return result, nil
}

// Parse converts raw bytes data into an event struct. Can accept events.RawJSON as input
func ParseEvent(raw []byte) (Event, error) {
	// Try to recognize the event first.
	var e events.EventName
	if err := easyjson.Unmarshal(raw, &e); err != nil {
		return nil, fmt.Errorf("failed to recognize event: %v", err)
	}

	// Get the event "constructor" from the map.
	newEvent, ok := EventNames[e.Name]
	if !ok {
		return nil, fmt.Errorf("unsupported event: '%s'", e.Name)
	}
	event := newEvent()

	// Parse the known event.
	if err := easyjson.Unmarshal(raw, event); err != nil {
		return nil, fmt.Errorf("failed to parse event '%s': %v", e.Name, err)
	}

	return event, nil
}<commit_msg>Fix webhook test failures<commit_after>package mailgun

import (
	"fmt"
	"reflect"
	"time"

	"github.com/mailgun/mailgun-go/v4/events"
	"github.com/mailru/easyjson"
)

// All events returned by the EventIterator conform to this interface
type Event interface {
	easyjson.Unmarshaler
	easyjson.Marshaler
	GetName() string
	SetName(name string)
	GetTimestamp() time.Time
	SetTimestamp(time.Time)
	GetID() string
	SetID(id string)
}

// A list of all JSON event types returned by the /events API
var EventNames = map[string]func() Event{
	"accepted":                 new_(events.Accepted{}),
	"clicked":                  new_(events.Clicked{}),
	"complained":               new_(events.Complained{}),
	"delivered":                new_(events.Delivered{}),
	"failed":                   new_(events.Failed{}),
	"opened":                   new_(events.Opened{}),
	"rejected":                 new_(events.Rejected{}),
	"stored":                   new_(events.Stored{}),
	"unsubscribed":             new_(events.Unsubscribed{}),
	"list_member_uploaded":     new_(events.ListMemberUploaded{}),
	"list_member_upload_error": new_(events.ListMemberUploadError{}),
	"list_uploaded":            new_(events.ListUploaded{}),
}

// new_ is a universal event "constructor".
func new_(e interface{}) func() Event {
	typ := reflect.TypeOf(e)
	return func() Event {
		return reflect.New(typ).Interface().(Event)
	}
}

func parseResponse(raw []byte) ([]Event, error) {
	var resp events.Response
	if err := easyjson.Unmarshal(raw, &resp); err != nil {
		return nil, fmt.Errorf("failed to un-marshall event.Response: %s", err)
	}

	var result []Event
	for _, value := range resp.Items {
		event, err := ParseEvent(value)
		if err != nil {
			return nil, fmt.Errorf("while parsing event: %s", err)
		}
		result = append(result, event)
	}
	return result, nil
}

// Given a slice of events.RawJSON events return a slice of Event for each parsed event
func ParseEvents(raw []events.RawJSON) ([]Event, error) {
	var result []Event
	for _, value := range raw {
		event, err := ParseEvent(value)
		if err != nil {
			return nil, fmt.Errorf("while parsing event: %s", err)
		}
		result = append(result, event)
	}
	return result, nil
}

// Parse converts raw bytes data into an event struct. Can accept events.RawJSON as input
func ParseEvent(raw []byte) (Event, error) {
	// Try to recognize the event first.
	var e events.EventName
	if err := easyjson.Unmarshal(raw, &e); err != nil {
		return nil, fmt.Errorf("failed to recognize event: %v", err)
	}

	// Get the event "constructor" from the map.
	newEvent, ok := EventNames[e.GetName()]
	if !ok {
		return nil, fmt.Errorf("unsupported event: '%s'", e.GetName())
	}
	event := newEvent()

	// Parse the known event.
	if err := easyjson.Unmarshal(raw, event); err != nil {
		return nil, fmt.Errorf("failed to parse event '%s': %v", e.GetName(), err)
	}

	return event, nil
}
{'fraction_non_alphanumeric': 0.1403021357874631, 'fraction_numerical': 0.0003472825143254037, 'mean_word_length': 3.3835616438356166, 'pattern_counts': {'":': 24, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2662856', 'n_tokens_mistral': 1918, 'n_tokens_neox': 1833, 'n_words': 545}