Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
HiteshRepo committed Dec 15, 2023
1 parent 2021dfe commit bad10a4
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 110 deletions.
31 changes: 31 additions & 0 deletions src/internal/common/keys/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package keys

type Set map[string]struct{}

func (ks Set) HasKey(key string) bool {
if _, ok := ks[key]; ok {
return true
}

return false
}

func (ks Set) Keys() []string {
sliceKeys := make([]string, 0)

for k := range ks {
sliceKeys = append(sliceKeys, k)
}

return sliceKeys
}

func HasKeys(data map[string]any, keys ...string) bool {
for _, k := range keys {
if _, ok := data[k]; !ok {
return false
}
}

return true
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package maps
package keys

import (
"testing"
Expand All @@ -17,28 +17,28 @@ func TestKeySetTestSuite(t *testing.T) {
suite.Run(t, &KeySetTestSuite{Suite: tester.NewUnitSuite(t)})
}

func (suite *KeySetTestSuite) Test_HasKey() {
func (suite *KeySetTestSuite) TestHasKey() {
tests := []struct {
name string
keySet KeySet
keySet Set
key string
expect assert.BoolAssertionFunc
}{
{
name: "key exists in the set",
keySet: KeySet{"key1": {}, "key2": {}},
keySet: Set{"key1": {}, "key2": {}},
key: "key1",
expect: assert.True,
},
{
name: "key does not exist in the set",
keySet: KeySet{"key1": {}, "key2": {}},
keySet: Set{"key1": {}, "key2": {}},
key: "nonexistent",
expect: assert.False,
},
{
name: "empty set",
keySet: KeySet{},
keySet: Set{},
key: "key",
expect: assert.False,
},
Expand All @@ -51,20 +51,20 @@ func (suite *KeySetTestSuite) Test_HasKey() {
}
}

func (suite *KeySetTestSuite) Test_Keys() {
func (suite *KeySetTestSuite) TestKeys() {
tests := []struct {
name string
keySet KeySet
keySet Set
expect assert.ValueAssertionFunc
}{
{
name: "non-empty set",
keySet: KeySet{"key1": {}, "key2": {}},
keySet: Set{"key1": {}, "key2": {}},
expect: assert.NotEmpty,
},
{
name: "empty set",
keySet: KeySet{},
keySet: Set{},
expect: assert.Empty,
},
}
Expand All @@ -76,3 +76,47 @@ func (suite *KeySetTestSuite) Test_Keys() {
})
}
}

func (suite *KeySetTestSuite) TestHasKeys() {
tests := []struct {
name string
data map[string]any
keys []string
expect assert.BoolAssertionFunc
}{
{
name: "has all keys",
data: map[string]any{
"key1": "data1",
"key2": 2,
"key3": struct{}{},
},
keys: []string{"key1", "key2", "key3"},
expect: assert.True,
},
{
name: "has some keys",
data: map[string]any{
"key1": "data1",
"key2": 2,
},
keys: []string{"key1", "key2", "key3"},
expect: assert.False,
},
{
name: "has no key",
data: map[string]any{
"key1": "data1",
"key2": 2,
},
keys: []string{"key4", "key5", "key6"},
expect: assert.False,
},
}

for _, test := range tests {
suite.Run(test.name, func() {
test.expect(suite.T(), HasKeys(test.data, test.keys...))
})
}
}
21 changes: 0 additions & 21 deletions src/internal/common/maps/keyset.go

This file was deleted.

11 changes: 0 additions & 11 deletions src/internal/common/maps/maps.go

This file was deleted.

62 changes: 0 additions & 62 deletions src/internal/common/maps/maps_test.go

This file was deleted.

12 changes: 6 additions & 6 deletions src/pkg/services/m365/api/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/alcionai/clues"
"github.com/microsoftgraph/msgraph-sdk-go/models"

"github.com/alcionai/corso/src/internal/common/maps"
"github.com/alcionai/corso/src/internal/common/keys"
"github.com/alcionai/corso/src/internal/common/ptr"
"github.com/alcionai/corso/src/pkg/services/m365/api/graph"
)
Expand Down Expand Up @@ -61,13 +61,13 @@ var readOnlyAddressFieldNames = []string{
DispNameFieldName,
}

var legacyColumns = maps.KeySet{
var legacyColumns = keys.Set{
AttachmentsColumnName: {},
EditColumnName: {},
ContentTypeColumnDisplayName: {},
}

var readOnlyFieldNames = maps.KeySet{
var readOnlyFieldNames = keys.Set{
AttachmentsColumnName: {},
EditColumnName: {},
ContentTypeColumnName: {},
Expand Down Expand Up @@ -533,17 +533,17 @@ func retainPrimaryAddressField(additionalData map[string]any) {
}

func hasAddressFields(additionalData map[string]any) bool {
if !maps.HasKeys(additionalData, readOnlyAddressFieldNames...) {
if !keys.HasKeys(additionalData, readOnlyAddressFieldNames...) {
return false
}

for _, value := range additionalData {
nestedFields, ok := value.(map[string]any)
if !ok || maps.HasKeys(nestedFields, GeoLocFieldName) {
if !ok || keys.HasKeys(nestedFields, GeoLocFieldName) {
continue
}

if maps.HasKeys(nestedFields, addressFieldNames...) {
if keys.HasKeys(nestedFields, addressFieldNames...) {
return true
}
}
Expand Down

0 comments on commit bad10a4

Please sign in to comment.