Skip to content

Commit

Permalink
SeedReference.location --> packet
Browse files Browse the repository at this point in the history
Part of #3.
  • Loading branch information
jkomoros committed Jun 24, 2023
1 parent 8202c8e commit 8e86176
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ Run `garden`. You can pick a non-default seed to grow by running `garden --seed
### Seed Types

All parameters can accept a literal value or a reference to another seed's
result (`{location: 'seed_packet_file.json', id: 'REFERENCE_ID'}`), unless otherwise noted.
result (`{packet: 'seed_packet_file.json', id: 'REFERENCE_ID'}`), unless otherwise noted.

The location can be any of:
The packet can be any of:
- An absolute https:// or http:// file
- A filepath relative to where the command is run from (in non-browser mode)
- A relative path to the original (e.g. '../b/file.json')
Expand Down
2 changes: 1 addition & 1 deletion seed-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{
"type": "object",
"properties": {
"location": {
"packet": {
"anyOf": [
{
"anyOf": [
Expand Down
2 changes: 1 addition & 1 deletion seeds/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"remote-example": {
"type": "log",
"value": {
"location": "https://raw.githubusercontent.com/jkomoros/prompt-garden/main/seeds/default.json",
"packet": "https://raw.githubusercontent.com/jkomoros/prompt-garden/main/seeds/default.json",
"id": "hello-world"
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ export class Garden {
async seed(ref : SeedID | SeedReference = '') : Promise<Seed> {
if (typeof ref == 'string') {
ref = {
location: this.location || '',
packet: this.location || '',
id: ref
};
}
const absoluteRef = makeAbsolute(ref, this.location || '');
//This will return early if it already is fetched
await this.ensureSeedPacket(absoluteRef.location);
const collection = this._seeds[absoluteRef.location];
await this.ensureSeedPacket(absoluteRef.packet);
const collection = this._seeds[absoluteRef.packet];
if (!collection) throw new Error('Unexpectedly no packet');
const seed = collection[ref.id];
if (!seed) throw new Error(`No seed with ID ${seedReferenceToString(ref)}`);
Expand Down Expand Up @@ -105,10 +105,10 @@ export class Garden {
}

plantSeed(ref : AbsoluteSeedReference, data : SeedData) {
if (this._seeds[ref.location] == undefined) {
this._seeds[ref.location] = {};
if (this._seeds[ref.packet] == undefined) {
this._seeds[ref.packet] = {};
}
this._seeds[ref.location][ref.id] = new Seed(this, ref, data);
this._seeds[ref.packet][ref.id] = new Seed(this, ref, data);
}

plantSeedPacket(location: SeedPacketAbsoluteLocation, packet: SeedPacket) {
Expand All @@ -117,7 +117,7 @@ export class Garden {
if (!this._location) this._location = location;
for (const [id, seed] of Object.entries(packet.seeds)) {
const ref : AbsoluteSeedReference = {
location,
packet: location,
id
};
this.plantSeed(ref, seed);
Expand Down
10 changes: 5 additions & 5 deletions src/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const isLocalLocation = (location : SeedPacketAbsoluteLocation) : boolean
};

export const seedReferenceToString = (ref : SeedReference) : string => {
let result = ref.location || '';
let result = ref.packet || '';
result += '#';
result += ref.id;
return result;
Expand All @@ -24,24 +24,24 @@ export const isRelativeSeedPacketLocation = (location : SeedPacketLocation) : lo
};

export const makeAbsolute = (ref : SeedReference, base : SeedPacketAbsoluteLocation) : AbsoluteSeedReference => {
const location = ref.location || '';
const location = ref.packet || '';
if (!isRelativeSeedPacketLocation(location)) {
return {
location: location || base,
packet: location || base,
id: ref.id
};
}
if (isLocalLocation(base)) {
const url = new URL(location, 'file://localhost/' + base);
return {
//TODO: this slices off the '/' assuming the base is a relative path from the current working directory.
location: url.pathname.slice(1),
packet: url.pathname.slice(1),
id: ref.id
};
}
const url = new URL(location, base);
return {
location: url.toString(),
packet: url.toString(),
id: ref.id
};
};
2 changes: 1 addition & 1 deletion src/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Seed<D extends SeedData = SeedData> {
}

get location() : SeedPacketAbsoluteLocation {
return this._ref.location;
return this._ref.packet;
}

get type() : SeedDataType {
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ export const seedPacketLocation = z.union([
export type SeedPacketLocation = z.infer<typeof seedPacketLocation>;

export const seedReference = z.object({
location: z.optional(seedPacketLocation),
packet: z.optional(seedPacketLocation),
id: seedID
});

export type SeedReference = z.infer<typeof seedReference>;

export const requiredSeedReference = z.object({
location: seedPacketAbsoluteLocation,
packet: seedPacketAbsoluteLocation,
id: seedID
});

Expand Down
2 changes: 1 addition & 1 deletion test/base/b_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"remote-ref": {
"type": "log",
"value": {
"location": "./a_test.json",
"packet": "./a_test.json",
"id": "true"
}
}
Expand Down
30 changes: 15 additions & 15 deletions test/base/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe('Garden smoke test', () => {

it('handles a seed from another file', async() => {
const garden = loadTestGarden();
const seed = await garden.seed({location: 'test/base/b_test.json', id: ''});
const seed = await garden.seed({packet: 'test/base/b_test.json', id: ''});
const result = await seed.grow();
const golden = 'test-other hello world';
assert.deepStrictEqual(result, golden);
Expand All @@ -151,7 +151,7 @@ describe('Garden smoke test', () => {
it('handles growing a seed that references a seed in another file', async () => {
//Garden will have both files loaded up, so it won't need to be fetched.
const garden = loadTestGarden();
const seed = await garden.seed({location: 'test/base/b_test.json', id: 'remote-ref'});
const seed = await garden.seed({packet: 'test/base/b_test.json', id: 'remote-ref'});
const result = await seed.grow();
const golden = true;
assert.deepStrictEqual(result, golden);
Expand All @@ -160,7 +160,7 @@ describe('Garden smoke test', () => {
it('handles growing a seed that references a seed in another file', async () => {
//Garden will have both files loaded up, so it won't need to be fetched.
const garden = loadTestGarden();
const seed = await garden.seed({location: 'test/base/b_test.json', id: 'remote-ref'});
const seed = await garden.seed({packet: 'test/base/b_test.json', id: 'remote-ref'});
const result = await seed.grow();
const golden = true;
assert.deepStrictEqual(result, golden);
Expand All @@ -169,7 +169,7 @@ describe('Garden smoke test', () => {
it('handles growing a seed that references a seed in another file that isn\'t loaded yet', async () => {
//Force garden to have only the first file loaded.
const garden = loadTestGarden(['test/base/b_test.json']);
const seed = await garden.seed({location: 'test/base/b_test.json', id: 'remote-ref'});
const seed = await garden.seed({packet: 'test/base/b_test.json', id: 'remote-ref'});
const result = await seed.grow();
const golden = true;
assert.deepStrictEqual(result, golden);
Expand All @@ -178,7 +178,7 @@ describe('Garden smoke test', () => {
it('handles growing a seed that references a seed in another file that isn\'t loaded yet with no files loaded yet', async () => {
//Force garden to have no files loaded to start
const garden = loadTestGarden([]);
const seed = await garden.seed({location: 'test/base/b_test.json', id: 'remote-ref'});
const seed = await garden.seed({packet: 'test/base/b_test.json', id: 'remote-ref'});
const result = await seed.grow();
const golden = true;
assert.deepStrictEqual(result, golden);
Expand All @@ -188,7 +188,7 @@ describe('Garden smoke test', () => {
//Create an empty garden with no fetch
const garden = loadTestGarden([], true);
try {
await garden.seed({location: 'test/base/b_test.json', id: 'remote-ref'});
await garden.seed({packet: 'test/base/b_test.json', id: 'remote-ref'});
} catch(err) {
//Err expected
return;
Expand Down Expand Up @@ -261,7 +261,7 @@ describe('makeAbsolute', () => {
it('absolute is no op', async() => {
const base = 'd/e.json';
const input : AbsoluteSeedReference = {
location: 'a/b/c.json',
packet: 'a/b/c.json',
id: 'foo'
};
const result = makeAbsolute(input, base);
Expand All @@ -272,12 +272,12 @@ describe('makeAbsolute', () => {
it('relative works for local', async() => {
const base = 'a/b/c.json';
const input : SeedReference = {
location: '../c/e.json',
packet: '../c/e.json',
id: 'foo'
};
const result = makeAbsolute(input, base);
const golden : AbsoluteSeedReference = {
location: 'a/c/e.json',
packet: 'a/c/e.json',
id: 'foo'
};
assert.deepStrictEqual(result, golden);
Expand All @@ -286,12 +286,12 @@ describe('makeAbsolute', () => {
it('relative works for https', async() => {
const base = 'https://localhost/a/b/c.json';
const input : SeedReference = {
location: '../c/e.json',
packet: '../c/e.json',
id: 'foo'
};
const result = makeAbsolute(input, base);
const golden : AbsoluteSeedReference = {
location: 'https://localhost/a/c/e.json',
packet: 'https://localhost/a/c/e.json',
id: 'foo'
};
assert.deepStrictEqual(result, golden);
Expand All @@ -300,12 +300,12 @@ describe('makeAbsolute', () => {
it('relative with dot works for local', async() => {
const base = 'a/b/c.json';
const input : SeedReference = {
location: './f/e.json',
packet: './f/e.json',
id: 'foo'
};
const result = makeAbsolute(input, base);
const golden : AbsoluteSeedReference = {
location: 'a/b/f/e.json',
packet: 'a/b/f/e.json',
id: 'foo'
};
assert.deepStrictEqual(result, golden);
Expand All @@ -314,12 +314,12 @@ describe('makeAbsolute', () => {
it('relative with dot works for https', async() => {
const base = 'https://localhost/a/b/c.json';
const input : SeedReference = {
location: './f/e.json',
packet: './f/e.json',
id: 'foo'
};
const result = makeAbsolute(input, base);
const golden : AbsoluteSeedReference = {
location: 'https://localhost/a/b/f/e.json',
packet: 'https://localhost/a/b/f/e.json',
id: 'foo'
};
assert.deepStrictEqual(result, golden);
Expand Down

0 comments on commit 8e86176

Please sign in to comment.