GC: Use of Ptr extension (#2477)
## Description Use of Pointer package in /connector/exchange <!-- Insert PR description--> ## Does this PR need a docs update or release note? Wanted to extend the package to generics, but it is incomplete - [x] ⛔ No ## Type of change <!--- Please check the type of change your PR introduces: ---> - [x] 🌻 Feature ## Issue(s) <!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. --> * related #2474<issue> ## Test Plan - [x] ⚡ Unit test
This commit is contained in:
parent
6b6f684119
commit
eda4699d1a
@ -1,13 +1,20 @@
|
|||||||
package ptr
|
package ptr
|
||||||
|
|
||||||
// Val helper method for unwrapping strings
|
// ptr package is a common package used for pointer
|
||||||
|
// access and deserialization.
|
||||||
|
|
||||||
|
// Val Generic function for dereferencing pointers.
|
||||||
// Microsoft Graph saves many variables as string pointers.
|
// Microsoft Graph saves many variables as string pointers.
|
||||||
// Function will safely check if the point is nil prior to
|
// Function will safely check if the point is nil prior to
|
||||||
// dereferencing the pointer. If the pointer is nil,
|
// dereferencing the pointer. If the pointer is nil,
|
||||||
// an empty string is returned.
|
// an empty version of the object is returned.
|
||||||
func Val(ptr *string) string {
|
// Operation does not work on Nested objects.
|
||||||
|
// For example:
|
||||||
|
// *evt.GetEnd().GetDateTime() will still cause a panic
|
||||||
|
// if evt is nil or GetEnd() is nil
|
||||||
|
func Val[T any](ptr *T) T {
|
||||||
if ptr == nil {
|
if ptr == nil {
|
||||||
return ""
|
return *new(T)
|
||||||
}
|
}
|
||||||
|
|
||||||
return *ptr
|
return *ptr
|
||||||
|
|||||||
99
src/internal/common/ptr/pointer_test.go
Normal file
99
src/internal/common/ptr/pointer_test.go
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
package ptr_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
|
|
||||||
|
"github.com/alcionai/corso/src/internal/common/ptr"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PointerSuite struct {
|
||||||
|
suite.Suite
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPointerSuite(t *testing.T) {
|
||||||
|
suite.Run(t, new(PointerSuite))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestVal checks to ptr derefencing for the
|
||||||
|
// following types:
|
||||||
|
// - *string
|
||||||
|
// - *bool
|
||||||
|
// - *time.Time
|
||||||
|
func (suite *PointerSuite) TestVal() {
|
||||||
|
var (
|
||||||
|
t = suite.T()
|
||||||
|
created *time.Time
|
||||||
|
testString *string
|
||||||
|
testBool *bool
|
||||||
|
testInt *int
|
||||||
|
testInt32 *int32
|
||||||
|
testInt64 *int64
|
||||||
|
)
|
||||||
|
|
||||||
|
// String Checks
|
||||||
|
subject := ptr.Val(testString)
|
||||||
|
assert.Empty(t, subject)
|
||||||
|
|
||||||
|
hello := "Hello World"
|
||||||
|
testString = &hello
|
||||||
|
subject = ptr.Val(testString)
|
||||||
|
|
||||||
|
t.Logf("Received: %s", subject)
|
||||||
|
assert.NotEmpty(t, subject)
|
||||||
|
|
||||||
|
// Time Checks
|
||||||
|
|
||||||
|
myTime := ptr.Val(created)
|
||||||
|
assert.Empty(t, myTime)
|
||||||
|
assert.NotNil(t, myTime)
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
created = &now
|
||||||
|
myTime = ptr.Val(created)
|
||||||
|
assert.NotEmpty(t, myTime)
|
||||||
|
|
||||||
|
// Bool Checks
|
||||||
|
truth := true
|
||||||
|
myBool := ptr.Val(testBool)
|
||||||
|
assert.NotNil(t, myBool)
|
||||||
|
assert.False(t, myBool)
|
||||||
|
|
||||||
|
testBool = &truth
|
||||||
|
myBool = ptr.Val(testBool)
|
||||||
|
assert.NotNil(t, myBool)
|
||||||
|
assert.True(t, myBool)
|
||||||
|
|
||||||
|
// Int checks
|
||||||
|
myInt := ptr.Val(testInt)
|
||||||
|
myInt32 := ptr.Val(testInt32)
|
||||||
|
myInt64 := ptr.Val(testInt64)
|
||||||
|
|
||||||
|
assert.NotNil(t, myInt)
|
||||||
|
assert.NotNil(t, myInt32)
|
||||||
|
assert.NotNil(t, myInt64)
|
||||||
|
assert.Empty(t, myInt)
|
||||||
|
assert.Empty(t, myInt32)
|
||||||
|
assert.Empty(t, myInt64)
|
||||||
|
|
||||||
|
num := 4071
|
||||||
|
num32 := int32(num * 32)
|
||||||
|
num64 := int64(num * 2048)
|
||||||
|
testInt = &num
|
||||||
|
testInt32 = &num32
|
||||||
|
testInt64 = &num64
|
||||||
|
|
||||||
|
myInt = ptr.Val(testInt)
|
||||||
|
myInt32 = ptr.Val(testInt32)
|
||||||
|
myInt64 = ptr.Val(testInt64)
|
||||||
|
|
||||||
|
assert.NotNil(t, myInt)
|
||||||
|
assert.NotNil(t, myInt32)
|
||||||
|
assert.NotNil(t, myInt64)
|
||||||
|
assert.NotEmpty(t, myInt)
|
||||||
|
assert.NotEmpty(t, myInt32)
|
||||||
|
assert.NotEmpty(t, myInt64)
|
||||||
|
}
|
||||||
@ -3,7 +3,6 @@ package api
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/alcionai/clues"
|
"github.com/alcionai/clues"
|
||||||
"github.com/hashicorp/go-multierror"
|
"github.com/hashicorp/go-multierror"
|
||||||
@ -13,6 +12,7 @@ import (
|
|||||||
"github.com/microsoftgraph/msgraph-sdk-go/users"
|
"github.com/microsoftgraph/msgraph-sdk-go/users"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
"github.com/alcionai/corso/src/internal/common/ptr"
|
||||||
"github.com/alcionai/corso/src/internal/connector/graph"
|
"github.com/alcionai/corso/src/internal/connector/graph"
|
||||||
"github.com/alcionai/corso/src/internal/connector/graph/api"
|
"github.com/alcionai/corso/src/internal/connector/graph/api"
|
||||||
"github.com/alcionai/corso/src/internal/connector/support"
|
"github.com/alcionai/corso/src/internal/connector/support"
|
||||||
@ -317,16 +317,8 @@ func (c Contacts) Serialize(
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
func ContactInfo(contact models.Contactable) *details.ExchangeInfo {
|
func ContactInfo(contact models.Contactable) *details.ExchangeInfo {
|
||||||
name := ""
|
name := ptr.Val(contact.GetDisplayName())
|
||||||
created := time.Time{}
|
created := ptr.Val(contact.GetCreatedDateTime())
|
||||||
|
|
||||||
if contact.GetDisplayName() != nil {
|
|
||||||
name = *contact.GetDisplayName()
|
|
||||||
}
|
|
||||||
|
|
||||||
if contact.GetCreatedDateTime() != nil {
|
|
||||||
created = *contact.GetCreatedDateTime()
|
|
||||||
}
|
|
||||||
|
|
||||||
return &details.ExchangeInfo{
|
return &details.ExchangeInfo{
|
||||||
ItemType: details.ExchangeContact,
|
ItemType: details.ExchangeContact,
|
||||||
|
|||||||
@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/alcionai/corso/src/internal/common"
|
"github.com/alcionai/corso/src/internal/common"
|
||||||
|
"github.com/alcionai/corso/src/internal/common/ptr"
|
||||||
"github.com/alcionai/corso/src/internal/connector/graph"
|
"github.com/alcionai/corso/src/internal/connector/graph"
|
||||||
"github.com/alcionai/corso/src/internal/connector/graph/api"
|
"github.com/alcionai/corso/src/internal/connector/graph/api"
|
||||||
"github.com/alcionai/corso/src/internal/connector/support"
|
"github.com/alcionai/corso/src/internal/connector/support"
|
||||||
@ -390,11 +391,12 @@ func (c CalendarDisplayable) GetParentFolderId() *string {
|
|||||||
|
|
||||||
func EventInfo(evt models.Eventable) *details.ExchangeInfo {
|
func EventInfo(evt models.Eventable) *details.ExchangeInfo {
|
||||||
var (
|
var (
|
||||||
organizer, subject string
|
organizer string
|
||||||
recurs bool
|
subject = ptr.Val(evt.GetSubject())
|
||||||
start = time.Time{}
|
recurs bool
|
||||||
end = time.Time{}
|
start = time.Time{}
|
||||||
created = time.Time{}
|
end = time.Time{}
|
||||||
|
created = ptr.Val(evt.GetCreatedDateTime())
|
||||||
)
|
)
|
||||||
|
|
||||||
if evt.GetOrganizer() != nil &&
|
if evt.GetOrganizer() != nil &&
|
||||||
@ -405,10 +407,6 @@ func EventInfo(evt models.Eventable) *details.ExchangeInfo {
|
|||||||
GetAddress()
|
GetAddress()
|
||||||
}
|
}
|
||||||
|
|
||||||
if evt.GetSubject() != nil {
|
|
||||||
subject = *evt.GetSubject()
|
|
||||||
}
|
|
||||||
|
|
||||||
if evt.GetRecurrence() != nil {
|
if evt.GetRecurrence() != nil {
|
||||||
recurs = true
|
recurs = true
|
||||||
}
|
}
|
||||||
@ -437,10 +435,6 @@ func EventInfo(evt models.Eventable) *details.ExchangeInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if evt.GetCreatedDateTime() != nil {
|
|
||||||
created = *evt.GetCreatedDateTime()
|
|
||||||
}
|
|
||||||
|
|
||||||
return &details.ExchangeInfo{
|
return &details.ExchangeInfo{
|
||||||
ItemType: details.ExchangeEvent,
|
ItemType: details.ExchangeEvent,
|
||||||
Organizer: organizer,
|
Organizer: organizer,
|
||||||
|
|||||||
@ -3,7 +3,6 @@ package api
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/alcionai/clues"
|
"github.com/alcionai/clues"
|
||||||
"github.com/hashicorp/go-multierror"
|
"github.com/hashicorp/go-multierror"
|
||||||
@ -13,6 +12,7 @@ import (
|
|||||||
"github.com/microsoftgraph/msgraph-sdk-go/users"
|
"github.com/microsoftgraph/msgraph-sdk-go/users"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
"github.com/alcionai/corso/src/internal/common/ptr"
|
||||||
"github.com/alcionai/corso/src/internal/connector/graph"
|
"github.com/alcionai/corso/src/internal/connector/graph"
|
||||||
"github.com/alcionai/corso/src/internal/connector/graph/api"
|
"github.com/alcionai/corso/src/internal/connector/graph/api"
|
||||||
"github.com/alcionai/corso/src/internal/connector/support"
|
"github.com/alcionai/corso/src/internal/connector/support"
|
||||||
@ -348,9 +348,9 @@ func (c Mail) Serialize(
|
|||||||
|
|
||||||
func MailInfo(msg models.Messageable) *details.ExchangeInfo {
|
func MailInfo(msg models.Messageable) *details.ExchangeInfo {
|
||||||
sender := ""
|
sender := ""
|
||||||
subject := ""
|
subject := ptr.Val(msg.GetSubject())
|
||||||
received := time.Time{}
|
received := ptr.Val(msg.GetReceivedDateTime())
|
||||||
created := time.Time{}
|
created := ptr.Val(msg.GetCreatedDateTime())
|
||||||
|
|
||||||
if msg.GetSender() != nil &&
|
if msg.GetSender() != nil &&
|
||||||
msg.GetSender().GetEmailAddress() != nil &&
|
msg.GetSender().GetEmailAddress() != nil &&
|
||||||
@ -358,18 +358,6 @@ func MailInfo(msg models.Messageable) *details.ExchangeInfo {
|
|||||||
sender = *msg.GetSender().GetEmailAddress().GetAddress()
|
sender = *msg.GetSender().GetEmailAddress().GetAddress()
|
||||||
}
|
}
|
||||||
|
|
||||||
if msg.GetSubject() != nil {
|
|
||||||
subject = *msg.GetSubject()
|
|
||||||
}
|
|
||||||
|
|
||||||
if msg.GetReceivedDateTime() != nil {
|
|
||||||
received = *msg.GetReceivedDateTime()
|
|
||||||
}
|
|
||||||
|
|
||||||
if msg.GetCreatedDateTime() != nil {
|
|
||||||
created = *msg.GetCreatedDateTime()
|
|
||||||
}
|
|
||||||
|
|
||||||
return &details.ExchangeInfo{
|
return &details.ExchangeInfo{
|
||||||
ItemType: details.ExchangeMail,
|
ItemType: details.ExchangeMail,
|
||||||
Sender: sender,
|
Sender: sender,
|
||||||
|
|||||||
@ -25,7 +25,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func attachmentType(attachment models.Attachmentable) models.AttachmentType {
|
func attachmentType(attachment models.Attachmentable) models.AttachmentType {
|
||||||
switch *attachment.GetOdataType() {
|
attachmentType := ptr.Val(attachment.GetOdataType())
|
||||||
|
switch attachmentType {
|
||||||
case fileAttachmentOdataValue:
|
case fileAttachmentOdataValue:
|
||||||
return models.FILE_ATTACHMENTTYPE
|
return models.FILE_ATTACHMENTTYPE
|
||||||
case itemAttachmentOdataValue:
|
case itemAttachmentOdataValue:
|
||||||
|
|||||||
@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/alcionai/corso/src/internal/common"
|
"github.com/alcionai/corso/src/internal/common"
|
||||||
|
"github.com/alcionai/corso/src/internal/common/ptr"
|
||||||
"github.com/alcionai/corso/src/internal/connector/exchange/api"
|
"github.com/alcionai/corso/src/internal/connector/exchange/api"
|
||||||
"github.com/alcionai/corso/src/internal/connector/graph"
|
"github.com/alcionai/corso/src/internal/connector/graph"
|
||||||
"github.com/alcionai/corso/src/internal/connector/support"
|
"github.com/alcionai/corso/src/internal/connector/support"
|
||||||
@ -71,7 +72,7 @@ func RestoreExchangeContact(
|
|||||||
|
|
||||||
response, err := service.Client().UsersById(user).ContactFoldersById(destination).Contacts().Post(ctx, contact, nil)
|
response, err := service.Client().UsersById(user).ContactFoldersById(destination).Contacts().Post(ctx, contact, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
name := *contact.GetGivenName()
|
name := ptr.Val(contact.GetGivenName())
|
||||||
|
|
||||||
return nil, errors.Wrap(
|
return nil, errors.Wrap(
|
||||||
err,
|
err,
|
||||||
@ -146,7 +147,8 @@ func RestoreExchangeEvent(
|
|||||||
errs = support.WrapAndAppend(
|
errs = support.WrapAndAppend(
|
||||||
fmt.Sprintf(
|
fmt.Sprintf(
|
||||||
"uploading attachment for message %s: %s",
|
"uploading attachment for message %s: %s",
|
||||||
*transformedEvent.GetId(), support.ConnectorStackErrorTrace(err),
|
ptr.Val(transformedEvent.GetId()),
|
||||||
|
support.ConnectorStackErrorTrace(err),
|
||||||
),
|
),
|
||||||
err,
|
err,
|
||||||
errs,
|
errs,
|
||||||
@ -283,12 +285,8 @@ func SendMailToBackStore(
|
|||||||
|
|
||||||
for _, attachment := range attached {
|
for _, attachment := range attached {
|
||||||
if err := uploadAttachment(ctx, uploader, attachment); err != nil {
|
if err := uploadAttachment(ctx, uploader, attachment); err != nil {
|
||||||
if attachment.GetOdataType() != nil &&
|
if ptr.Val(attachment.GetOdataType()) == "#microsoft.graph.itemAttachment" {
|
||||||
*attachment.GetOdataType() == "#microsoft.graph.itemAttachment" {
|
name := ptr.Val(attachment.GetName())
|
||||||
var name string
|
|
||||||
if attachment.GetName() != nil {
|
|
||||||
name = *attachment.GetName()
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Ctx(ctx).Infow(
|
logger.Ctx(ctx).Infow(
|
||||||
"item attachment upload not successful. content not accepted by M365 server",
|
"item attachment upload not successful. content not accepted by M365 server",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user