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

fix(compiler): cannot lift a class that extends a cloud resource #6490

Merged
merged 39 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
f399d23
midwork
Chriscbr May 7, 2024
0cced6c
Merge branch 'main' into rybickic/extend-resources
Chriscbr May 15, 2024
4349634
midwork
Chriscbr May 15, 2024
9250ae6
remove
Chriscbr May 15, 2024
14a59cb
clean up codegen
Chriscbr May 15, 2024
8a48a59
fixes
Chriscbr May 15, 2024
6f78f33
nice cleanup
Chriscbr May 15, 2024
77195bf
Merge branch 'main' into rybickic/extend-resources
Chriscbr May 29, 2024
a591275
more work
Chriscbr May 29, 2024
5a1db9f
update azure resources
Chriscbr May 29, 2024
0e64e31
updating azure and gcp resources
Chriscbr May 29, 2024
2538b02
liftedFields -> liftedState
Chriscbr May 29, 2024
0f8a56a
update awscdk
Chriscbr May 29, 2024
154275d
update some snapshots - still some tests failing
Chriscbr May 29, 2024
ad67230
handle edge case
Chriscbr May 29, 2024
e5638bc
fix
Chriscbr May 29, 2024
f394186
fix docs
Chriscbr May 29, 2024
31a97f3
update more snaps
Chriscbr May 29, 2024
c6285e5
fix missing toInflightType
Chriscbr May 29, 2024
56110e8
minor fix
Chriscbr May 29, 2024
637c805
remove unused method
Chriscbr May 29, 2024
c925fbd
Merge branch 'main' into rybickic/extend-resources
Chriscbr Jul 23, 2024
cf6d676
fix service
Chriscbr Jul 23, 2024
5314925
fix snaps
Chriscbr Jul 23, 2024
0c26c14
midwork
Chriscbr Jul 23, 2024
f099099
rename method
Chriscbr Jul 23, 2024
7ddd9bd
undo change to SCOPE_PARAM
Chriscbr Jul 23, 2024
0393497
Merge branch 'main' into rybickic/extend-resources
monadabot Jul 23, 2024
7adab07
chore: self mutation (e2e-1of2.diff)
monadabot Jul 23, 2024
c93f676
chore: self mutation (e2e-2of2.diff)
monadabot Jul 23, 2024
6640669
improve codegen a bit
Chriscbr Jul 23, 2024
5ea7aa5
Merge branch 'main' into rybickic/extend-resources
monadabot Jul 23, 2024
03c7f3b
chore: self mutation (e2e-1of2.diff)
monadabot Jul 23, 2024
f68ca70
chore: self mutation (e2e-2of2.diff)
monadabot Jul 23, 2024
4745d04
fix bug
Chriscbr Jul 23, 2024
25ebeb8
Merge branch 'main' into rybickic/extend-resources
Chriscbr Jul 24, 2024
04b8181
skip emitting _liftedState if there are no lifts
Chriscbr Jul 24, 2024
385770b
reduce code gen a bit
Chriscbr Jul 24, 2024
39987a8
update snapshots
Chriscbr Jul 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion apps/wing/src/commands/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ async function testTf(synthDir: string, options: TestOptions): Promise<std.TestR

const testArns = await terraformOutput(synthDir, ENV_WING_TEST_RUNNER_FUNCTION_IDENTIFIERS);
const { TestRunnerClient } = await import(testRunnerPath);
const runner = new TestRunnerClient(testArns);
const runner = new TestRunnerClient({ $tests: testArns });

