Skip to content

Commit

Permalink
chore: remove _supportedOps (#6350)
Browse files Browse the repository at this point in the history
Fixes #6353

The information provided by this method is basically a lighter form of `_liftMap` and the duplication introduces some confusion while iterating on lifting code, so this finishes migrating some of the code over. Most of this is boilerplate that the compiler generates for you under ordinary circumstances.

Misc:
* Added some checks to `wing-api-checker` to ensure `@inflight` annotations are added to SDK resources written in TypeScript.

## Checklist

- [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [ ] Description explains motivation and solution
- [ ] Tests added (always)
- [ ] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
Chriscbr authored Apr 29, 2024
1 parent bc37c54 commit 25512ad
Show file tree
Hide file tree
Showing 63 changed files with 501 additions and 462 deletions.
31 changes: 30 additions & 1 deletion apps/wing-api-checker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ async function generateWarnings(packageDir: string): Promise<number> {
warnings += 1;
}

const IResource = interfaces.find((x) => x.name === "IResource");
if (!IResource) {
throw new Error("IResource not found");
}

const IInflight = interfaces.find((x) => x.name === "IInflight");
if (!IInflight) {
throw new Error("IInflight not found");
}

for (const cls of classes) {
if (!cls.docs.summary) {
warn(`Missing docstring for ${cls.fqn} (${loc(cls)})`);
Expand All @@ -40,6 +50,21 @@ async function generateWarnings(packageDir: string): Promise<number> {
);
}
}
if (
cls.getInterfaces(true).includes(IResource) ||
cls.getInterfaces(true).includes(IInflight)
) {
if (
cls.docs.customTag("inflight") === undefined &&
cls.docs.customTag("noinflight") === undefined
) {
warn(
`Missing @inflight in docstring for ${cls.fqn} (${loc(
cls
)}). Suppress with @noinflight.`
);
}
}
}

for (const iface of interfaces) {
Expand All @@ -57,7 +82,11 @@ async function generateWarnings(packageDir: string): Promise<number> {
if (iface.name.endsWith("Props")) {
for (const prop of iface.allProperties) {
if (prop.optional && !prop.docs.docs.default) {
warn(`Missing @default for ${iface.fqn}.${prop.name} (${loc(prop)})`);
warn(
`Missing @default in docstring for ${iface.fqn}.${prop.name} (${loc(
prop
)})`
);
}
}
}
Expand Down
9 changes: 0 additions & 9 deletions docs/docs/04-standard-library/cloud/domain.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,4 @@ The website's custom domain name.
```


## Protocols <a name="Protocols" id="Protocols"></a>

### IDomainClient <a name="IDomainClient" id="@winglang/sdk.cloud.IDomainClient"></a>

- *Implemented By:* <a href="#@winglang/sdk.cloud.IDomainClient">IDomainClient</a>

Inflight interface for `Domain`.



9 changes: 0 additions & 9 deletions docs/docs/04-standard-library/cloud/endpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,4 @@ For UI purposes.
```


## Protocols <a name="Protocols" id="Protocols"></a>

### IEndpointClient <a name="IEndpointClient" id="@winglang/sdk.cloud.IEndpointClient"></a>

- *Implemented By:* <a href="#@winglang/sdk.cloud.IEndpointClient">IEndpointClient</a>

Inflight interface for `Endpoint`.



10 changes: 2 additions & 8 deletions docs/docs/04-standard-library/sim/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ new sim.Policy(props: PolicyProps);

#### Methods <a name="Methods" id="Methods"></a>

##### Preflight Methods

| **Name** | **Description** |
| --- | --- |
| <code><a href="#@winglang/sdk.sim.Policy.addStatement">addStatement</a></code> | Adds a statement to the policy. |
Expand Down Expand Up @@ -611,14 +613,6 @@ The resource to which the policy is attached.

## Protocols <a name="Protocols" id="Protocols"></a>

### IPolicyClient <a name="IPolicyClient" id="@winglang/sdk.sim.IPolicyClient"></a>

- *Implemented By:* <a href="#@winglang/sdk.sim.IPolicyClient">IPolicyClient</a>

Inflight interface for `Policy`.



### ISimulatorInflightHost <a name="ISimulatorInflightHost" id="@winglang/sdk.sim.ISimulatorInflightHost"></a>

- *Extends:* <a href="#@winglang/sdk.std.IInflightHost">IInflightHost</a>
Expand Down
48 changes: 28 additions & 20 deletions libs/awscdk/src/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import { cloud, core, std } from "@winglang/sdk";
import { convertBetweenHandlers } from "@winglang/sdk/lib/shared/convert";
import { calculateBucketPermissions } from "@winglang/sdk/lib/shared-aws/permissions";
import { IAwsBucket } from "@winglang/sdk/lib/shared-aws/bucket";
import { IAwsCdkFunction, addPolicyStatements, isAwsCdkFunction } from "./function";
import {
IAwsCdkFunction,
addPolicyStatements,
isAwsCdkFunction,
} from "./function";
import { LiftMap } from "@winglang/sdk/lib/core";

const EVENTS = {
[cloud.BucketEventType.DELETE]: EventType.OBJECT_REMOVED,
Expand Down Expand Up @@ -81,24 +86,24 @@ export class Bucket extends cloud.Bucket implements IAwsBucket {
}

/** @internal */
public _supportedOps(): string[] {
return [
cloud.BucketInflightMethods.DELETE,
cloud.BucketInflightMethods.GET,
cloud.BucketInflightMethods.GET_JSON,
cloud.BucketInflightMethods.LIST,
cloud.BucketInflightMethods.PUT,
cloud.BucketInflightMethods.PUT_JSON,
cloud.BucketInflightMethods.PUBLIC_URL,
cloud.BucketInflightMethods.EXISTS,
cloud.BucketInflightMethods.TRY_GET,
cloud.BucketInflightMethods.TRY_GET_JSON,
cloud.BucketInflightMethods.TRY_DELETE,
cloud.BucketInflightMethods.SIGNED_URL,
cloud.BucketInflightMethods.METADATA,
cloud.BucketInflightMethods.COPY,
cloud.BucketInflightMethods.RENAME,
];
public get _liftMap(): LiftMap {
return {
[cloud.BucketInflightMethods.DELETE]: [],
[cloud.BucketInflightMethods.GET]: [],
[cloud.BucketInflightMethods.GET_JSON]: [],
[cloud.BucketInflightMethods.LIST]: [],
[cloud.BucketInflightMethods.PUT]: [],
[cloud.BucketInflightMethods.PUT_JSON]: [],
[cloud.BucketInflightMethods.PUBLIC_URL]: [],
[cloud.BucketInflightMethods.EXISTS]: [],
[cloud.BucketInflightMethods.TRY_GET]: [],
[cloud.BucketInflightMethods.TRY_GET_JSON]: [],
[cloud.BucketInflightMethods.TRY_DELETE]: [],
[cloud.BucketInflightMethods.SIGNED_URL]: [],
[cloud.BucketInflightMethods.METADATA]: [],
[cloud.BucketInflightMethods.COPY]: [],
[cloud.BucketInflightMethods.RENAME]: [],
};
}

public onCreate(
Expand Down Expand Up @@ -197,7 +202,10 @@ export class Bucket extends cloud.Bucket implements IAwsBucket {
throw new Error("Expected 'host' to implement IAwsCdkFunction");
}

addPolicyStatements(host.awscdkFunction, calculateBucketPermissions(this.bucket.bucketArn, ops));
addPolicyStatements(
host.awscdkFunction,
calculateBucketPermissions(this.bucket.bucketArn, ops)
);

// The bucket name needs to be passed through an environment variable since
// it may not be resolved until deployment time.
Expand Down
30 changes: 16 additions & 14 deletions libs/awscdk/src/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { COUNTER_HASH_KEY } from "@winglang/sdk/lib/shared-aws/commons";
import { calculateCounterPermissions } from "@winglang/sdk/lib/shared-aws/permissions";
import { IAwsCounter } from "@winglang/sdk/lib/shared-aws/counter";
import { addPolicyStatements, isAwsCdkFunction } from "./function";
import { LiftMap } from "@winglang/sdk/lib/core";

/**
* AWS implementation of `cloud.Counter`.
Expand All @@ -26,21 +27,24 @@ export class Counter extends cloud.Counter implements IAwsCounter {
}

/** @internal */
public _supportedOps(): string[] {
return [
cloud.CounterInflightMethods.INC,
cloud.CounterInflightMethods.DEC,
cloud.CounterInflightMethods.PEEK,
cloud.CounterInflightMethods.SET,
];
public get _liftMap(): LiftMap {
return {
[cloud.CounterInflightMethods.INC]: [],
[cloud.CounterInflightMethods.DEC]: [],
[cloud.CounterInflightMethods.PEEK]: [],
[cloud.CounterInflightMethods.SET]: [],
};
}

public onLift(host: std.IInflightHost, ops: string[]): void {
if (!isAwsCdkFunction(host)) {
throw new Error("Expected 'host' to implement 'isAwsCdkFunction' method");
}

addPolicyStatements(host.awscdkFunction, calculateCounterPermissions(this.table.tableArn, ops));
addPolicyStatements(
host.awscdkFunction,
calculateCounterPermissions(this.table.tableArn, ops)
);

host.addEnvironment(this.envName(), this.table.tableName);

Expand All @@ -49,12 +53,10 @@ export class Counter extends cloud.Counter implements IAwsCounter {

/** @internal */
public _toInflight(): string {
return core.InflightClient.for(
__dirname,
__filename,
"CounterClient",
[`process.env["${this.envName()}"]`, `${this.initial}`]
);
return core.InflightClient.for(__dirname, __filename, "CounterClient", [
`process.env["${this.envName()}"]`,
`${this.initial}`,
]);
}

private envName(): string {
Expand Down
11 changes: 6 additions & 5 deletions libs/awscdk/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { IAwsFunction, PolicyStatement } from "@winglang/sdk/lib/shared-aws";
import { resolve } from "path";
import { renameSync, rmSync, writeFileSync } from "fs";
import { App } from "./app";
import { LiftMap } from "@winglang/sdk/lib/core";

/**
* Implementation of `awscdk.Function` are expected to implement this
Expand Down Expand Up @@ -104,11 +105,11 @@ export class Function
}

/** @internal */
public _supportedOps(): string[] {
return [
cloud.FunctionInflightMethods.INVOKE,
cloud.FunctionInflightMethods.INVOKE_ASYNC,
];
public get _liftMap(): LiftMap {
return {
[cloud.FunctionInflightMethods.INVOKE]: [],
[cloud.FunctionInflightMethods.INVOKE_ASYNC]: [],
};
}

public onLift(host: std.IInflightHost, ops: string[]): void {
Expand Down
80 changes: 46 additions & 34 deletions libs/awscdk/src/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import { Construct } from "constructs";
import { App } from "./app";
import { std, core, cloud } from "@winglang/sdk";
import { calculateQueuePermissions } from "@winglang/sdk/lib/shared-aws/permissions";
import { IAwsQueue, Queue as AwsQueue, QueueSetConsumerHandler } from "@winglang/sdk/lib/shared-aws/queue";
import {
IAwsQueue,
Queue as AwsQueue,
QueueSetConsumerHandler,
} from "@winglang/sdk/lib/shared-aws/queue";
import { addPolicyStatements, isAwsCdkFunction } from "./function";
import { LiftMap } from "@winglang/sdk/lib/core";

/**
* AWS implementation of `cloud.Queue`.
Expand All @@ -21,25 +26,32 @@ export class Queue extends cloud.Queue implements IAwsQueue {
super(scope, id, props);
this.timeout = props.timeout ?? std.Duration.fromSeconds(30);

const queueOpt = props.dlq ? {
visibilityTimeout: props.timeout
? Duration.seconds(props.timeout?.seconds)
: Duration.seconds(30),
retentionPeriod: props.retentionPeriod
? Duration.seconds(props.retentionPeriod?.seconds)
: Duration.hours(1),
deadLetterQueue: {
queue: SQSQueue.fromQueueArn(this, "DeadLetterQueue", AwsQueue.from(props.dlq.queue)?.queueArn!),
maxReceiveCount: props.dlq.maxDeliveryAttempts ?? cloud.DEFAULT_DELIVERY_ATTEMPTS,
}
} : {
visibilityTimeout: props.timeout
? Duration.seconds(props.timeout?.seconds)
: Duration.seconds(30),
retentionPeriod: props.retentionPeriod
? Duration.seconds(props.retentionPeriod?.seconds)
: Duration.hours(1),
}
const queueOpt = props.dlq
? {
visibilityTimeout: props.timeout
? Duration.seconds(props.timeout?.seconds)
: Duration.seconds(30),
retentionPeriod: props.retentionPeriod
? Duration.seconds(props.retentionPeriod?.seconds)
: Duration.hours(1),
deadLetterQueue: {
queue: SQSQueue.fromQueueArn(
this,
"DeadLetterQueue",
AwsQueue.from(props.dlq.queue)?.queueArn!
),
maxReceiveCount:
props.dlq.maxDeliveryAttempts ?? cloud.DEFAULT_DELIVERY_ATTEMPTS,
},
}
: {
visibilityTimeout: props.timeout
? Duration.seconds(props.timeout?.seconds)
: Duration.seconds(30),
retentionPeriod: props.retentionPeriod
? Duration.seconds(props.retentionPeriod?.seconds)
: Duration.hours(1),
};

this.queue = new SQSQueue(this, "Default", queueOpt);
}
Expand Down Expand Up @@ -82,13 +94,13 @@ export class Queue extends cloud.Queue implements IAwsQueue {
}

/** @internal */
public _supportedOps(): string[] {
return [
cloud.QueueInflightMethods.PUSH,
cloud.QueueInflightMethods.PURGE,
cloud.QueueInflightMethods.APPROX_SIZE,
cloud.QueueInflightMethods.POP,
];
public get _liftMap(): LiftMap {
return {
[cloud.QueueInflightMethods.PUSH]: [],
[cloud.QueueInflightMethods.PURGE]: [],
[cloud.QueueInflightMethods.APPROX_SIZE]: [],
[cloud.QueueInflightMethods.POP]: [],
};
}

public onLift(host: std.IInflightHost, ops: string[]): void {
Expand All @@ -98,7 +110,10 @@ export class Queue extends cloud.Queue implements IAwsQueue {

const env = this.envName();

addPolicyStatements(host.awscdkFunction, calculateQueuePermissions(this.queue.queueArn, ops));
addPolicyStatements(
host.awscdkFunction,
calculateQueuePermissions(this.queue.queueArn, ops)
);

// The queue url needs to be passed through an environment variable since
// it may not be resolved until deployment time.
Expand All @@ -109,12 +124,9 @@ export class Queue extends cloud.Queue implements IAwsQueue {

/** @internal */
public _toInflight(): string {
return core.InflightClient.for(
__dirname,
__filename,
"QueueClient",
[`process.env["${this.envName()}"]`]
);
return core.InflightClient.for(__dirname, __filename, "QueueClient", [
`process.env["${this.envName()}"]`,
]);
}

private envName(): string {
Expand Down
Loading

0 comments on commit 25512ad

Please sign in to comment.