Skip to content

Commit

Permalink
feat(sdk): changing the bind method from internal to public (#3776)
Browse files Browse the repository at this point in the history
This PR changes the _bind methods, making them public and removing the internal tag. It makes more sense to make this method public, as a Wing class can rewrite the method when necessary.

Closes #3775 


## Checklist

- [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] 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
marciocadev authored Aug 11, 2023
1 parent 42a1eb7 commit f416d4b
Show file tree
Hide file tree
Showing 38 changed files with 80 additions and 115 deletions.
2 changes: 1 addition & 1 deletion apps/jsii-docgen/src/docgen/view/wing-filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const VISIBLE_SUBMODULES = [
"redis",
"http",
];
export const HIDDEN_METHODS = ["toString", "toJSON"];
export const HIDDEN_METHODS = ["toString", "toJSON", "bind"];
export const HIDDEN_PROPS = ["node", "display"];
export const HIDDEN_STATIC = ["of", "isConstruct", "addConnection"];
export const HEADLESS_SUBMODULES = ["cloud"];
Expand Down
2 changes: 1 addition & 1 deletion examples/tests/valid/dynamo.w
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class DynamoTable {
this.tableName = this.table.name;
}

_bind(host: std.IInflightHost, ops: Array<str>) {
bind(host: std.IInflightHost, ops: Array<str>) {
if let host = aws.Function.from(host) {
if ops.contains("putItem") {
host.addPolicyStatements([aws.PolicyStatement {
Expand Down
22 changes: 9 additions & 13 deletions libs/wingsdk/src/std/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ export interface IResource extends IInspectable, IConstruct {
*
* If `ops` contains any operations not supported by the resource, it should throw an
* error.
*
* @internal
*/
_bind(host: IInflightHost, ops: string[]): void;
bind(host: IInflightHost, ops: string[]): void;

/**
* Register that the resource needs to be bound to the host for the given
* operations. This means that the resource's `_bind` method will be called
* operations. This means that the resource's `bind` method will be called
* during pre-synthesis.
*
* @internal
Expand Down Expand Up @@ -139,7 +137,7 @@ export abstract class Resource extends Construct implements IResource {
*
* - Primitives and Duration objects are ignored.
* - Arrays, sets and maps and structs (Objects) are recursively bound.
* - Resources are bound to the host by calling their _bind() method.
* - Resources are bound to the host by calling their bind() method.
*
* @param obj The object to bind.
* @param host The host to bind to
Expand Down Expand Up @@ -188,7 +186,7 @@ export abstract class Resource extends Construct implements IResource {
return;
}

// if the object is a resource (i.e. has a "_bind" method"), register a binding between it and the host.
// if the object is a resource (i.e. has a "bind" method"), register a binding between it and the host.
if (isResource(obj)) {
// Explicitly register the resource's `$inflight_init` op, which is a special op that can be used to makes sure
// the host can instantiate a client for this resource.
Expand Down Expand Up @@ -251,20 +249,18 @@ export abstract class Resource extends Construct implements IResource {
*
* You can override this method to perform additional logic like granting
* IAM permissions to the host based on what methods are being called. But
* you must call `super._bind(host, ops)` to ensure that the resource is
* you must call `super.bind(host, ops)` to ensure that the resource is
* actually bound.
*
* @internal
*/
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
// Do nothing by default
host;
ops;
}

/**
* Register that the resource needs to be bound to the host for the given
* operations. This means that the resource's `_bind` method will be called
* operations. This means that the resource's `bind` method will be called
* during pre-synthesis.
*
* @internal
Expand Down Expand Up @@ -331,7 +327,7 @@ export abstract class Resource extends Construct implements IResource {
// By aggregating the binding operations, we can avoid performing
// multiple bindings for the same resource-host pairs.
for (const [host, ops] of this.bindMap.entries()) {
this._bind(host, Array.from(ops));
this.bind(host, Array.from(ops));
}
}

Expand Down Expand Up @@ -543,7 +539,7 @@ function isIResourceType(t: any): t is new (...args: any[]) => IResource {
return (
t instanceof Function &&
"prototype" in t &&
typeof t.prototype._bind === "function" &&
typeof t.prototype.bind === "function" &&
typeof t.prototype._registerBind === "function"
);
}
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-awscdk/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ export class Bucket extends cloud.Bucket {
);
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
if (!(host instanceof Function)) {
throw new Error("buckets can only be bound by tfaws.Function for now");
}
Expand All @@ -194,7 +193,7 @@ export class Bucket extends cloud.Bucket {
// it may not be resolved until deployment time.
host.addEnvironment(this.envName(), this.bucket.bucketName);

super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
3 changes: 1 addition & 2 deletions libs/wingsdk/src/target-awscdk/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export class Counter extends cloud.Counter {
});
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
if (!(host instanceof Function)) {
throw new Error("counters can only be bound by awscdk.Function for now");
}
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-awscdk/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ export class Function extends cloud.Function {
this.arn = this.function.functionArn;
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
if (!(host instanceof Function)) {
throw new Error("functions can only be bound by awscdk.Function for now");
}
Expand All @@ -68,7 +67,7 @@ export class Function extends cloud.Function {
// it may not be resolved until deployment time.
host.addEnvironment(this.envName(), this.function.functionArn);

super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-awscdk/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ export class Queue extends cloud.Queue {
return fn;
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
if (!(host instanceof Function)) {
throw new Error("queues can only be bound by tfaws.Function for now");
}
Expand All @@ -95,7 +94,7 @@ export class Queue extends cloud.Queue {
// it may not be resolved until deployment time.
host.addEnvironment(env, this.queue.queueUrl);

super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-awscdk/secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ export class Secret extends cloud.Secret {
return this.secret.secretArn;
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
if (!(host instanceof Function)) {
throw new Error("secrets can only be bound by awscdk.Function for now");
}
Expand All @@ -53,7 +52,7 @@ export class Secret extends cloud.Secret {

host.addEnvironment(this.envName(), this.secret.secretArn);

super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-awscdk/test-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export class TestRunner extends std.TestRunner {
output.overrideLogicalId(OUTPUT_TEST_RUNNER_FUNCTION_ARNS);
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
if (!(host instanceof AwsFunction)) {
throw new Error("TestRunner can only be bound by tfaws.Function for now");
}
Expand All @@ -50,7 +49,7 @@ export class TestRunner extends std.TestRunner {
JSON.stringify([...testFunctions.entries()])
);

super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-awscdk/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ export class Topic extends cloud.Topic {
return fn;
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
if (!(host instanceof Function)) {
throw new Error("topics can only be bound by awscdk.Function for now");
}
Expand All @@ -81,7 +80,7 @@ export class Topic extends cloud.Topic {

host.addEnvironment(this.envName(), this.topic.topicArn);

super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-sim/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,9 @@ export class Api extends cloud.Api implements ISimulatorResource {
return schema;
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
bindSimulatorResource(__filename, this, host);
super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-sim/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ export class Bucket extends cloud.Bucket implements ISimulatorResource {
return schema;
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
bindSimulatorResource(__filename, this, host);
super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-sim/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ export class Counter extends cloud.Counter implements ISimulatorResource {
return schema;
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
bindSimulatorResource(__filename, this, host);
super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
4 changes: 2 additions & 2 deletions libs/wingsdk/src/target-sim/event-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ export class EventMapping extends Resource implements ISimulatorResource {
return schema;
}

public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
bindSimulatorResource(__filename, this, host);
super._bind(host, ops);
super.bind(host, ops);
}

public _toInflight(): Code {
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-sim/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ export class Function extends cloud.Function implements ISimulatorResource {
return schema;
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
bindSimulatorResource(__filename, this, host);
super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-sim/on-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ export class OnDeploy extends cloud.OnDeploy {
return schema;
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
bindSimulatorResource(__filename, this, host);
super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-sim/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,9 @@ export class Queue extends cloud.Queue implements ISimulatorResource {
return schema;
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
bindSimulatorResource(__filename, this, host);
super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-sim/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ export class Redis extends ex.Redis implements ISimulatorResource {
return schema;
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
bindSimulatorResource(__filename, this, host);
super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-sim/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ export class Schedule extends cloud.Schedule implements ISimulatorResource {
return makeSimulatorJsClient(__filename, this);
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
bindSimulatorResource(__filename, this, host);
super._bind(host, ops);
super.bind(host, ops);
}
}
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-sim/secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ export class Secret extends cloud.Secret implements ISimulatorResource {
ResourceNames.generateName(this, { disallowedRegex: /[^\w]/g });
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
bindSimulatorResource(__filename, this, host);
super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
4 changes: 2 additions & 2 deletions libs/wingsdk/src/target-sim/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ export class Service extends cloud.Service implements ISimulatorResource {
return schema;
}

public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
bindSimulatorResource(__filename, this, host);
super._bind(host, ops);
super.bind(host, ops);
}

public _toInflight(): core.Code {
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-sim/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ export class Table extends ex.Table implements ISimulatorResource {
return schema;
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
bindSimulatorResource(__filename, this, host);
super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-sim/test-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ export class TestRunner extends std.TestRunner implements ISimulatorResource {
return schema;
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
bindSimulatorResource("test-runner", this, host);
super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
5 changes: 2 additions & 3 deletions libs/wingsdk/src/target-sim/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ export class Topic extends cloud.Topic implements ISimulatorResource {
return fn;
}

/** @internal */
public _bind(host: IInflightHost, ops: string[]): void {
public bind(host: IInflightHost, ops: string[]): void {
bindSimulatorResource(__filename, this, host);
super._bind(host, ops);
super.bind(host, ops);
}

/** @internal */
Expand Down
Loading

0 comments on commit f416d4b

Please sign in to comment.