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

handles multiple lookup field values #5112

Merged
merged 34 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6faf18c
provisions to identify fields with multiple value
HiteshRepo Jan 24, 2024
fa4ea5b
handles multiple persons list items
HiteshRepo Jan 24, 2024
26c957d
handles multiple lookup field values
HiteshRepo Jan 24, 2024
b770d02
fix test TestColumnDefinitionable_LegacyColumns
HiteshRepo Jan 24, 2024
573686c
Merge remote-tracking branch 'origin/lists-multiple' into person-mult…
HiteshRepo Jan 24, 2024
7c22a0a
Merge remote-tracking branch 'origin/person-multiple' into lookup-mul…
HiteshRepo Jan 24, 2024
43c678d
updates function name that sets multiple enabled by data type
HiteshRepo Jan 25, 2024
265ef59
resolves conflicts with lists-multiple
HiteshRepo Jan 25, 2024
162d32e
updates function name that sets multiple enabled by data type
HiteshRepo Jan 25, 2024
ea7c988
fix merge conflicts
HiteshRepo Jan 25, 2024
feedafe
Merge remote-tracking branch 'origin/person-multiple' into lookup-mul…
HiteshRepo Jan 25, 2024
e2f2732
skips setting odatatype for text columns
HiteshRepo Jan 25, 2024
5daae6f
merge conflicts with lists-multiple
HiteshRepo Jan 25, 2024
3d77b56
resolve conflicts with person-multiple
HiteshRepo Jan 25, 2024
98b7e5e
fix lint
HiteshRepo Jan 25, 2024
00f1405
resolve conflicts with base
HiteshRepo Jan 25, 2024
8cb30e6
Merge remote-tracking branch 'origin/person-multiple' into lookup-mul…
HiteshRepo Jan 25, 2024
01a524f
simplifies logic to distinguish namings
HiteshRepo Jan 25, 2024
9aa1ae7
resolve conflicts with base
HiteshRepo Jan 25, 2024
aac0d39
fix test TestColumnDefinitionable_LegacyColumns
HiteshRepo Jan 25, 2024
9b8aa6c
Merge remote-tracking branch 'origin/person-multiple' into lookup-mul…
HiteshRepo Jan 25, 2024
abdf620
Merge remote-tracking branch 'origin/main' into lists-multiple
HiteshRepo Jan 29, 2024
0c7572e
updates comment for colDetails names
HiteshRepo Jan 29, 2024
5774003
Merge remote-tracking branch 'origin/lists-multiple' into person-mult…
HiteshRepo Jan 29, 2024
20cee12
fixes nit
HiteshRepo Jan 29, 2024
fa4a6ca
Merge remote-tracking branch 'origin/person-multiple' into lookup-mul…
HiteshRepo Jan 29, 2024
55aed0d
address review comment
HiteshRepo Jan 29, 2024
193d1d3
address review comments
HiteshRepo Jan 29, 2024
9177702
address review comments
HiteshRepo Jan 29, 2024
75a13fb
fix type cast
HiteshRepo Jan 29, 2024
d250d72
Merge remote-tracking branch 'origin/person-multiple' into lookup-mul…
HiteshRepo Jan 29, 2024
7346d54
resolve conflicts with main
HiteshRepo Jan 30, 2024
65602ba
Merge remote-tracking branch 'origin/person-multiple' into lookup-mul…
HiteshRepo Jan 30, 2024
0485c51
resolve merge conflicts with main
HiteshRepo Jan 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/pkg/services/m365/api/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type columnDetails struct {
createFieldName string
getFieldName string
isPersonColumn bool
isLookupColumn bool
isMultipleEnabled bool
hasDefaultedToText bool
}
Expand Down Expand Up @@ -420,6 +421,16 @@ func setColumnType(
case orig.GetNumber() != nil:
newColumn.SetNumber(orig.GetNumber())
case orig.GetLookup() != nil:
colDetails.isLookupColumn = true
isMultipleEnabled := ptr.Val(orig.GetLookup().GetAllowMultipleValues())
colDetails.isMultipleEnabled = isMultipleEnabled
updatedName := colName + LookupIDFieldNamePart
colDetails.createFieldName = updatedName

if !isMultipleEnabled {
colDetails.getFieldName = updatedName
}

newColumn.SetLookup(orig.GetLookup())
case orig.GetThumbnail() != nil:
newColumn.SetThumbnail(orig.GetThumbnail())
Expand Down Expand Up @@ -540,7 +551,8 @@ func populateMultipleValues(val any, filteredData map[string]any, colDetails *co
return
}

if !colDetails.isPersonColumn {
if !colDetails.isPersonColumn &&
!colDetails.isLookupColumn {
return
}

Expand All @@ -550,11 +562,10 @@ func populateMultipleValues(val any, filteredData map[string]any, colDetails *co
}

lookupIDs := make([]float64, 0)
lookupKeys := []string{LookupIDKey, LookupValueKey, PersonEmailKey}

for _, nestedFields := range multiNestedFields {
md, ok := nestedFields.(map[string]any)
if !ok || !keys.HasKeys(md, lookupKeys...) {
if !ok || !keys.HasKeys(md, checkFields(colDetails)...) {
continue
}

Expand All @@ -569,6 +580,17 @@ func populateMultipleValues(val any, filteredData map[string]any, colDetails *co
filteredData[colDetails.createFieldName] = lookupIDs
}

func checkFields(colDetails *columnDetails) []string {
switch {
case colDetails.isLookupColumn:
return []string{LookupIDKey, LookupValueKey}
case colDetails.isPersonColumn:
return []string{LookupIDKey, LookupValueKey, PersonEmailKey}
default:
return []string{}
}
}

// when creating list items with multiple values for a single column
// we let the API know that we are sending a collection.
// Hence this adds an additional field '<columnName>@@odata.type'
Expand All @@ -587,7 +609,7 @@ func specifyODataType(filteredData map[string]any, colDetails *columnDetails, co
}

switch {
case colDetails.isPersonColumn:
case colDetails.isPersonColumn, colDetails.isLookupColumn:
filteredData[colName+ODataTypeFieldNamePart] = ODataTypeFieldNameIntVal
default:
filteredData[colName+ODataTypeFieldNamePart] = ODataTypeFieldNameStringVal
Expand Down
108 changes: 108 additions & 0 deletions src/pkg/services/m365/api/lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,41 @@ func (suite *ListsUnitSuite) TestColumnDefinitionable_ColumnType() {
colNames["pg-col"].createFieldName == "pg-colLookupId"
},
},
{
name: "lookup column, single value",
getOrig: func() models.ColumnDefinitionable {
lookupColumn := models.NewLookupColumn()

cd := models.NewColumnDefinition()
cd.SetLookup(lookupColumn)
cd.SetName(ptr.To("refer-col"))

return cd
},
checkFn: func(cd models.ColumnDefinitionable, colNames map[string]*columnDetails) bool {
return cd.GetLookup() != nil &&
colNames["refer-col"].getFieldName == "refer-colLookupId" &&
colNames["refer-col"].createFieldName == "refer-colLookupId"
},
},
{
name: "lookup column, multiple value",
getOrig: func() models.ColumnDefinitionable {
lookupColumn := models.NewLookupColumn()
lookupColumn.SetAllowMultipleValues(ptr.To(true))

cd := models.NewColumnDefinition()
cd.SetLookup(lookupColumn)
cd.SetName(ptr.To("refer-col"))

return cd
},
checkFn: func(cd models.ColumnDefinitionable, colNames map[string]*columnDetails) bool {
return cd.GetLookup() != nil &&
colNames["refer-col"].getFieldName == "refer-col" &&
colNames["refer-col"].createFieldName == "refer-colLookupId"
},
},
{
name: "defaulted to text column",
getOrig: func() models.ColumnDefinitionable {
Expand Down Expand Up @@ -848,6 +883,51 @@ func (suite *ListsUnitSuite) TestSetAdditionalDataByColumnNames() {
"[email protected]": "Collection(Edm.Int32)",
},
},
{
name: "lookup column, single value",
additionalData: map[string]any{
"ReferLookupId": ptr.To(10),
},
colDetails: map[string]*columnDetails{
"Refer": {
isLookupColumn: true,
getFieldName: "ReferLookupId",
createFieldName: "ReferLookupId",
},
},
assertFn: assert.False,
expectedResult: map[string]any{
"ReferLookupId": ptr.To(10),
},
},
{
name: "lookup column, multiple values",
additionalData: map[string]any{
"Refers": []any{
map[string]any{
"LookupId": ptr.To(float64(10)),
"LookupValue": ptr.To("item-1"),
},
map[string]any{
"LookupId": ptr.To(float64(11)),
"LookupValue": ptr.To("item-1"),
},
},
},
colDetails: map[string]*columnDetails{
"Refers": {
isLookupColumn: true,
isMultipleEnabled: true,
getFieldName: "Refers",
createFieldName: "RefersLookupId",
},
},
assertFn: assert.True,
expectedResult: map[string]any{
"RefersLookupId": []float64{10, 11},
"[email protected]": "Collection(Edm.Int32)",
},
},
}

for _, test := range tests {
Expand All @@ -861,6 +941,34 @@ func (suite *ListsUnitSuite) TestSetAdditionalDataByColumnNames() {
}
}

func (suite *ListsUnitSuite) TestCheckFields() {
t := suite.T()

tests := []struct {
name string
colDetails *columnDetails
expectedKeys []string
}{
{
name: "lookup column",
colDetails: &columnDetails{isLookupColumn: true},
expectedKeys: []string{LookupIDKey, LookupValueKey},
},
{
name: "person column",
colDetails: &columnDetails{isPersonColumn: true},
expectedKeys: []string{LookupIDKey, LookupValueKey, PersonEmailKey},
},
}

for _, test := range tests {
suite.Run(test.name, func() {
res := checkFields(test.colDetails)
assert.Equal(t, test.expectedKeys, res)
})
}
}

type ListsAPIIntgSuite struct {
tester.Suite
its intgTesterSetup
Expand Down
Loading