Skip to content

Commit

Permalink
Fix a condition in array/object auto-rolling.
Browse files Browse the repository at this point in the history
If the sub-object only had seed-references, it wouldn't notice it should unroll (it would only unroll if
it was a nested seed).

Added a test to catch this behavior and ensure it works right.

Part of #42. Noticed while working on #37.
  • Loading branch information
jkomoros committed Jul 8, 2023
1 parent cd6dc44 commit 7e0e132
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ changes were made in it or any sub-keeys, changesMade will be true. If
changesMade is false, then the return result will === the argument.
*/
const expandSeedComputedObjects = <D extends SeedData | InputValue>(data : D, comingFrom : ComingFrom = '') : [result : D | SeedData, changesMade : boolean, isSeedData : boolean] => { if (!data || typeof data != 'object') return [data, false, false];
const expandSeedComputedObjects = <D extends SeedData | InputValue>(data : D, comingFrom : ComingFrom = '') : [result : D | SeedData, changesMade : boolean, isComputedObject : boolean] => { if (!data || typeof data != 'object') return [data, false, false];

//It's a seed reference, which is a leaf and fine.
//TODO: shouldn't I use a more explicit test?
if ('seed' in data) return [data, false, false];
if ('seed' in data) return [data, false, true];

//We check for type in data, and not seedData.parse, because if there are
//nested arrays and objects with seedData in they will fail the seedData
Expand Down Expand Up @@ -153,14 +153,14 @@ const expandSeedComputedObjects = <D extends SeedData | InputValue>(data : D, co
if (Array.isArray(data)) {
const clone = [...data] as InputValueArray;
let changesMade = false;
let containsSeedData = false;
let containsComputed = false;
for (const [i, value] of data.entries()) {
const [modifiedValue, localChangesMade, localContainsSeedData] = expandSeedComputedObjects(value);
const [modifiedValue, localChangesMade, localContainsComputed] = expandSeedComputedObjects(value);
if (localChangesMade) changesMade = true;
if (localContainsSeedData) containsSeedData = true;
if (localContainsComputed) containsComputed = true;
clone[i] = modifiedValue as InputValue;
}
if (changesMade || containsSeedData) {
if (changesMade || containsComputed) {
//TODO: why do I have to do this unncessary and incorrect cast to
//SeedDataArray to get typescript to be satisfied?
//eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -179,15 +179,15 @@ const expandSeedComputedObjects = <D extends SeedData | InputValue>(data : D, co
//it's a generic object.
const clone = {...data} as InputValueObject;
let changesMade = false;
let containsSeedData = false;
let containsComputed = false;
for (const [key, value] of TypedObject.entries(data)) {
const [modifiedValue, localChangesMade, localContainsSeedData] = expandSeedComputedObjects(value);
const [modifiedValue, localChangesMade, localContainsComputed] = expandSeedComputedObjects(value);
if (localChangesMade) changesMade = true;
if (localContainsSeedData) containsSeedData = true;
if (localContainsComputed) containsComputed = true;
//eslint-disable-next-line @typescript-eslint/no-explicit-any
(clone as any)[key] = modifiedValue;
}
if (changesMade || containsSeedData) {
if (changesMade || containsComputed) {
//TODO: why do I have to do this unncessary and incorrect cast to
//SeedDataObject to get typescript to be satisfied?
if (comingFrom == 'object') return [clone as SeedDataObject, true, true];
Expand Down
43 changes: 43 additions & 0 deletions test/base/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,49 @@ describe('expandSeedPacket tests', () => {
assert.deepStrictEqual(result, golden);
});

it('seed with auto-object single layer nested with seed ref', async () => {
const packet : SeedPacket = {
version: 0,
environment: {},
seeds: {
'': {
type: 'render',
template: '{{name}} is {{age}}',
vars: {
name: {
seed: 'other'
},
age: 3
}
}
}
};
const result = expandSeedPacket(packet);
const golden : ExpandedSeedPacket = {
version: 0,
environment: {},
seeds: {
'': {
type: 'render',
template: '{{name}} is {{age}}',
vars: {
seed: '-vars',
}
},
'-vars': {
type: 'object',
properties: {
name: {
seed: 'other'
},
age: 3
}
}
}
};
assert.deepStrictEqual(result, golden);
});

it('seed with auto-object with auto-array inside layer nested', async () => {
const packet : SeedPacket = {
version: 0,
Expand Down

0 comments on commit 7e0e132

Please sign in to comment.