From 6ef2c2d494b56af3ff21897e50edbfba92b91a26 Mon Sep 17 00:00:00 2001 From: ashmrtn <3891298+ashmrtn@users.noreply.github.com> Date: Mon, 29 Jan 2024 11:53:22 -0800 Subject: [PATCH] Use type switch instead of strings of types (#5156) Instead of relying on strings pulled from the graph SDK to identify the types of the items we're dealing with use the type of the interface in golang. --- #### Does this PR need a docs update or release note? - [ ] :white_check_mark: Yes, it's included - [ ] :clock1: Yes, but in a later PR - [x] :no_entry: No #### Type of change - [ ] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Supportability/Tests - [ ] :computer: CI/Deployment - [x] :broom: Tech Debt/Cleanup #### Issue(s) * #5124 #### Test Plan - [ ] :muscle: Manual - [x] :zap: Unit test - [ ] :green_heart: E2E --- .../m365/collection/exchange/transform.go | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/internal/m365/collection/exchange/transform.go b/src/internal/m365/collection/exchange/transform.go index 5b9514874..75d1a010d 100644 --- a/src/internal/m365/collection/exchange/transform.go +++ b/src/internal/m365/collection/exchange/transform.go @@ -170,6 +170,7 @@ func insertStringToBody(body getContenter, newContent string) string { return newContent + content } +//nolint:lll // =============================================================================================== // Sanitization section // Set of functions that support ItemAttachemtable object restoration. @@ -187,14 +188,6 @@ func insertStringToBody(body getContenter, newContent string) string { // currently supported for Restore operations. // itemAttachments // support ODataType values -// -//nolint:lll -const ( - itemAttachment = "#microsoft.graph.itemAttachment" - eventItemType = "#microsoft.graph.event" - mailItemType = "#microsoft.graph.message" - contactItemType = "#microsoft.graph.contact" -) // toItemAttachment transforms internal item, OutlookItemables, into // objects that are able to be uploaded into M365. @@ -205,20 +198,16 @@ func toItemAttachment(orig models.Attachmentable) (models.Attachmentable, error) } item := transform.GetItem() - itemType := ptr.Val(item.GetOdataType()) - - switch itemType { - case contactItemType: - contact := item.(models.Contactable) - revised := sanitizeContact(contact) + switch val := item.(type) { + case models.Contactable: + revised := sanitizeContact(val) transform.SetItem(revised) return transform, nil - case eventItemType: - event := item.(models.Eventable) - newEvent, err := sanitizeEvent(event) + case models.Eventable: + newEvent, err := sanitizeEvent(val) if err != nil { return nil, err } @@ -226,10 +215,9 @@ func toItemAttachment(orig models.Attachmentable) (models.Attachmentable, error) transform.SetItem(newEvent) return transform, nil - case mailItemType: - message := item.(models.Messageable) - newMessage, err := sanitizeMessage(message) + case models.Messageable: + newMessage, err := sanitizeMessage(val) if err != nil { return nil, err } @@ -237,8 +225,12 @@ func toItemAttachment(orig models.Attachmentable) (models.Attachmentable, error) transform.SetItem(newMessage) return transform, nil + default: - return nil, clues.New(fmt.Sprintf("unsupported attachment type: %T", itemType)) + return nil, clues.New(fmt.Sprintf( + "unsupported attachment type: (%T) %s", + val, + ptr.Val(item.GetOdataType()))) } }