messaging

package
v1.11.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2024 License: MIT Imports: 7 Imported by: 8

Documentation

Overview

Package messaging contains types used across messaging packages.

Example (UsingCloudEvent)
package main

import (
	"encoding/json"
	"fmt"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore/messaging"
)

func main() {
	type sampleType struct {
		CustomField string `json:"custom_field"`
	}

	eventToSend, err := messaging.NewCloudEvent("source", "eventtype", &sampleType{
		CustomField: "hello, a custom field value",
	}, nil)

	if err != nil {
		panic(err)
	}

	receivedEvent, err := sendAndReceiveCloudEvent(eventToSend)

	if err != nil {
		panic(err)
	}

	var receivedData *sampleType

	if err := json.Unmarshal(receivedEvent.Data.([]byte), &receivedData); err != nil {
		panic(err)
	}

	fmt.Printf("Custom field = %s\n", receivedData.CustomField)

}

func sendAndReceiveCloudEvent(ce messaging.CloudEvent) (messaging.CloudEvent, error) {
	bytes, err := json.Marshal(ce)

	if err != nil {
		return messaging.CloudEvent{}, err
	}

	var received *messaging.CloudEvent

	if err := json.Unmarshal(bytes, &received); err != nil {
		return messaging.CloudEvent{}, err
	}

	return *received, nil
}
Output:

Custom field = hello, a custom field value

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CloudEvent

type CloudEvent struct {

	// ID identifies the event. Producers MUST ensure that source + id is unique for each distinct event. If a duplicate
	// event is re-sent (e.g. due to a network error) it MAY have the same id. Consumers MAY assume that Events with
	// identical source and id are duplicates.
	ID string

	// Source identifies the context in which an event happened.
	Source string

	// SpecVersion is the version of the CloudEvents specification which the event uses.
	SpecVersion string

	// Type contains a value describing the type of event related to the originating occurrence.
	Type string

	// Data is the payload for the event.
	// * []byte will be serialized and deserialized as []byte.
	// * Any other type will be serialized to a JSON object and deserialized into
	//   a []byte, containing the JSON text.
	//
	// To deserialize into your chosen type:
	//
	//   var yourData *YourType
	//   json.Unmarshal(cloudEvent.Data.([]byte), &yourData)
	//
	Data any

	// DataContentType is the content type of [Data] value (ex: "text/xml")
	DataContentType *string

	// DataSchema identifies the schema that Data adheres to.
	DataSchema *string

	// Extensions are attributes that are serialized as siblings to attributes like Data.
	Extensions map[string]any

	// Subject of the event, in the context of the event producer (identified by Source).
	Subject *string

	// Time represents the time this event occurred.
	Time *time.Time
}

CloudEvent represents an event conforming to the CloudEvents 1.0 spec. See here for more details: https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/spec.md

func NewCloudEvent

func NewCloudEvent(source string, eventType string, data any, options *CloudEventOptions) (CloudEvent, error)

NewCloudEvent creates a CloudEvent.

  • source - Identifies the context in which an event happened. The combination of id and source must be unique for each distinct event.
  • eventType - Type of event related to the originating occurrence.
  • data - data to be added to the event. Can be a []byte, or any JSON serializable type, or nil.
  • options - additional fields that are not required.

func (CloudEvent) MarshalJSON

func (ce CloudEvent) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for CloudEvent.

func (*CloudEvent) UnmarshalJSON

func (ce *CloudEvent) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for CloudEvent.

type CloudEventOptions

type CloudEventOptions struct {
	// DataContentType is the content type of [Data] value (ex: "text/xml")
	DataContentType *string

	// DataSchema identifies the schema that Data adheres to.
	DataSchema *string

	// Extensions are attributes that are serialized as siblings to attributes like Data.
	Extensions map[string]any

	// Subject of the event, in the context of the event producer (identified by Source).
	Subject *string

	// Time represents the time this event occurred.
	// Defaults to time.Now().UTC()
	Time *time.Time
}

CloudEventOptions are options for the NewCloudEvent function.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL