Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Use type switch instead of strings of types (#5156)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ashmrtn authored Jan 29, 2024
1 parent 8a7a61f commit 6ef2c2d
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions src/internal/m365/collection/exchange/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -205,40 +198,39 @@ 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
}

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
}

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

Expand Down

0 comments on commit 6ef2c2d

Please sign in to comment.