Skip to content

Commit

Permalink
Update how function works.
Browse files Browse the repository at this point in the history
Now it's basically like `seeds/example-utility#expect` in that it's an array of expected parameters.

Part of #20.
  • Loading branch information
jkomoros committed Jul 29, 2023
1 parent 616fdc1 commit afb501f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ Defines a procedure that other seeds can call with `call`.
Note that semantically it's basically equivalent to a `let-multi` with named arguments, but using `function` communicates more clearly the intent that this is a callable-sub-procedure, which allows tooling to understand it 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.
- `arguments` - The list of names of arguments that are expected to be passed. 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
Expand Down
18 changes: 3 additions & 15 deletions seed-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2907,21 +2907,9 @@
"$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": {
"type": "array",
"items": {
"type": "string",
"pattern": "^[a-zA-Z0-9-_.]*$"
},
"description": "The map of name -> variables to set"
Expand Down
18 changes: 8 additions & 10 deletions src/grow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,17 +694,15 @@ const growFunction = async (seed : Seed<SeedDataFunction>, env : Environment) :
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;
if (!Array.isArray(values)) throw new Error('Values must be an array');
for (const name of values) {
if (typeof name != 'string') throw new Error('argument name must be string');
if (isNamespaced(name)) throw new Error('arguments should not be namespaced');
const processedKey = getNamespacedID(name, FUNCTION_ARG_NAMESPACE);
const val = env.get(processedKey);
if (val === null) throw new Error(`${val} was not set as expected`);
}
const newEnv = env.clone(vars);
return await getProperty(seed, newEnv, data.block);
return await getProperty(seed, env, data.block);
};

const growCall = async (seed : Seed<SeedDataCall>, env : Environment) : Promise<Value> => {
Expand Down
6 changes: 1 addition & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -945,11 +945,7 @@ const seedDataConfigFunction = {
type: z.literal('function'),
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'),
arguments: z.array(genericExtraID).describe('The map of name -> variables to set'),
block: inputNonObjectValue.describe('The sub-expression where name=value will be set in environment')
}
};
Expand Down

0 comments on commit afb501f

Please sign in to comment.