Skip to content

Commit

Permalink
Make it so property seed supports dotted property gets.
Browse files Browse the repository at this point in the history
This allows e.g. `a.2` to select from {a: [0,1,2]} to return 2.

Part of #50.
  • Loading branch information
jkomoros committed Jul 22, 2023
1 parent f0a7aaa commit 2bffd90
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ Selects a named property from an object
Required parameters:
- `object` - The object to select a property from. If some of the sub-keys need to be computed, nest a sub-seed of type `object`.
- `property` - The property to select from the object.
- `property` - The property to select from the object. It can be a "dotted" property to retrieve sub objects. For example `a.2` would fetch the `a` property from the object and then the `2` property from that object. (Note that due to the way that javascript handles arrays, fetching numbered items from an array works).
#### keys
Expand Down
5 changes: 2 additions & 3 deletions src/grow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import {
} from './types.js';

import {
assertUnreachable
assertUnreachable, getObjectProperty
} from './util.js';

import {
Expand Down Expand Up @@ -425,8 +425,7 @@ const growProperty = async (seed : Seed<SeedDataProperty>, env : Environment) :
const property = extractString(await getProperty(seed, env, data.property));
//obj might be an object, an array, or even an embedding. Whatever!

//eslint-disable-next-line @typescript-eslint/no-explicit-any
return (obj as any)[property];
return getObjectProperty(obj as {[name : string]: unknown}, property) as Value;
};

const growKeys = async (seed : Seed<SeedDataKeys>, env : Environment) : Promise<Value> => {
Expand Down
11 changes: 11 additions & 0 deletions test/base/a_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,17 @@
"two"
],
"delimiter": ":"
},
"dotted-property-test": {
"type": "property",
"object": {
"a": [
0,
1,
2
]
},
"property": "a.1"
}
}
}
8 changes: 8 additions & 0 deletions test/base/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,14 @@ Suffix`;
assert.deepStrictEqual(actual, golden);
});

it('dotted property test', async () => {
const garden = loadTestGarden();
const seed = await garden.seed('dotted-property-test');
const actual = await seed.grow();
const golden = 1;
assert.deepStrictEqual(actual, golden);
});

});

describe('expandSeedPacket tests', () => {
Expand Down

0 comments on commit 2bffd90

Please sign in to comment.