Skip to content

Commit

Permalink
A call seed_type which is used with function.
Browse files Browse the repository at this point in the history
Just sketched in, not clear if it works yet.

Part of #20.
  • Loading branch information
jkomoros committed Jul 29, 2023
1 parent fa0187c commit 616fdc1
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,16 @@ Required parameters:
- `arguments` - The object of name -> value pairs to set. These variable names should not include a namespace. Internally they will be prefixed with the `arg:` namespace.
- `block` - The sub-seed that will be evaluated where the environment will have `name=value`.
#### call
Calls a procedure that was defined by `function`.
Semantically this is just a let-multi to set parameters and then execute the function seed-reference, but it communicates intent better.
Required parameters:
- `arguments` - The object of name -> value pairs to set. These variable names should not include a namespace. Internally they will be prefixed with the `arg:` namespace.
- `function` - The sub-seed refererence to the `function` to execute.
#### store
Stores a value in the long-term key/val store.
Expand Down
78 changes: 78 additions & 0 deletions seed-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2957,6 +2957,84 @@
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"id": {
"$ref": "#/definitions/seedData/anyOf/0/properties/id"
},
"description": {
"$ref": "#/definitions/seedData/anyOf/0/properties/description"
},
"comment": {
"$ref": "#/definitions/seedData/anyOf/0/properties/comment"
},
"private": {
"$ref": "#/definitions/seedData/anyOf/0/properties/private"
},
"type": {
"type": "string",
"const": "call"
},
"arguments": {
"anyOf": [
{
"$ref": "#/definitions/seedData"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
},
{
"type": "object",
"additionalProperties": {
"anyOf": [
{
"$ref": "#/definitions/seedData/anyOf/27/properties/properties/additionalProperties/anyOf/0"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
},
{
"$ref": "#/definitions/seedData/anyOf/5/properties/value/anyOf/2/anyOf/1/additionalProperties/anyOf/2"
}
]
},
"propertyNames": {
"pattern": "^[a-zA-Z0-9-_.]*$"
},
"description": "The map of name -> variables to set"
}
]
},
"function": {
"anyOf": [
{
"$ref": "#/definitions/seedData"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
},
{
"anyOf": [
{
"$ref": "#/definitions/seedData/anyOf/5/properties/value/anyOf/2/anyOf/0"
},
{
"$ref": "#/definitions/seedData/anyOf/5/properties/value/anyOf/2/anyOf/1/additionalProperties/anyOf/2/anyOf/2"
}
],
"description": "The seed reference of the function to call"
}
]
}
},
"required": [
"type",
"arguments",
"function"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
Expand Down
24 changes: 23 additions & 1 deletion src/grow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ import {
fetchMethod,
fetchFormat,
SeedDataFilter,
SeedDataFunction
SeedDataFunction,
SeedDataCall
} from './types.js';

import {
Expand Down Expand Up @@ -706,6 +707,24 @@ const growFunction = async (seed : Seed<SeedDataFunction>, env : Environment) :
return await getProperty(seed, newEnv, data.block);
};

const growCall = async (seed : Seed<SeedDataCall>, env : Environment) : Promise<Value> => {
const data = seed.data;

const values = await getProperty(seed, env, data.arguments);
if (typeof values != 'object') throw new Error('Values must be an object');
if (Array.isArray(values)) throw new Error('Values must be an object');
if (!values) throw new Error('Values must be an object');
const vars : EnvironmentData = {};
for (const [key, val] of Object.entries(values)) {
if (isNamespaced(key)) throw new Error('arguments should not be namespaced');
const processedKey = getNamespacedID(key, FUNCTION_ARG_NAMESPACE);
vars[processedKey] = val;
}
const newEnv = env.clone(vars);
//TODO: throw if data.function is not a seed-reference to a seed of type function.
return await getProperty(seed, newEnv, data.function);
};

const growStore = async (seed : Seed<SeedDataStore>, env : Environment) : Promise<Value> => {
const data = seed.data;
const rawStoreID = extractString(await getProperty(seed, env, data.store, env.getKnownStringKey('store')));
Expand Down Expand Up @@ -867,6 +886,9 @@ export const grow = async (seed : Seed, env : Environment) : Promise<Value> => {
case 'function':
result = await growFunction(seed as Seed<SeedDataFunction>, env);
break;
case 'call':
result = await growCall(seed as Seed<SeedDataCall>, env);
break;
case 'store':
result = await growStore(seed as Seed<SeedDataStore>, env);
break;
Expand Down
21 changes: 21 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,25 @@ const seedDataFunction = makeSeedData(seedDataConfigFunction);

export type SeedDataFunction = z.infer<typeof seedDataFunction>;

const seedDataConfigCall = {
type: z.literal('call'),
properties: {
//We don't use varName because we want a non-namespaced ID
arguments: z.record(genericExtraID, z.union([
lazySeedData,
seedReference,
inputValue
])).describe('The map of name -> variables to set'),
//TODO: shouldn't this be typed to just be a seed function only?
function: inputNonObjectValue.describe('The seed reference of the function to call')
}
};

const nestedSeedDataCall = makeNestedSeedData(seedDataConfigCall);
const seedDataCall = makeSeedData(seedDataConfigCall);

export type SeedDataCall = z.infer<typeof seedDataCall>;

const seedDataConfigStore = {
type: z.literal('store'),
properties: {
Expand Down Expand Up @@ -1047,6 +1066,7 @@ export const expandedSeedData = z.discriminatedUnion('type', [
seedDataLet,
seedDataLetMulti,
seedDataFunction,
seedDataCall,
seedDataStore,
seedDataRetrieve,
seedDataDelete
Expand Down Expand Up @@ -1095,6 +1115,7 @@ export const seedData = z.discriminatedUnion('type', [
nestedSeedDataLet,
nestedSeedDataLetMulti,
nestedSeedDataFunction,
nestedSeedDataCall,
nestedSeedDataStore,
nestedSeedDataRetrieve,
nestedSeedDataDelete
Expand Down

0 comments on commit 616fdc1

Please sign in to comment.