func (e *Event) legacySetData(obj interface{}) error {
	data, err := datacodec.Encode(context.Background(), e.DataMediaType(), obj)
	if err != nil {
		return err
	}
	if e.DeprecatedDataContentEncoding() == Base64 {
		buf := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
		base64.StdEncoding.Encode(buf, data)
		e.DataEncoded = buf
		e.DataBase64 = false
	} else {
		data, err := datacodec.Encode(context.Background(), e.DataMediaType(), obj)
		if err != nil {
			return err
		}
		e.DataEncoded = data
		e.DataBase64 = false
	}
	return nil
}
Deprecated: Delete when we do not have to support Spec v0.3.
package event
import (
	"context"
	"encoding/base64"
	"fmt"
	"strconv"
	"github.com/cloudevents/sdk-go/v2/event/datacodec"
)
func (e *Event) SetData(contentType string, obj interface{}) error {
	e.SetDataContentType(contentType)
	if e.SpecVersion() != CloudEventsVersionV1 {
		return e.legacySetData(obj)
	}
	switch obj := obj.(type) {
	case []byte:
		e.DataEncoded = obj
		e.DataBase64 = true
	default:
		data, err := datacodec.Encode(context.Background(), e.DataMediaType(), obj)
		if err != nil {
			return err
		}
		e.DataEncoded = data
		e.DataBase64 = false
	}
	return nil
}