Skip to content

Commit

Permalink
Create verifySeedPacket. For now it mostly checks that each local ref…
Browse files Browse the repository at this point in the history
… is to a valid seed.

Not yet exercised.

Part of #39.
  • Loading branch information
jkomoros committed Jul 6, 2023
1 parent 0646458 commit 6ff186e
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
SeedDataObject,
nestedSeedDataArray,
SeedDataArray,
seedReference
seedReference,
seedPacket
} from './types.js';

import {
Expand All @@ -29,7 +30,14 @@ import {
import {
Environment
} from './environment.js';
import { safeName } from './util.js';

import {
safeName
} from './util.js';

import {
TypedObject
} from './typed-object.js';

//expandSeedData adds itself (and any sub-seeds) to the result. It returns the
//actual ID the seed decided on and registered itself with.
Expand Down Expand Up @@ -99,8 +107,8 @@ export const expandSeedPacket = (packet : SeedPacket) : ExpandedSeedPacket => {
return result;
};

//TODO: unexport
export const collectSeedReferences = (data : ExpandedSeedData) : SeedReference[] => {
const collectSeedReferences = (data : ExpandedSeedData) : SeedReference[] => {
//Should this return an object of key->ref instead?
const result : SeedReference[] = [];
for (const value of Object.values(data)) {
const parsed = seedReference.safeParse(value);
Expand All @@ -111,6 +119,19 @@ export const collectSeedReferences = (data : ExpandedSeedData) : SeedReference[]
return result;
};

export const verifySeedPacket = (packet : ExpandedSeedPacket) : void => {
//First sanity check we typecheck, throwing if not.
seedPacket.parse(packet);
for (const [id, data] of TypedObject.entries(packet.seeds)) {
const refs = collectSeedReferences(data);
for (const ref of refs) {
//We don't check external references
if (ref.packet) continue;
if (!packet.seeds[ref.id]) throw new Error(`Seed ${id} referenced a non-existent local seed: ${ref.id}`);
}
}
};

export class Seed<D extends ExpandedSeedData = ExpandedSeedData> {

_garden : Garden;
Expand Down

0 comments on commit 6ff186e

Please sign in to comment.