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

[azsystemevents] Regen to add in new ACS events #1

10 changes: 10 additions & 0 deletions sdk/messaging/eventgrid/azsystemevents/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Release History

## 0.3.0 (TBD)

### Features Added

- Added events ACSRouterWorkerUpdatedEventData and ACSAdvancedMessageDeliveryStatusUpdatedEventData. (PR#22638)

### Breaking Changes

- ACS events and constants have been changed to use an all-caps name (ex: AcsEmailDeliveryReportStatusDetails -> ACSEmailDeliveryReportStatusDetails).

## 0.2.0 (2024-03-14)

### Features Added
Expand Down
25 changes: 1 addition & 24 deletions sdk/messaging/eventgrid/azsystemevents/autorest.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: Azure Event Grid system events
generated-metadata: false
clear-output-folder: false
go: true
require: https://github.com/Azure/azure-rest-api-specs/blob/64819c695760764afa059d799fc7320d3fee33de/specification/eventgrid/data-plane/readme.md
require: https://github.com/Azure/azure-rest-api-specs/blob/c25006263b7c15ad5da755a5ef864e4301e56b54/specification/eventgrid/data-plane/readme.md
license-header: MICROSOFT_MIT_NO_VERSION
openapi-type: "data-plane"
output-folder: ../azsystemevents
Expand Down Expand Up @@ -144,26 +144,3 @@ directive:
}
return $;
```

Fix acronyms so they match our naming convention.

```yaml
directive:
- from:
- models.go
- models_serde.go
where: $
debug: true
transform: |
const acronyms = ["Acs", "Avs", "Iot"];
for (let acr of acronyms) {
// ex:
// '// AcsChatMessageDeletedEventData - Schema'
// 'type AcsChatMessageDeletedEventData struct'
// 'Participants []AcsChatThreadParticipantProperties'
// 'ParticipantRemoved *AcsChatThreadParticipantProperties'
const re = new RegExp(`([ *\\]])${acr}([A-Za-z0-9]+?(?:EventData|Properties))`, "sg");
$ = $.replace(re, `$1${acr.toUpperCase()}$2`);
}
return $;
```
2 changes: 1 addition & 1 deletion sdk/messaging/eventgrid/azsystemevents/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

//go:generate autorest ./autorest.md
//go:generate goimports -w ./..
//go:generate go run ./internal/generate generate
//go:generate go run ./internal/generate
//go:generate goimports -w ./..

package azsystemevents
274 changes: 179 additions & 95 deletions sdk/messaging/eventgrid/azsystemevents/constants.go

Large diffs are not rendered by default.

80 changes: 80 additions & 0 deletions sdk/messaging/eventgrid/azsystemevents/custom_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,90 @@

package azsystemevents

import (
"encoding/json"
"fmt"
"strings"
)

//type IotHubDeviceConnectedEventData DeviceConnectionStateEventProperties

func fixNAValue(s **string) {
if *s != nil && **s == "n/a" {
*s = nil
}
}

func unmarshalInternalACSAdvancedMessageChannelEventError(val json.RawMessage, fn string, re **Error) error {
var realErr *internalACSAdvancedMessageChannelEventError

if err := json.Unmarshal(val, &realErr); err != nil {
return fmt.Errorf("struct field %s: %v", fn, err)
}

*re = &Error{
Code: *realErr.ChannelCode,
message: *realErr.ChannelMessage,
}

return nil
}

func unpackMsg(err *internalAcsRouterCommunicationError, indent string) string {
if err == nil {
return ""
}

sb := strings.Builder{}

code := ""
if err.Code != nil {
code = *err.Code
}

message := ""
if err.Message != nil {
message = *err.Message
}

sb.WriteString(fmt.Sprintf("%sCode: %s\n%sMessage: %s\n", indent, code, indent, message))

if len(err.Details) > 0 {
for i, detailErr := range err.Details {
sb.WriteString(fmt.Sprintf("%sDetails[%d]:\n%s", indent, i, unpackMsg(&detailErr, indent+" ")))
}
}

if err.Innererror != nil {
sb.WriteString(fmt.Sprintf("%sInnerError:\n%s", indent, unpackMsg(err.Innererror, indent+" ")))
}

return sb.String()
}

func unmarshalInternalACSRouterCommunicationError(val json.RawMessage, fn string, re *[]*Error) error {
var tmp []internalAcsRouterCommunicationError

if err := json.Unmarshal(val, &tmp); err != nil {
return fmt.Errorf("struct field %s: %v", fn, err)
}

for _, se := range tmp {
code := ""

if se.Code != nil {
code = *se.Code
}

e := &Error{
Code: code,
// we're going to compress the remainder of these details into a
// string.
message: unpackMsg(&se, ""),
}

*re = append(*re, e)
}

return nil
}
231 changes: 231 additions & 0 deletions sdk/messaging/eventgrid/azsystemevents/custom_events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package azsystemevents

import (
"encoding/json"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/stretchr/testify/require"
)

func TestUnmarshalInternalACSRouterCommunicationError(t *testing.T) {
t.Run("null", func(t *testing.T) {
var unmarshalledErrs []*Error
err := unmarshalInternalACSRouterCommunicationError(json.RawMessage("null"), "FieldName", &unmarshalledErrs)
require.NoError(t, err)
require.Empty(t, unmarshalledErrs)

err = unmarshalInternalACSRouterCommunicationError(json.RawMessage("[{ \"Code\": null }]"), "FieldName", &unmarshalledErrs)
require.NoError(t, err)
require.Empty(t, unmarshalledErrs[0].Code)
})

t.Run("single", func(t *testing.T) {
text := `[
{
"code": "Failure",
"message": "Classification failed due to <reason>",
"target": null,
"innererror": {
"code": "InnerFailure",
"message": "Classification failed due to <reason>",
"target": null},
"details": null
}
]`

var unmarshalledErrs []*Error
err := unmarshalInternalACSRouterCommunicationError(json.RawMessage(text), "FieldName", &unmarshalledErrs)
require.NoError(t, err)

require.Equal(t, "Failure", unmarshalledErrs[0].Code)
require.Equal(t, "Code: Failure\n"+
"Message: Classification failed due to <reason>\n"+
"InnerError:\n"+
" Code: InnerFailure\n"+
" Message: Classification failed due to <reason>\n", unmarshalledErrs[0].Error())
})

t.Run("Multiple", func(t *testing.T) {
text := `[
{
"code": "Failure",
"message": "Classification failed due to <reason>"
},
{
"code": "FailureAsWell",
"message": "Classification failed due to <reason>"
}
]`

var unmarshalledErrs []*Error
err := unmarshalInternalACSRouterCommunicationError(json.RawMessage(text), "FieldName", &unmarshalledErrs)
require.NoError(t, err)

require.Equal(t, "Failure", unmarshalledErrs[0].Code)
require.Equal(t, "Code: Failure\n"+
"Message: Classification failed due to <reason>\n", unmarshalledErrs[0].Error())
require.Equal(t, "FailureAsWell", unmarshalledErrs[1].Code)
require.Equal(t, "Code: FailureAsWell\n"+
"Message: Classification failed due to <reason>\n", unmarshalledErrs[1].Error())
})
}

func TestUnmarshalInternalAcsRouterCCSmunicationErrorRecursive(t *testing.T) {
text := `[
{
"code": "Root.Failure",
"message": "Root.Message",
"innererror": {
"code": "Root->Inner.Failure",
"message": "Root->Inner.Message",
"innererror": {
"code": "Root->Inner->Inner.Failure",
"message": "Root->Inner->Inner.Message"
},
"details": [
{
"code": "Root->Inner->Details[0].Failure",
"message": "Root->Inner->Details[0].Message",
"innererror": {
"code": "Root->Inner->Details[0].Inner.Failure",
"message": "Root->Inner->Details[0].Inner.Message"
}
}
]
},
"details": [
{
"code": "Root->Details[0].Failure",
"message": "Root->Details[0].Message",
"innererror": {
"code": "Root->Details[0]->Inner.Failure",
"message": "Root->Details[0]->Inner.Message"
}
}
]
}
]`

var unmarshalledErrs []*Error
err := unmarshalInternalACSRouterCommunicationError(json.RawMessage(text), "FieldName", &unmarshalledErrs)
require.NoError(t, err)

require.NotEmpty(t, unmarshalledErrs)
require.Equal(t, "Root.Failure", unmarshalledErrs[0].Code)

expectedMsg := "Code: Root.Failure\n" +
"Message: Root.Message\n" +
"Details[0]:\n" +
" Code: Root->Details[0].Failure\n" +
" Message: Root->Details[0].Message\n" +
" InnerError:\n" +
" Code: Root->Details[0]->Inner.Failure\n" +
" Message: Root->Details[0]->Inner.Message\n" +
"InnerError:\n" +
" Code: Root->Inner.Failure\n" +
" Message: Root->Inner.Message\n" +
" Details[0]:\n" +
" Code: Root->Inner->Details[0].Failure\n" +
" Message: Root->Inner->Details[0].Message\n" +
" InnerError:\n" +
" Code: Root->Inner->Details[0].Inner.Failure\n" +
" Message: Root->Inner->Details[0].Inner.Message\n" +
" InnerError:\n" +
" Code: Root->Inner->Inner.Failure\n" +
" Message: Root->Inner->Inner.Message\n"

require.Equal(t, expectedMsg, unmarshalledErrs[0].Error())
}

func TestFormattingErrors(t *testing.T) {
t.Run("simple", func(t *testing.T) {
msg := unpackMsg(&internalAcsRouterCommunicationError{
Code: to.Ptr("code"),
Message: to.Ptr("message"),
}, "")

require.Equal(t, "Code: code\n"+
"Message: message\n", msg)
})

t.Run("InnerError", func(t *testing.T) {
msg := unpackMsg(&internalAcsRouterCommunicationError{
Code: to.Ptr("code"),
Message: to.Ptr("message"),
Innererror: &internalAcsRouterCommunicationError{
Code: to.Ptr("inner.code"),
Message: to.Ptr("inner.message"),
},
}, "")

require.Equal(t, "Code: code\n"+
"Message: message\n"+
"InnerError:\n"+
" Code: inner.code\n"+
" Message: inner.message\n", msg)
})

t.Run("InnerInnerError", func(t *testing.T) {
msg := unpackMsg(&internalAcsRouterCommunicationError{
Code: to.Ptr("code"),
Message: to.Ptr("message"),
Innererror: &internalAcsRouterCommunicationError{
Code: to.Ptr("inner.code"),
Message: to.Ptr("inner.message"),
Innererror: &internalAcsRouterCommunicationError{
Code: to.Ptr("inner->inner.code"),
Message: to.Ptr("inner->inner.message"),
},
},
}, "")

require.Equal(t, "Code: code\n"+
"Message: message\n"+
"InnerError:\n"+
" Code: inner.code\n"+
" Message: inner.message\n"+
" InnerError:\n"+
" Code: inner->inner.code\n"+
" Message: inner->inner.message\n", msg)
})

t.Run("Details", func(t *testing.T) {
msg := unpackMsg(&internalAcsRouterCommunicationError{
Code: to.Ptr("code"),
Message: to.Ptr("message"),
Details: []internalAcsRouterCommunicationError{
{
Code: to.Ptr("details[0].code"),
Message: to.Ptr("details[0].message"),
Details: []internalAcsRouterCommunicationError{
{
Code: to.Ptr("details[0]->details[0].code"),
Message: to.Ptr("details[0]->details[0].message"),
},
{
Code: to.Ptr("details[0]->details[1].code"),
Message: to.Ptr("details[0]->details[1].message"),
},
}},
},
}, "")

require.Equal(t, "Code: code\n"+
"Message: message\n"+
"Details[0]:\n"+
" Code: details[0].code\n"+
" Message: details[0].message\n"+
" Details[0]:\n"+
" Code: details[0]->details[0].code\n"+
" Message: details[0]->details[0].message\n"+
" Details[1]:\n"+
" Code: details[0]->details[1].code\n"+
" Message: details[0]->details[1].message\n", msg)
})

t.Run("Errors", func(t *testing.T) {
require.Empty(t, unpackMsg(nil, ""))
require.Equal(t, "Code: \nMessage: \n", unpackMsg(&internalAcsRouterCommunicationError{}, ""))
})
}
Loading