Is it possible to declare nested arrays as Parameters?

I’m facing a challenge in developing a pack that creates the cartesian product of lists.
Basically, I’m working on a tool that needs data to be disaggregated along a few dimensions, each dimension comprising a few categories. Example: dimension gender (with categories [“woman”, “man”, “non-binary”], and dimension age_group, with categories [““18-35”, “36+”]. Other dimensions could be “Level of confidence” (with 5 levels as categories: [conf-1, …, conf-5]. The categories are separate lists.
I want to create the cartesian product of all the categories in each list, as follows: [conf-1, woman, 18-25], [conf-1, woman, 36+], [conf-1, man, 18-25],…

The challenge I’m facing is defining nested arrays as parameters. Is that even possible with the ParameterTyp.Array ? See my code snippet of a pack that tries to recreate the Python function “itertools.product()”
——————–

parameters: [
coda.makeParameter({
type: coda.ParameterType.Array,
  name: "lists",
  description: `A single list containing multiple lists, e.g., [["F","M"],["Urban","Rural"]].`,
  items: {
type: coda.ValueType.Array, // Defines each item as a list.
    items: {type: coda.ValueType.String},  // Defines the contents of that inner list as strings.
  },
}),

],
resultType: coda.ValueType.Array,
items: coda.makeSchema({
type: coda.ValueType.Array,
items: { type: coda.ValueType.String },
}),

—————

I keep receiving this error message when trying to build the pack:

We found some errors: Pack metadata failed validation.

  • formulas[0].parameters[0].type: Required”

Any help would be greatly appreciated. Thanks in advance.

2 Likes

Not sure if it’s possible to pass a list of lists — last time I needed it in my Formula Tables pack I worked around that limitation with a special idiom (wrapping in an object and adding a top-level item into the list of lists), receiving the parameter as String, and JSON.parse()-ing and unwrapping it in the pack.

Hi @Gerardo_Ducos - Unfortunately, it’s not possible to have a list of lists as a parameter. In addition to the JSON approach that @Paul_Danyliuk recommended, another pattern you can follow is to use varargParameters. This allows your Pack to accept an arbitrary number of lists:

import * as coda from "@codahq/packs-sdk";

export const pack = coda.newPack();

pack.addFormula({
  name: "Test",
  description: "A test formula.",
  parameters: [],
  varargParameters: [
    coda.makeParameter({
      type: coda.ParameterType.StringArray,
      name: "list",
      description: "A list of things.",
    }),
  ],
  resultType: coda.ValueType.String,
  execute: async function (args, context) {
    let [...lists] = args;
    return JSON.stringify(lists);
  },
});

3 Likes

Hi both, @Paul_Danyliuk and @Eric_Koleda.
Thanks so much for taking the time to reply and for the tips.
I’ll try the varargParameters approach and post the result if it works.

2 Likes