Skip to content

Commit

Permalink
Do some truly heinous typescript stuff to get nested objects in seed_…
Browse files Browse the repository at this point in the history
…type object to work.

Object now does the main thing it's supposed to.

Part of #18.
  • Loading branch information
jkomoros committed Jun 27, 2023
1 parent 4728184 commit 8897ad4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 11 deletions.
25 changes: 18 additions & 7 deletions src/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
SeedData,
ExpandedSeedPacket,
seedData,
SeedReference
SeedReference,
nestedSeedDataObject,
SeedDataObject
} from './types.js';

import {
Expand All @@ -29,10 +31,20 @@ const expandSeedData = (idFromParent : SeedID, data : SeedData, result : Expande

const id = data.id !== undefined ? data.id : idFromParent;

const resultData = {...data} as ExpandedSeedData;
//TODO: if it's a SeedDataObject, iterate through and set the properties.()
//object instead of the top-level object.
for (const [key, value] of Object.entries(data)) {
const resultSeed = {...data} as ExpandedSeedData;
let resultData = resultSeed as {[key : string]: Value | SeedReference | SeedData};

//resultSeed and resultData are the same object in most cases, but not if
//the seed is of type object, where we should basically do the normal
//sub-seed expansion entirely within the properties argument.
if (nestedSeedDataObject.safeParse(data).success) {
const properties = (data as SeedDataObject).properties;
resultData = {...properties};
//eslint-disable-next-line @typescript-eslint/no-explicit-any
(resultSeed as SeedDataObject).properties = (resultData as any);
}

for (const [key, value] of Object.entries(resultData)) {
//if it's a reserved key, a normal value, or a SeedReference, then the copied over value is fine.

//Even though Typescript doesn't realize the value might be a SeedData, zod won't be fooled.
Expand All @@ -52,8 +64,7 @@ const expandSeedData = (idFromParent : SeedID, data : SeedData, result : Expande
//eslint-disable-next-line @typescript-eslint/no-explicit-any
(resultData as any)[key] = subReference;
}

result.seeds[id] = resultData;
result.seeds[id] = resultSeed;
return id;
};

Expand Down
9 changes: 5 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,16 @@ const seedDataObject = seedDataBase.extend({
properties: z.record(z.string(), makeSeedReferenceProperty(value))
});

const nestedSeedDataObject = seedDataBase.extend({
const lazySeedData = z.lazy(() => seedData) as never;

export const nestedSeedDataObject = seedDataBase.extend({
type: z.literal('object'),
properties: z.record(z.string(), z.union([
z.lazy(() => seedData),
lazySeedData,
seedReference,
value
]))
}) as never;
//^ This 'as never' is the only thing I found to make this build.
});

export type SeedDataObject = z.infer<typeof seedDataObject>;

Expand Down
10 changes: 10 additions & 0 deletions test/base/a_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
"a": 5,
"b": true
}
},
"computed-object": {
"type": "object",
"properties": {
"a": {
"type": "log",
"value": 5
},
"b": true
}
}
}
}
49 changes: 49 additions & 0 deletions test/base/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,17 @@ describe('Garden smoke test', () => {
assert.deepStrictEqual(result, golden);
});

it ('testing computed object seed', async () => {
const garden = loadTestGarden();
const seed = await garden.seed('computed-object');
const result = await seed.grow();
const golden = {
a: 5,
b: true
};
assert.deepStrictEqual(result, golden);
});


});

Expand Down Expand Up @@ -424,6 +435,44 @@ describe('expandSeedPacket tests', () => {
};
assert.deepStrictEqual(result, golden);
});

it('seed-type object nested', async () => {
const packet = seedPacket.parse({
version: 0,
seeds: {
'': {
'type': 'object',
'properties': {
'a' : {
'type': 'log',
'value': true
},
'b': true
}
}
}
});
const result = expandSeedPacket(packet);
const golden : ExpandedSeedPacket = {
version: 0,
seeds: {
'': {
'type': 'object',
'properties': {
'a': {
'id': '-a'
},
'b': true,
}
},
'-a': {
'type': 'log',
'value': true
}
}
};
assert.deepStrictEqual(result, golden);
});
});


Expand Down

0 comments on commit 8897ad4

Please sign in to comment.