Skip to content

Commit 19f6749

Browse files
authored
Merge pull request Sofie-Automation#1651 from evs-broadcast/contribute/objectWithOverrides-array-support
Fix objectWithOverrides to correctly handle arrays
2 parents 8bda2b0 + 30487a4 commit 19f6749

2 files changed

Lines changed: 34 additions & 17 deletions

File tree

packages/corelib/src/settings/__tests__/objectWithOverrides.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ interface BasicType {
1313
valC: number
1414
valD?: string
1515
}
16+
valE?: [
17+
{
18+
valF: number
19+
valG: string
20+
},
21+
]
1622
}
1723

1824
describe('applyAndValidateOverrides', () => {
@@ -159,6 +165,7 @@ describe('applyAndValidateOverrides', () => {
159165
valC: 5,
160166
valD: 'xyz',
161167
},
168+
valE: [{ valF: 27, valG: 'hij' }],
162169
}
163170

164171
const inputObjWithOverrides: ObjectWithOverrides<BasicType> = {
@@ -172,6 +179,7 @@ describe('applyAndValidateOverrides', () => {
172179
valC: 6,
173180
valD: 'uvw',
174181
},
182+
valE: [{ valF: 32, valG: 'klm' }],
175183
}
176184

177185
const res = updateOverrides(inputObjWithOverrides, updateObj)
@@ -185,11 +193,13 @@ describe('applyAndValidateOverrides', () => {
185193
valC: 5,
186194
valD: 'xyz',
187195
},
196+
valE: [{ valF: 27, valG: 'hij' }],
188197
},
189198
overrides: [
190199
{ op: 'set', path: 'valB.valD', value: 'uvw' },
191200
{ op: 'set', path: 'valA', value: 'def' },
192201
{ op: 'set', path: 'valB.valC', value: 6 },
202+
{ op: 'set', path: 'valE', value: [{ valF: 32, valG: 'klm' }] },
193203
],
194204
})
195205
)

packages/corelib/src/settings/objectWithOverrides.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,30 @@ function recursivelyGenerateOverrides<T extends object>(
150150
})
151151
continue
152152
}
153-
if (Array.isArray(rawValue) && !_.isEqual(curValue, rawValue)) {
154-
outOverrides.push({
155-
op: 'set',
156-
path: fullKeyPathString,
157-
value: rawValue,
158-
})
159-
}
160-
if (typeof curValue === 'object' && curValue !== null && typeof rawValue === 'object' && rawValue !== null) {
161-
recursivelyGenerateOverrides(curValue, rawValue, fullKeyPath, outOverrides)
162-
continue
163-
}
164-
if (curValue !== rawValue) {
165-
outOverrides.push({
166-
op: 'set',
167-
path: fullKeyPathString,
168-
value: rawValue,
169-
})
153+
if (Array.isArray(rawValue)) {
154+
if (!_.isEqual(curValue, rawValue))
155+
outOverrides.push({
156+
op: 'set',
157+
path: fullKeyPathString,
158+
value: rawValue,
159+
})
160+
} else {
161+
if (
162+
typeof curValue === 'object' &&
163+
curValue !== null &&
164+
typeof rawValue === 'object' &&
165+
rawValue !== null
166+
) {
167+
recursivelyGenerateOverrides(curValue, rawValue, fullKeyPath, outOverrides)
168+
continue
169+
}
170+
if (curValue !== rawValue) {
171+
outOverrides.push({
172+
op: 'set',
173+
path: fullKeyPathString,
174+
value: rawValue,
175+
})
176+
}
170177
}
171178
}
172179
for (const [rawKey, rawValue] of Object.entries<any>(rawObj)) {

0 commit comments

Comments
 (0)