From 858aa13f2aaca8e45085e778eaeaeabe4928dd52 Mon Sep 17 00:00:00 2001 From: ashmrtn Date: Mon, 5 Dec 2022 14:09:57 -0800 Subject: [PATCH] Create helper function for getting data from map (#1694) ## Description Helper function only deals with string values in the map. Returns empty string if the requested item was not present. Will also be useful for retrieving delta tokens in future patches. ## Type of change - [ ] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Test - [ ] :computer: CI/Deployment - [x] :hamster: Trivial/Minor ## Issue(s) * #1685 ## Test Plan - [ ] :muscle: Manual - [x] :zap: Unit test - [ ] :green_heart: E2E --- .../connector/exchange/service_iterators.go | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/internal/connector/exchange/service_iterators.go b/src/internal/connector/exchange/service_iterators.go index 3bf2a8fef..8609a305e 100644 --- a/src/internal/connector/exchange/service_iterators.go +++ b/src/internal/connector/exchange/service_iterators.go @@ -20,6 +20,25 @@ import ( const nextLinkKey = "@odata.nextLink" +// getAdditionalDataString gets a string value from the AdditionalData map. If +// the value is not in the map returns an empty string. +func getAdditionalDataString( + key string, + data map[string]any, +) string { + iface := data[key] + if iface == nil { + return "" + } + + value, ok := iface.(*string) + if !ok { + return "" + } + + return *value +} + // FilterContainersAndFillCollections is a utility function // that places the M365 object ids belonging to specific directories // into a Collection. Messages outside of those directories are omitted. @@ -252,17 +271,14 @@ func FetchContactIDsFromDirectory(ctx context.Context, gs graph.Service, user, d ids = append(ids, *item.GetId()) } - nextLinkIface := resp.GetAdditionalData()[nextLinkKey] - if nextLinkIface == nil { + addtlData := resp.GetAdditionalData() + + nextLink := getAdditionalDataString(nextLinkKey, addtlData) + if len(nextLink) == 0 { break } - nextLink := nextLinkIface.(*string) - if len(*nextLink) == 0 { - break - } - - builder = cdelta.NewDeltaRequestBuilder(*nextLink, gs.Adapter()) + builder = cdelta.NewDeltaRequestBuilder(nextLink, gs.Adapter()) } return ids, errs.ErrorOrNil() @@ -312,17 +328,14 @@ func FetchMessageIDsFromDirectory( ids = append(ids, *item.GetId()) } - nextLinkIface := resp.GetAdditionalData()[nextLinkKey] - if nextLinkIface == nil { + addtlData := resp.GetAdditionalData() + + nextLink := getAdditionalDataString(nextLinkKey, addtlData) + if len(nextLink) == 0 { break } - nextLink := nextLinkIface.(*string) - if len(*nextLink) == 0 { - break - } - - builder = mdelta.NewDeltaRequestBuilder(*nextLink, gs.Adapter()) + builder = mdelta.NewDeltaRequestBuilder(nextLink, gs.Adapter()) } return ids, errs.ErrorOrNil()