Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[azcore/azeventgrid] Remove json.RawMessage usage in the public API #21282

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sdk/azcore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
* Added function `SanitizePagerPollerPath` to the `server` package to centralize sanitization and formalize the contract.

### Breaking Changes

* `messaging.CloudEvent` deserializes JSON objects as `[]byte`, instead of `json.RawMessage`. See the documentation for CloudEvent.Data for more information.

> These changes affect only code written against beta versions `v1.7.0-beta.2` and `v1.8.0-beta.1`.
* Removed parameter from method `Span.End()` and its type `tracing.SpanEndOptions`. This API GA'ed in `v1.2.0` so we cannot change it.

Expand Down
8 changes: 4 additions & 4 deletions sdk/azcore/messaging/cloud_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ type CloudEvent struct {
// 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 [json.RawMessage].
// a []byte, containing the JSON text.
//
// To deserialize a [json.RawMessage] into your chosen type:
// To deserialize into your chosen type:
//
// var yourData *YourType
// json.Unmarshal(cloudEvent.Data.(json.RawMessage), &yourData)
// json.Unmarshal(cloudEvent.Data.([]byte), &yourData)
//
Data any
richardpark-msft marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -240,7 +240,7 @@ func updateFieldFromValue(ce *CloudEvent, k string, raw json.RawMessage) error {
//
case "data":
// let the user deserialize so they can put it into their own native type.
ce.Data = raw
ce.Data = []byte(raw)
case "datacontenttype":
return json.Unmarshal(raw, &ce.DataContentType)
case "dataschema":
Expand Down
2 changes: 1 addition & 1 deletion sdk/azcore/messaging/cloud_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestCloudEventJSONData(t *testing.T) {
actualCE := roundTrip(t, ce)

var dest *map[string]string
require.NoError(t, json.Unmarshal(actualCE.Data.(json.RawMessage), &dest))
require.NoError(t, json.Unmarshal(actualCE.Data.([]byte), &dest))

require.Equal(t, data, *dest)
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/azcore/messaging/example_usingcloudevent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Example_usingCloudEvent() {

var receivedData *sampleType

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

Expand Down
6 changes: 3 additions & 3 deletions sdk/messaging/azeventgrid/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,15 @@ func TestPublishingAndReceivingCloudEvents(t *testing.T) {
SpecVersion: "1.0",
Source: "hello-source",
Type: "eventType",
Data: json.RawMessage("\"hello world 1\""),
Data: []byte("\"hello world 1\""),
}, resp.Value[0].Event)

requireEqualCloudEvent(t, messaging.CloudEvent{
SpecVersion: "1.0",
Source: "hello-source",
Type: "eventType",
DataSchema: to.Ptr("https://dataschema"),
Data: json.RawMessage("\"hello world 2\""),
Data: []byte("\"hello world 2\""),
DataContentType: to.Ptr("data content type"),
Subject: to.Ptr("subject"),
Extensions: map[string]any{
Expand All @@ -260,7 +260,7 @@ func TestPublishingAndReceivingCloudEvents(t *testing.T) {
SpecVersion: "1.0",
Source: "hello-source",
Type: "eventType",
Data: json.RawMessage(bytes),
Data: []byte(bytes),
}, resp.Value[2].Event)

ackArgs := azeventgrid.AcknowledgeOptions{}
Expand Down
45 changes: 34 additions & 11 deletions sdk/messaging/azeventgrid/example_publish_and_receive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package azeventgrid_test

import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand All @@ -22,32 +21,54 @@ func Example_publishAndReceiveCloudEvents() {
topicName := os.Getenv("EVENTGRID_TOPIC")
subscriptionName := os.Getenv("EVENTGRID_SUBSCRIPTION")

if endpoint == "" || key == "" || topicName == "" || subscriptionName == "" {
return
}

client, err := azeventgrid.NewClientWithSharedKeyCredential(endpoint, key, nil)

if err != nil {
panic(err)
}

//
// Publish an event with a string payload
//
fmt.Fprintf(os.Stderr, "Published event with a string payload 'hello world'\n")
eventWithString, err := publishAndReceiveEvent(client, topicName, subscriptionName, "hello world")

if err != nil {
panic(err)
}

fmt.Printf("ID: %s\n", eventWithString.Event.ID)
fmt.Printf(" Body: %s\n", eventWithString.Event.Data.(string))
fmt.Printf(" Delivery count: %d\n", eventWithString.BrokerProperties.DeliveryCount)
fmt.Fprintf(os.Stderr, "Received an event with a string payload\n")
fmt.Fprintf(os.Stderr, "ID: %s\n", eventWithString.Event.ID)

var str *string

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

fmt.Fprintf(os.Stderr, " Body: %s\n", *str) // prints 'Body: hello world'
fmt.Fprintf(os.Stderr, " Delivery count: %d\n", eventWithString.BrokerProperties.DeliveryCount)

//
// Publish an event with a []byte payload
//
eventWithBytes, err := publishAndReceiveEvent(client, topicName, subscriptionName, []byte{0, 1, 2})

if err != nil {
panic(err)
}

fmt.Printf("ID: %s\n", eventWithBytes.Event.ID)
fmt.Printf(" Body: %s\n", hex.EncodeToString(eventWithBytes.Event.Data.([]byte)))
fmt.Printf(" Delivery count: %d\n", eventWithBytes.BrokerProperties.DeliveryCount)
fmt.Fprintf(os.Stderr, "ID: %s\n", eventWithBytes.Event.ID)
fmt.Fprintf(os.Stderr, " Body: %#v\n", eventWithBytes.Event.Data.([]byte)) // prints 'Body: []byte{0x0, 0x1, 0x2}'
fmt.Fprintf(os.Stderr, " Delivery count: %d\n", eventWithBytes.BrokerProperties.DeliveryCount)

//
// Publish an event with a struct as the payload
//
type SampleData struct {
Name string `json:"name"`
}
Expand All @@ -59,13 +80,15 @@ func Example_publishAndReceiveCloudEvents() {
}

var sampleData *SampleData
if err := json.Unmarshal(eventWithStruct.Event.Data.(json.RawMessage), &sampleData); err != nil {
if err := json.Unmarshal(eventWithStruct.Event.Data.([]byte), &sampleData); err != nil {
panic(err)
}

fmt.Printf("ID: %s\n", eventWithStruct.Event.ID)
fmt.Printf(" Body: %#v\n", sampleData)
fmt.Printf(" Delivery count: %d\n", eventWithStruct.BrokerProperties.DeliveryCount)
fmt.Fprintf(os.Stderr, "ID: %s\n", eventWithStruct.Event.ID)
fmt.Fprintf(os.Stderr, " Body: %#v\n", sampleData) // prints 'Body: &azeventgrid_test.SampleData{Name:"hello"}'
fmt.Fprintf(os.Stderr, " Delivery count: %d\n", eventWithStruct.BrokerProperties.DeliveryCount)

// Output:
}

func publishAndReceiveEvent(client *azeventgrid.Client, topicName string, subscriptionName string, payload any) (azeventgrid.ReceiveDetails, error) {
Expand Down
7 changes: 6 additions & 1 deletion sdk/messaging/azeventgrid/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ module github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventgrid
go 1.18

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.8.0-beta.1
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.8.0-beta.2
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0
github.com/joho/godotenv v1.5.1
github.com/stretchr/testify v1.7.0
)

replace (
// temporary until we officially release the next beta.
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.8.0-beta.2 => ../../azcore
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dnaeon/go-vcr v1.1.0 // indirect
Expand Down