const allTests = await runner.listTests();
const filteredTests = filterTests(allTests, testFilter);
Expand Down
2 changes: 1 addition & 1 deletion docs/api/04-standard-library/cloud/topic.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ AWS implementations of `cloud.Topic` use [AWS SNS](https://docs.aws.amazon.com/s

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

A topic.
A topic for pub/sub notifications.

#### Initializers <a name="Initializers" id="@winglang/sdk.cloud.Topic.Initializer"></a>

Expand Down
49 changes: 49 additions & 0 deletions examples/tests/valid/extend_counter.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
bring cloud;
bring util;
bring expect;

let global_value = "yo";

class MyCounter extends cloud.Counter {
pub field1: num;
new() {
this.field1 = 5;
}

pub inflight inc() {
expect.equal(this.field1, 5);
expect.equal(global_value, "yo");
super.inc();
}

pub inflight extra1(): str {
return "extra1";
}
}

class MySuperCounter extends MyCounter {
pub field2: num;
new() {
this.field2 = 10;
}

pub inflight inc() {
expect.equal(this.field2, 10);
expect.equal(this.field1, 5);
expect.equal(global_value, "yo");
super.inc();
}

pub inflight extra2(): str {
return "extra2";
}
}
Chriscbr marked this conversation as resolved.
Show resolved Hide resolved

let c = new MySuperCounter();

test "counter works" {
c.inc();
expect.equal(c.peek(), 1);
expect.equal(c.extra1(), "extra1");
expect.equal(c.extra2(), "extra2");
}
24 changes: 16 additions & 8 deletions libs/awscdk/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ import { isAwsCdkFunction } from "./function";
* AWS Implementation of `cloud.Api`.
*/
export class Api extends cloud.Api implements IAwsApi {
/** @internal */
public static _toInflightType(): string {
return core.InflightClient.forType(
__filename.replace("api", "api.inflight"),
"ApiClient"
);
}

private readonly api: WingRestApi;
private readonly handlers: Record<string, cloud.Function> = {};
private readonly endpoint: cloud.Endpoint;
Expand Down Expand Up @@ -53,7 +61,7 @@ export class Api extends cloud.Api implements IAwsApi {
method: string,
path: string,
inflight: cloud.IApiEndpointHandler,
props?: cloud.ApiEndpointOptions,
props?: cloud.ApiEndpointOptions
): void {
const lowerMethod = method.toLowerCase();
const upperMethod = method.toUpperCase();
Expand Down Expand Up @@ -186,7 +194,7 @@ export class Api extends cloud.Api implements IAwsApi {
inflight: cloud.IApiEndpointHandler,
method: string,
path: string,
props?: cloud.ApiEndpointOptions,
props?: cloud.ApiEndpointOptions
): cloud.Function {
return this.addInflightHandler(inflight, method, path, props);
}
Expand All @@ -201,7 +209,7 @@ export class Api extends cloud.Api implements IAwsApi {
inflight: cloud.IApiEndpointHandler,
method: string,
path: string,
props?: cloud.ApiEndpointOptions,
props?: cloud.ApiEndpointOptions
): cloud.Function {
let handler = this.handlers[inflight._id];
if (!handler) {
Expand All @@ -214,7 +222,7 @@ export class Api extends cloud.Api implements IAwsApi {
this,
App.of(this).makeId(this, prefix),
newInflight,
props,
props
);
this.handlers[inflight._id] = handler;
}
Expand All @@ -229,10 +237,10 @@ export class Api extends cloud.Api implements IAwsApi {
}

/** @internal */
public _toInflight(): string {
return core.InflightClient.for(__dirname, __filename, "ApiClient", [
`process.env["${this.urlEnvName()}"]`,
]);
public _liftedState(): Record<string, string> {
return {
$url: `process.env["${this.urlEnvName()}"]`,
Chriscbr marked this conversation as resolved.
Show resolved Hide resolved
};
}

private urlEnvName(): string {
Expand Down
48 changes: 33 additions & 15 deletions libs/awscdk/src/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import { BucketDeployment, Source } from "aws-cdk-lib/aws-s3-deployment";
import { LambdaDestination } from "aws-cdk-lib/aws-s3-notifications";
import { Construct } from "constructs";
import { App } from "./app";
import { cloud, core, std } from "@winglang/sdk";
import { cloud, std } from "@winglang/sdk";
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 { LiftMap, lift } from "@winglang/sdk/lib/core";
import { LiftMap, lift, InflightClient } from "@winglang/sdk/lib/core";

const EVENTS = {
[cloud.BucketEventType.DELETE]: EventType.OBJECT_REMOVED,
Expand All @@ -31,6 +31,14 @@ const EVENTS = {
* @inflight `@winglang/sdk.cloud.IBucketClient`
*/
export class Bucket extends cloud.Bucket implements IAwsBucket {
/** @internal */
public static _toInflightType(): string {
return InflightClient.forType(
__filename.replace("bucket", "bucket.inflight"),
"BucketClient"
);
}

private readonly bucket: S3Bucket;
private readonly public: boolean;
private bucketDeployment?: BucketDeployment;
Expand Down Expand Up @@ -59,7 +67,10 @@ export class Bucket extends cloud.Bucket implements IAwsBucket {
inflight: cloud.IBucketEventHandler,
opts?: cloud.BucketOnCreateOptions
): IAwsCdkFunction {
const functionHandler = lift({ handler: inflight, eventType: event }).inflight(async (ctx, event) => {
const functionHandler = lift({
handler: inflight,
eventType: event,
}).inflight(async (ctx, event) => {
const record = event.Records[0];
if (!record) {
throw new Error("No record found in the S3 event");
Expand Down Expand Up @@ -112,7 +123,11 @@ export class Bucket extends cloud.Bucket implements IAwsBucket {
inflight: cloud.IBucketEventHandler,
opts?: cloud.BucketOnCreateOptions
): void {
const fn = this.onEventFunction(cloud.BucketEventType.CREATE, inflight, opts);
const fn = this.onEventFunction(
cloud.BucketEventType.CREATE,
inflight,
opts
);

std.Node.of(this).addConnection({
source: this,
Expand All @@ -130,7 +145,11 @@ export class Bucket extends cloud.Bucket implements IAwsBucket {
inflight: cloud.IBucketEventHandler,
opts?: cloud.BucketOnDeleteOptions
): void {
const fn = this.onEventFunction(cloud.BucketEventType.DELETE, inflight, opts);
const fn = this.onEventFunction(
cloud.BucketEventType.DELETE,
inflight,
opts
);

std.Node.of(this).addConnection({
source: this,
Expand All @@ -148,7 +167,11 @@ export class Bucket extends cloud.Bucket implements IAwsBucket {
inflight: cloud.IBucketEventHandler,
opts?: cloud.BucketOnUpdateOptions
): void {
const fn = this.onEventFunction(cloud.BucketEventType.UPDATE, inflight, opts);
const fn = this.onEventFunction(
cloud.BucketEventType.UPDATE,
inflight,
opts
);

std.Node.of(this).addConnection({
source: this,
Expand Down Expand Up @@ -189,15 +212,10 @@ export class Bucket extends cloud.Bucket implements IAwsBucket {
}

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

private isPublicEnvName(): string {
return `${this.envName()}_IS_PUBLIC`;
public _liftedState(): Record<string, string> {
return {
$url: `process.env["${this.envName()}"]`,
};
}

private envName(): string {
Expand Down
22 changes: 15 additions & 7 deletions libs/awscdk/src/counter.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import { RemovalPolicy } from "aws-cdk-lib";
import { AttributeType, BillingMode, Table } from "aws-cdk-lib/aws-dynamodb";
import { Construct } from "constructs";
import { cloud, core, std } from "@winglang/sdk";
import { cloud, std } from "@winglang/sdk";
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";
import { InflightClient, LiftMap } from "@winglang/sdk/lib/core";

/**
* AWS implementation of `cloud.Counter`.
*
* @inflight `@winglang/sdk.cloud.ICounterClient`
*/
export class Counter extends cloud.Counter implements IAwsCounter {
/** @internal */
public static _toInflightType(): string {
return InflightClient.forType(
__filename.replace("counter", "counter.inflight"),
"CounterClient"
);
}

private readonly table: Table;

constructor(scope: Construct, id: string, props: cloud.CounterProps = {}) {
Expand Down Expand Up @@ -52,11 +60,11 @@ 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}`,
]);
public _liftedState(): Record<string, string> {
return {
$tableName: `process.env["${this.envName()}"]`,
$initial: `${this.initial}`,
};
}

private envName(): string {
Expand Down
29 changes: 20 additions & 9 deletions libs/awscdk/src/endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { CfnOutput } from "aws-cdk-lib";
import { Construct } from "constructs";
import { core, cloud, std } from "@winglang/sdk";
import { cloud, std } from "@winglang/sdk";
import { InflightClient } from "@winglang/sdk/lib/core";

/**
* AWS implementation of `cloud.Endpoint`.
*/
export class Endpoint extends cloud.Endpoint {
constructor(scope: Construct, id: string, url: string, props: cloud.EndpointProps = {}) {
/** @internal */
public static _toInflightType(): string {
return InflightClient.forType(
__filename.replace("endpoint", "endpoint.inflight"),
"EndpointClient"
);
}

constructor(
scope: Construct,
id: string,
url: string,
props: cloud.EndpointProps = {}
) {
super(scope, id, url, props);

new CfnOutput(this, "Url", {
Expand All @@ -21,13 +35,10 @@ export class Endpoint extends cloud.Endpoint {
}

/** @internal */
public _toInflight(): string {
return core.InflightClient.for(
__dirname,
__filename,
"EndpointClient",
[`process.env["${this.urlEnvName()}"]`]
);
public _liftedState(): Record<string, string> {
return {
$url: `process.env["${this.urlEnvName()}"]`,
};
}

private urlEnvName(): string {
Expand Down
21 changes: 15 additions & 6 deletions libs/awscdk/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { LogGroup, RetentionDays } from "aws-cdk-lib/aws-logs";
import { Asset } from "aws-cdk-lib/aws-s3-assets";
import { Construct, IConstruct } from "constructs";
import { cloud, std, core } from "@winglang/sdk";
import { cloud, std } from "@winglang/sdk";
import { NotImplementedError } from "@winglang/sdk/lib/core/errors";
import { createBundle } from "@winglang/sdk/lib/shared/bundling";
import {
Expand All @@ -24,7 +24,7 @@ import { makeAwsLambdaHandler } from "@winglang/sdk/lib/shared-aws/function-util
import { resolve } from "path";
import { renameSync, rmSync, writeFileSync } from "fs";
import { App } from "./app";
import { LiftMap } from "@winglang/sdk/lib/core";
import { InflightClient, LiftMap } from "@winglang/sdk/lib/core";

/**
* Implementation of `awscdk.Function` are expected to implement this
Expand Down Expand Up @@ -58,6 +58,14 @@ export class Function
extends cloud.Function
implements IAwsCdkFunction, IAwsFunction
{
/** @internal */
public static _toInflightType(): string {
return InflightClient.forType(
__filename.replace("function", "function.inflight"),
"FunctionClient"
);
}

private readonly function: CdkFunction;
private readonly assetPath: string;

Expand Down Expand Up @@ -151,10 +159,11 @@ export class Function
}

/** @internal */
public _toInflight(): string {
return core.InflightClient.for(__dirname, __filename, "FunctionClient", [
`process.env["${this.envName()}"], "${this.node.path}"`,
]);
public _liftedState(): Record<string, string> {
return {
$functionArn: `process.env["${this.envName()}"]`,
$constructPath: `"${this.node.path}"`,
};
}

/**
Expand Down
Loading
Loading