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

Adding bulk actions to 1.1 #53

Merged
merged 13 commits into from
Feb 14, 2024
2 changes: 2 additions & 0 deletions examples/one_dot_one/src/generatedNoCheck/Ontology.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { OntologyDefinition } from '@osdk/api';
import type { Ontology as ClientOntology } from '@osdk/legacy-client';
import type { Actions } from './ontology/actions/Actions';
import type { BulkActions } from './ontology/actions/BulkActions';
import { actionTakesAllParameterTypes } from './ontology/actions/actionTakesAllParameterTypes';
import { createTodo } from './ontology/actions/createTodo';
import { ObjectTypeWithAllPropertyTypes } from './ontology/objects/ObjectTypeWithAllPropertyTypes';
Expand Down Expand Up @@ -58,5 +59,6 @@ export const Ontology: {
export interface Ontology extends ClientOntology<typeof Ontology> {
objects: Objects;
actions: Actions;
bulkActions: BulkActions;
Copy link
Contributor

Choose a reason for hiding this comment

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

can you mention rationale in the PR description as to why we add it at the top level vs within actions, for posterity?

queries: Queries;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type {
ActionError,
Attachment,
BulkActionExecutionOptions,
BulkActionResponseFromOptions,
Edits,
LocalDate,
ObjectSet,
Result,
Timestamp,
} from '@osdk/legacy-client';
import type { ObjectTypeWithAllPropertyTypes } from '../objects/ObjectTypeWithAllPropertyTypes';
import type { Person } from '../objects/Person';
import type { Todo } from '../objects/Todo';
export interface BulkActions {
/**
* An action which takes different types of parameters
* @param {ObjectSet<Todo>} params.objectSet
* @param {Person | Person["__primaryKey"]} params.object
* @param {string} params.string
* @param {Timestamp} params.time-stamp
* @param {Array<LocalDate>} params.dateArray
* @param {Array<Attachment>} params.attachmentArray
*/
actionTakesAllParameterTypes<O extends BulkActionExecutionOptions>(
params: {
objectSet: ObjectSet<Todo>;
object?: Person | Person['__primaryKey'];
string: string;
'time-stamp': Timestamp;
dateArray?: Array<LocalDate>;
attachmentArray: Array<Attachment>;
}[],
options?: O,
): Promise<Result<BulkActionResponseFromOptions<O, Edits<Todo, Todo | ObjectTypeWithAllPropertyTypes>>, ActionError>>;

/**
* Creates a new Todo
*/
createTodo<O extends BulkActionExecutionOptions>(
options?: O,
): Promise<Result<BulkActionResponseFromOptions<O, Edits<Todo, void>>, ActionError>>;
}
2 changes: 2 additions & 0 deletions examples/todoapp/src/generatedNoCheck/Ontology.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { OntologyDefinition } from '@osdk/api';
import type { Ontology as ClientOntology } from '@osdk/legacy-client';
import type { Actions } from './ontology/actions/Actions';
import type { BulkActions } from './ontology/actions/BulkActions';
import { completeTodo } from './ontology/actions/completeTodo';
import { createTodo } from './ontology/actions/createTodo';
import type { Objects } from './ontology/objects/Objects';
Expand Down Expand Up @@ -40,5 +41,6 @@ export const Ontology: {
export interface Ontology extends ClientOntology<typeof Ontology> {
objects: Objects;
actions: Actions;
bulkActions: BulkActions;
queries: Queries;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type {
ActionError,
BulkActionExecutionOptions,
BulkActionResponseFromOptions,
Edits,
Result,
} from '@osdk/legacy-client';
import type { Todo } from '../objects/Todo';
export interface BulkActions {
/**
* Creates Todo
* @param {string} params.Todo
* @param {boolean} params.is_complete
*/
createTodo<O extends BulkActionExecutionOptions>(
params: {
Todo: string;
is_complete: boolean;
}[],
options?: O,
): Promise<Result<BulkActionResponseFromOptions<O, Edits<Todo, void>>, ActionError>>;

/**
* Completes Todo
* @param {Todo | Todo["__primaryKey"]} params.Todo
* @param {boolean} params.is_complete
*/
completeTodo<O extends BulkActionExecutionOptions>(
params: {
Todo: Todo | Todo['__primaryKey'];
is_complete: boolean;
}[],
options?: O,
): Promise<Result<BulkActionResponseFromOptions<O, Edits<void, Todo>>, ActionError>>;
}
2 changes: 1 addition & 1 deletion packages/generator/src/v1.1/generateActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export async function generateActions(
);
}

function getTypeScriptTypeFromDataType(
export function getTypeScriptTypeFromDataType(
actionParameter: ActionParameterType,
importedObjects: Set<string>,
): string {
Expand Down
60 changes: 60 additions & 0 deletions packages/generator/src/v1.1/generateBulkActions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2023 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { describe, expect, it } from "vitest";
import { createMockMinimalFiles } from "../util/test/createMockMinimalFiles";
import { TodoWireOntology } from "../util/test/TodoWireOntology";
import { generateBulkActions } from "./generateBulkActions";

describe(generateBulkActions, () => {
it("generates bulk action interface", async () => {
const helper = createMockMinimalFiles();
const BASE_PATH = "/foo";

await generateBulkActions(
TodoWireOntology,
helper.minimalFiles,
BASE_PATH,
);

expect(helper.minimalFiles.writeFile).toBeCalled();

expect(helper.getFiles()[`${BASE_PATH}/BulkActions.ts`])
.toMatchInlineSnapshot(`
"import type {
ActionError,
BulkActionExecutionOptions,
BulkActionResponseFromOptions,
Edits,
Result,
} from '@osdk/legacy-client';
import type { Todo } from '../objects/Todo';
export interface BulkActions {
/**
* An action which takes different types of parameters
* @param {Todo | Todo["__primaryKey"]} params.object
*/
markTodoCompleted<O extends BulkActionExecutionOptions>(
params: {
object?: Todo | Todo['__primaryKey'];
}[],
options?: O,
): Promise<Result<BulkActionResponseFromOptions<O, Edits<void, Todo>>, ActionError>>;
}
"
`);
});
});
100 changes: 100 additions & 0 deletions packages/generator/src/v1.1/generateBulkActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2023 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import path from "node:path";
import type { MinimalFs } from "../MinimalFs";
import { getModifiedEntityTypes } from "../shared/getEditedEntities";
import { formatTs } from "../util/test/formatTs";
import type { WireOntologyDefinition } from "../WireOntologyDefinition";
import { getTypeScriptTypeFromDataType } from "./generateActions";

export async function generateBulkActions(
ontology: WireOntologyDefinition,
fs: MinimalFs,
outDir: string,
importExt: string = "",
) {
const importedObjects = new Set<string>();
let actionSignatures: any[] = [];
for (const action of Object.values(ontology.actionTypes)) {
const entries = Object.entries(action.parameters);

const modifiedEntityTypes = getModifiedEntityTypes(action);
const addedObjects = Array.from(modifiedEntityTypes.addedObjects);
Copy link
Member

Choose a reason for hiding this comment

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

I think .addedObjects/.modifiedObjects is a Set<string>. So we are converting it to an array so we can forEach add it to a different set. At the very least you could just forEach on the returned sets instead of the memory copy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yea I just pulled this from the generateActions, it looks like we converted to Array so we could just do a simple join on line 74 when generating the edits return type

const modifiedObjects = Array.from(modifiedEntityTypes.modifiedObjects);
addedObjects.forEach(importedObjects.add, importedObjects);
modifiedObjects.forEach(importedObjects.add, importedObjects);

let jsDocBlock = ["/**"];
if (action.description) {
jsDocBlock.push(`* ${action.description}`);
}

let parameterBlock = "";
if (entries.length > 0) {
parameterBlock = `params: { \n`;
for (
const [parameterName, parameterData] of entries
) {
parameterBlock += `"${parameterName}"`;
parameterBlock += parameterData.required ? ": " : "?: ";
const typeScriptType = getTypeScriptTypeFromDataType(
parameterData.dataType,
importedObjects,
);
parameterBlock += `${typeScriptType};\n`;

jsDocBlock.push(
`* @param {${typeScriptType}} params.${parameterName}`,
);
}
parameterBlock += "}[], ";
}

jsDocBlock.push(`*/`);
actionSignatures.push(
`
${jsDocBlock.join("\n")}
${action.apiName}<O extends BulkActionExecutionOptions>(${parameterBlock}options?: O):
Promise<Result<BulkActionResponseFromOptions<O, Edits<${
addedObjects.length > 0
? addedObjects.join(" | ")
: "void"
}, ${
modifiedObjects.length > 0
? modifiedObjects.join(" | ")
: "void"
}>>, ActionError>>;
`,
);
}

await fs.mkdir(outDir, { recursive: true });
await fs.writeFile(
path.join(outDir, "BulkActions.ts"),
await formatTs(`
import type { ObjectSet, LocalDate, Timestamp, Attachment, Edits, ActionExecutionOptions, BulkActionExecutionOptions, ActionError, Result, ActionResponseFromOptions, BulkActionResponseFromOptions } from "@osdk/legacy-client";
${
Array.from(importedObjects).map(importedObject =>
`import type { ${importedObject} } from "../objects/${importedObject}${importExt}";`
).join("\n")
}
export interface BulkActions {
${actionSignatures.join("\n")}
}
`),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { verifyOutdir } from "../util/verifyOutdir";
import type { WireOntologyDefinition } from "../WireOntologyDefinition";
import { generateActions } from "./generateActions";
import { generateBackCompatDeprecatedExports } from "./generateBackCompatDeprecatedExports";
import { generateBulkActions } from "./generateBulkActions";
import { generateFoundryClientFile } from "./generateFoundryClientFile";
import { generateIndexFile } from "./generateIndexFile";
import { generateMetadataFile } from "./generateMetadataFile";
Expand Down Expand Up @@ -79,6 +80,7 @@ export async function generateClientSdkVersionOneDotOne(
importExt,
);
await generateActions(sanitizedOntology, fs, actionsDir, importExt);
await generateBulkActions(sanitizedOntology, fs, actionsDir, importExt);
await generatePerActionDataFiles(
sanitizedOntology,
fs,
Expand Down
6 changes: 6 additions & 0 deletions packages/generator/src/v1.1/generateMetadataFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe(generateMetadataFile, () => {
"import type { OntologyDefinition } from '@osdk/api';
import type { Ontology as ClientOntology } from '@osdk/legacy-client';
import type { Actions } from './ontology/actions/Actions';
import type { BulkActions } from './ontology/actions/BulkActions';
import { markTodoCompleted } from './ontology/actions/markTodoCompleted';
import type { Objects } from './ontology/objects/Objects';
import { Person } from './ontology/objects/Person';
Expand Down Expand Up @@ -83,6 +84,7 @@ describe(generateMetadataFile, () => {
export interface Ontology extends ClientOntology<typeof Ontology> {
objects: Objects;
actions: Actions;
bulkActions: BulkActions;
queries: Queries;
}
"
Expand Down Expand Up @@ -165,6 +167,7 @@ describe(generateMetadataFile, () => {
"import type { OntologyDefinition } from '@osdk/api';
import type { Ontology as ClientOntology } from '@osdk/legacy-client';
import type { Actions } from './ontology/actions/Actions';
import type { BulkActions } from './ontology/actions/BulkActions';
import { bar } from './ontology/actions/bar';
import { foo as fooAction } from './ontology/actions/foo';
import type { Objects } from './ontology/objects/Objects';
Expand Down Expand Up @@ -212,6 +215,7 @@ describe(generateMetadataFile, () => {
export interface Ontology extends ClientOntology<typeof Ontology> {
objects: Objects;
actions: Actions;
bulkActions: BulkActions;
queries: Queries;
}
"
Expand Down Expand Up @@ -249,6 +253,7 @@ describe(generateMetadataFile, () => {
"import type { OntologyDefinition } from '@osdk/api';
import type { Ontology as ClientOntology } from '@osdk/legacy-client';
import type { Actions } from './ontology/actions/Actions';
import type { BulkActions } from './ontology/actions/BulkActions';
import type { Objects } from './ontology/objects/Objects';
import type { Queries } from './ontology/queries/Queries';

Expand All @@ -275,6 +280,7 @@ describe(generateMetadataFile, () => {
export interface Ontology extends ClientOntology<typeof Ontology> {
objects: Objects;
actions: Actions;
bulkActions: BulkActions;
queries: Queries;
}
"
Expand Down
2 changes: 2 additions & 0 deletions packages/generator/src/v1.1/generateMetadataFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export async function generateMetadataFile(
import type { Objects } from "./ontology/objects/Objects${importExt}";
import type { Actions } from "./ontology/actions/Actions${importExt}";
import type { Queries } from "./ontology/queries/Queries${importExt}";
import type { BulkActions } from "./ontology/actions/BulkActions${importExt}";
${
objectNames.map((name) =>
`import {${name}} from "./ontology/objects/${name}${importExt}";`
Expand Down Expand Up @@ -142,6 +143,7 @@ export async function generateMetadataFile(
export interface Ontology extends ClientOntology<typeof Ontology> {
objects: Objects;
actions: Actions;
bulkActions: BulkActions;
queries: Queries;
}`),
);
Expand Down
Loading
Loading