package messaging
import "github.com/Azure/azure-sdk-for-go/sdk/azcore/messaging"
Package messaging contains types used across messaging packages.
Code:play
Output: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
}
Custom field = hello, a custom field value
Index ¶
- type CloudEvent
- func NewCloudEvent(source string, eventType string, data any, options *CloudEventOptions) (CloudEvent, error)
- func (ce CloudEvent) MarshalJSON() ([]byte, error)
- func (ce *CloudEvent) UnmarshalJSON(data []byte) error
- type CloudEventOptions
Examples ¶
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.
Source Files ¶
- Version
- v1.9.0
- Published
- Nov 6, 2023
- Platform
- darwin/amd64
- Imports
- 7 packages
- Last checked
- 48 minutes ago –
Tools for package owners.