func (t *AnchorEventType) UnmarshalJSON(bytes []byte) error {
	t.ObjectType = NewObject()
	t.anchorEvent = &anchorEventType{}
	return UnmarshalJSON(bytes, t.ObjectType, t.anchorEvent)
}
UnmarshalJSON umarshals the object from JSON.
package vocab
import (
	"encoding/json"
	"fmt"
	"net/url"
	"github.com/trustbloc/sidetree-core-go/pkg/canonicalizer"
	orberrors "github.com/trustbloc/orb/pkg/errors"
	"github.com/trustbloc/orb/pkg/hashlink"
)
type AnchorEventType struct {
	*ObjectType
	anchorEvent *anchorEventType
}
type anchorEventType struct {
	Anchors *URLProperty           `json:"anchors,omitempty"`
	Parent  *URLCollectionProperty `json:"parent,omitempty"`
}
func NewAnchorEvent(opts ...Opt) *AnchorEventType {
	options := NewOptions(opts...)
	return &AnchorEventType{
		ObjectType: NewObject(
			WithContext(getContexts(options, ContextActivityAnchors)...),
			WithType(TypeAnchorEvent),
			WithPublishedTime(options.Published),
			WithURL(options.URL...),
			WithAttributedTo(options.AttributedTo),
			WithAttachment(options.Attachment...)),
		anchorEvent: &anchorEventType{
			Anchors: NewURLProperty(options.Anchors),
			Parent:  NewURLCollectionProperty(options.Parent...),
		},
	}
}
func (t *AnchorEventType) Anchors() *url.URL {
	if t == nil || t.anchorEvent == nil {
		return nil
	}
	return t.anchorEvent.Anchors.URL()
}
func (t *AnchorEventType) Parent() Urls {
	if t == nil || t.anchorEvent == nil {
		return nil
	}
	return t.anchorEvent.Parent.URLs()
}
func (t *AnchorEventType) Validate() error {
	if t == nil {
		return fmt.Errorf("nil anchor event")
	}
	if t.Anchors() == nil {
		if len(t.URL()) > 0 {
			return nil
		}
		return fmt.Errorf("either anchors or URL is required on anchor event")
	}
	var anchorObj *AnchorObjectType
	for _, attachment := range t.Attachment() {
		if !attachment.Type().Is(TypeAnchorObject) {
			return fmt.Errorf("unsupported attachment type [%s] in anchor event", attachment.Type())
		}
		ao := attachment.AnchorObject()
		err := validateAnchorObject(ao)
		if err != nil {
			return fmt.Errorf("invalid anchor object: %w", err)
		}
		if ao.URL()[0].String() == t.Anchors().String() {
			anchorObj = ao
			break
		}
	}
	if anchorObj == nil {
		return fmt.Errorf("unable to find the attachment that matches the anchors URL in the anchor event [%s]",
			t.Anchors())
	}
	if anchorObj.Witness() == nil {
		return fmt.Errorf("witness is required")
	}
	return nil
}
func validateAnchorObject(anchorObj *AnchorObjectType) error {
	if len(anchorObj.URL()) != 1 {
		return fmt.Errorf("anchor object must have exactly one URL")
	}
	anchorObjURL := anchorObj.URL()[0]
	if anchorObj.Generator() == "" {
		return fmt.Errorf("generator is required in anchor event")
	}
	if anchorObj.ContentObject() == nil {
		return fmt.Errorf("content object is required in anchor event")
	}
	contentObjBytes, err := canonicalizer.MarshalCanonical(anchorObj.ContentObject())
	if err != nil {
		return fmt.Errorf("marshal content object: %w", err)
	}
	hl, err := hashlink.New().CreateHashLink(contentObjBytes, nil)
	if err != nil {
		return fmt.Errorf("create hashlink from content object: %w", err)
	}
	if hl != anchorObjURL.String() {
		return fmt.Errorf("hashlink of content object [%s] does not match the anchor object URL %s",
			hl, anchorObjURL)
	}
	return nil
}
func (t *AnchorEventType) Witness() *ObjectType {
	if t == nil || len(t.Attachment()) == 0 {
		return nil
	}
	return t.Attachment()[0].AnchorObject().Witness().Object()
}
func (t *AnchorEventType) AnchorObject(u *url.URL) (*AnchorObjectType, error) {
	if t == nil || u == nil {
		return nil, orberrors.ErrContentNotFound
	}
	for _, attachment := range t.Attachment() {
		if attachment.AnchorObject().URL().Contains(u) {
			return attachment.AnchorObject(), nil
		}
	}
	return nil, orberrors.ErrContentNotFound
}
func (t *AnchorEventType) MarshalJSON() ([]byte, error) {
	return MarshalJSON(t.ObjectType, t.anchorEvent)
}