Skip to content

Commit f839de4

Browse files
[azcore/azeventgrid] Remove json.RawMessage usage in the public API (Azure#21282)
* azcore: Removing the json.RawMessage dependency in the public API (it wasn't part of the signature (formally) but it was part of the returned type. * Consume new pre-release azcore in azeventgrid.
1 parent 054fcba commit f839de4

File tree

7 files changed

+52
-21
lines changed

7 files changed

+52
-21
lines changed

sdk/azcore/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* Added function `SanitizePagerPollerPath` to the `server` package to centralize sanitization and formalize the contract.
88

99
### Breaking Changes
10+
11+
* `messaging.CloudEvent` deserializes JSON objects as `[]byte`, instead of `json.RawMessage`. See the documentation for CloudEvent.Data for more information.
12+
1013
> These changes affect only code written against beta versions `v1.7.0-beta.2` and `v1.8.0-beta.1`.
1114
* Removed parameter from method `Span.End()` and its type `tracing.SpanEndOptions`. This API GA'ed in `v1.2.0` so we cannot change it.
1215

sdk/azcore/messaging/cloud_event.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ type CloudEvent struct {
4444
// Data is the payload for the event.
4545
// * []byte will be serialized and deserialized as []byte.
4646
// * Any other type will be serialized to a JSON object and deserialized into
47-
// a [json.RawMessage].
47+
// a []byte, containing the JSON text.
4848
//
49-
// To deserialize a [json.RawMessage] into your chosen type:
49+
// To deserialize into your chosen type:
5050
//
5151
// var yourData *YourType
52-
// json.Unmarshal(cloudEvent.Data.(json.RawMessage), &yourData)
52+
// json.Unmarshal(cloudEvent.Data.([]byte), &yourData)
5353
//
5454
Data any
5555

@@ -240,7 +240,7 @@ func updateFieldFromValue(ce *CloudEvent, k string, raw json.RawMessage) error {
240240
//
241241
case "data":
242242
// let the user deserialize so they can put it into their own native type.
243-
ce.Data = raw
243+
ce.Data = []byte(raw)
244244
case "datacontenttype":
245245
return json.Unmarshal(raw, &ce.DataContentType)
246246
case "dataschema":

sdk/azcore/messaging/cloud_event_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestCloudEventJSONData(t *testing.T) {
6262
actualCE := roundTrip(t, ce)
6363

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

6767
require.Equal(t, data, *dest)
6868
}

sdk/azcore/messaging/example_usingcloudevent_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func Example_usingCloudEvent() {
3232

3333
var receivedData *sampleType
3434

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

sdk/messaging/azeventgrid/client_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,15 @@ func TestPublishingAndReceivingCloudEvents(t *testing.T) {
237237
SpecVersion: "1.0",
238238
Source: "hello-source",
239239
Type: "eventType",
240-
Data: json.RawMessage("\"hello world 1\""),
240+
Data: []byte("\"hello world 1\""),
241241
}, resp.Value[0].Event)
242242

243243
requireEqualCloudEvent(t, messaging.CloudEvent{
244244
SpecVersion: "1.0",
245245
Source: "hello-source",
246246
Type: "eventType",
247247
DataSchema: to.Ptr("https://dataschema"),
248-
Data: json.RawMessage("\"hello world 2\""),
248+
Data: []byte("\"hello world 2\""),
249249
DataContentType: to.Ptr("data content type"),
250250
Subject: to.Ptr("subject"),
251251
Extensions: map[string]any{
@@ -260,7 +260,7 @@ func TestPublishingAndReceivingCloudEvents(t *testing.T) {
260260
SpecVersion: "1.0",
261261
Source: "hello-source",
262262
Type: "eventType",
263-
Data: json.RawMessage(bytes),
263+
Data: []byte(bytes),
264264
}, resp.Value[2].Event)
265265

266266
ackArgs := azeventgrid.AcknowledgeOptions{}

sdk/messaging/azeventgrid/example_publish_and_receive_test.go

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package azeventgrid_test
55

66
import (
77
"context"
8-
"encoding/hex"
98
"encoding/json"
109
"errors"
1110
"fmt"
@@ -22,32 +21,54 @@ func Example_publishAndReceiveCloudEvents() {
2221
topicName := os.Getenv("EVENTGRID_TOPIC")
2322
subscriptionName := os.Getenv("EVENTGRID_SUBSCRIPTION")
2423

24+
if endpoint == "" || key == "" || topicName == "" || subscriptionName == "" {
25+
return
26+
}
27+
2528
client, err := azeventgrid.NewClientWithSharedKeyCredential(endpoint, key, nil)
2629

2730
if err != nil {
2831
panic(err)
2932
}
3033

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

3340
if err != nil {
3441
panic(err)
3542
}
3643

37-
fmt.Printf("ID: %s\n", eventWithString.Event.ID)
38-
fmt.Printf(" Body: %s\n", eventWithString.Event.Data.(string))
39-
fmt.Printf(" Delivery count: %d\n", eventWithString.BrokerProperties.DeliveryCount)
44+
fmt.Fprintf(os.Stderr, "Received an event with a string payload\n")
45+
fmt.Fprintf(os.Stderr, "ID: %s\n", eventWithString.Event.ID)
46+
47+
var str *string
48+
49+
if err := json.Unmarshal(eventWithString.Event.Data.([]byte), &str); err != nil {
50+
panic(err)
51+
}
52+
53+
fmt.Fprintf(os.Stderr, " Body: %s\n", *str) // prints 'Body: hello world'
54+
fmt.Fprintf(os.Stderr, " Delivery count: %d\n", eventWithString.BrokerProperties.DeliveryCount)
4055

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

4361
if err != nil {
4462
panic(err)
4563
}
4664

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

69+
//
70+
// Publish an event with a struct as the payload
71+
//
5172
type SampleData struct {
5273
Name string `json:"name"`
5374
}
@@ -59,13 +80,15 @@ func Example_publishAndReceiveCloudEvents() {
5980
}
6081

6182
var sampleData *SampleData
62-
if err := json.Unmarshal(eventWithStruct.Event.Data.(json.RawMessage), &sampleData); err != nil {
83+
if err := json.Unmarshal(eventWithStruct.Event.Data.([]byte), &sampleData); err != nil {
6384
panic(err)
6485
}
6586

66-
fmt.Printf("ID: %s\n", eventWithStruct.Event.ID)
67-
fmt.Printf(" Body: %#v\n", sampleData)
68-
fmt.Printf(" Delivery count: %d\n", eventWithStruct.BrokerProperties.DeliveryCount)
87+
fmt.Fprintf(os.Stderr, "ID: %s\n", eventWithStruct.Event.ID)
88+
fmt.Fprintf(os.Stderr, " Body: %#v\n", sampleData) // prints 'Body: &azeventgrid_test.SampleData{Name:"hello"}'
89+
fmt.Fprintf(os.Stderr, " Delivery count: %d\n", eventWithStruct.BrokerProperties.DeliveryCount)
90+
91+
// Output:
6992
}
7093

7194
func publishAndReceiveEvent(client *azeventgrid.Client, topicName string, subscriptionName string, payload any) (azeventgrid.ReceiveDetails, error) {

sdk/messaging/azeventgrid/go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ module github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventgrid
33
go 1.18
44

55
require (
6-
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.8.0-beta.1
6+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.8.0-beta.2
77
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0
88
github.com/joho/godotenv v1.5.1
99
github.com/stretchr/testify v1.7.0
1010
)
1111

12+
replace (
13+
// temporary until we officially release the next beta.
14+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.8.0-beta.2 => ../../azcore
15+
)
16+
1217
require (
1318
github.com/davecgh/go-spew v1.1.1 // indirect
1419
github.com/dnaeon/go-vcr v1.1.0 // indirect

0 commit comments

Comments
 (0)