Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#8066 Handle TABLE data type in AA api responses #8155

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
120 changes: 120 additions & 0 deletions src/contrib/automationanywhere/aaUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,109 @@ describe("selectBotOutput", () => {
},
],
},
out_Table: {
type: "TABLE",
boolean: "",
string: "",
number: "",
dictionary: [],
table: {
schema: [
{
name: "Date",
type: "STRING",
subtype: "STRING",
},
{
name: "Items",
type: "STRING",
subtype: "STRING",
},
{
name: "Total",
type: "STRING",
subtype: "STRING",
},
],
rows: [
{
values: [
{
type: "STRING",
string: "2022-01-01",
number: "",
boolean: "",
dictionary: [],
},
{
type: "STRING",
string: "Foo, bar, baz",
number: "",
boolean: "",
dictionary: [],
},
{
type: "STRING",
string: "42",
number: "",
boolean: "",
dictionary: [],
},
],
},
{
values: [
{
type: "STRING",
string: "2022-01-02",
number: "",
boolean: "",
dictionary: [],
},
{
type: "STRING",
string: "Qux, quux",
number: "",
boolean: "",
dictionary: [],
},
{
type: "STRING",
string: "24",
number: "",
boolean: "",
dictionary: [],
},
],
},
{
values: [
{
type: "STRING",
string: "2022-01-03",
number: "",
boolean: "",
dictionary: [],
},
{
type: "STRING",
string: "Corge",
number: "",
boolean: "",
dictionary: [],
},
{
type: "STRING",
string: "12",
number: "",
boolean: "",
dictionary: [],
},
],
},
],
},
},
},
},
};
Expand All @@ -204,6 +307,23 @@ describe("selectBotOutput", () => {
baz: true,
qux: 42,
},
out_Table: [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

{
Date: "2022-01-01",
Items: "Foo, bar, baz",
Total: "42",
},
{
Date: "2022-01-02",
Items: "Qux, quux",
Total: "24",
},
{
Date: "2022-01-03",
Items: "Corge",
Total: "12",
},
],
});
});
});
34 changes: 32 additions & 2 deletions src/contrib/automationanywhere/aaUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
type Execution,
type Interface,
type OutputValue,
type TableValue,
type Variable,
} from "@/contrib/automationanywhere/contract";
import { type JSONSchema7Type } from "json-schema";
Expand Down Expand Up @@ -145,7 +146,27 @@ export function mapBotInput(data: UnknownObject) {
});
}

function mapBotOutput(value: OutputValue): Primitive | UnknownObject {
function mapBotTableValue(tableValue: TableValue): UnknownObject[] {
const { schema, rows } = tableValue;
return rows.map((row) => {
const rowObject: UnknownObject = {};
for (const [index, value] of row.values.entries()) {
BLoe marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line security/detect-object-injection -- Using index lookups
const columnSchema = schema[index];
if (columnSchema == null) {
BLoe marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

rowObject[columnSchema.name] = mapBotOutput(value);
}

return rowObject;
});
}

function mapBotOutput(
value: OutputValue,
): Primitive | UnknownObject | UnknownObject[] {
switch (value.type) {
case "STRING": {
return value.string;
Expand All @@ -165,8 +186,17 @@ function mapBotOutput(value: OutputValue): Primitive | UnknownObject {
);
}

case "TABLE": {
if (value.table == null) {
return [];
}

return mapBotTableValue(value.table);
}

default: {
const exhaustiveCheck: never = value.type;
console.log("*** TABLE:", value);
BLoe marked this conversation as resolved.
Show resolved Hide resolved
throw new BusinessError(
`Type not supported by PixieBrix: ${exhaustiveCheck}`,
);
Expand All @@ -176,7 +206,7 @@ function mapBotOutput(value: OutputValue): Primitive | UnknownObject {

export function selectBotOutput(
execution: Pick<Execution, "botOutVariables">,
): Record<string, Primitive | UnknownObject> {
): Record<string, Primitive | UnknownObject | UnknownObject[]> {
return mapValues(execution.botOutVariables?.values ?? {}, (value) =>
mapBotOutput(value),
);
Expand Down
20 changes: 19 additions & 1 deletion src/contrib/automationanywhere/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const API_TASK_TYPE = "application/vnd.aa.headlessbot";

// Bots in the "Private" workspace are also referred to as Local bots
export type WorkspaceType = "public" | "private";
type VariableType = "STRING" | "NUMBER" | "BOOLEAN" | "DICTIONARY";
type VariableType = "STRING" | "NUMBER" | "BOOLEAN" | "DICTIONARY" | "TABLE";

export type Variable = {
name: string;
Expand Down Expand Up @@ -119,6 +119,22 @@ export const FAILURE_STATUSES = new Set([
"RUN_TIMED_OUT",
]);

type TableColumnSchema = {
BLoe marked this conversation as resolved.
Show resolved Hide resolved
name: string;
type: string;
subtype: string;
};

type TableRow = {
// eslint-disable-next-line @typescript-eslint/no-use-before-define -- recursive type
values: OutputValue[];
};

export type TableValue = {
schema: TableColumnSchema[];
rows: TableRow[];
};

export type OutputValue = {
type: VariableType;
string: string;
Expand All @@ -128,6 +144,8 @@ export type OutputValue = {
key: string;
value: OutputValue;
}>;
// Seems like table property is optional on the response value objects
table?: TableValue;
BLoe marked this conversation as resolved.
Show resolved Hide resolved
};

export type Activity = {
Expand Down