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?

- [ ]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x]  No

#### Type of change

- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [x] 🧹 Tech Debt/Cleanup

#### Issue(s)

* #5124

#### Test Plan

- [ ] 💪 Manual
- [x]  Unit test
- [ ] 💚 E2E
This commit is contained in:
ashmrtn 2024-01-29 11:53:22 -08:00 committed by GitHub
parent 8a7a61f05d
commit 6ef2c2d494
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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())))
}
}