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

Fixes for latest winglang version #22

Merged
merged 2 commits into from
Oct 25, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions dynamodb.w
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ bring util;

// --- dynamodb ---

enum AttributeType {
pub enum AttributeType {
String,
Number, // note: DynamoDB requires you to provide the `value` as a string
Binary,
}

struct Attribute {
pub struct Attribute {
type: AttributeType;
value: Json;
}

class Util {
extern "./util.js" static inflight jsonToMutArray(json: Json): MutArray<Map<Attribute>>;
extern "./util.js" static inflight jsonToArray(json: Json): Array<Map<Attribute>>;
extern "./util.js" static inflight mutArrayToJson(json: MutArray<Map<Attribute>>): Json;
pub class Util {
extern "./util.js" pub static inflight jsonToMutArray(json: Json): MutArray<Map<Attribute>>;
extern "./util.js" pub static inflight jsonToArray(json: Json): Array<Map<Attribute>>;
extern "./util.js" pub static inflight mutArrayToJson(json: MutArray<Map<Attribute>>): Json;
}

// TODO: https://github.com/winglang/wing/issues/3350
Expand All @@ -29,7 +29,7 @@ struct DynamoDBTableProps {
hashKey: str;
}

class DynamoDBTableSim {
pub class DynamoDBTableSim {
key: str;
data: cloud.Bucket;

Expand All @@ -39,14 +39,14 @@ class DynamoDBTableSim {
this.data.addObject(this.key, "[]");
}

inflight putItem(item: Map<Attribute>) {
pub inflight putItem(item: Map<Attribute>) {
let items = this.data.getJson(this.key);
let itemsMut = Util.jsonToMutArray(items);
itemsMut.push(item);
this.data.putJson(this.key, Util.mutArrayToJson(itemsMut));
}

inflight getItem(map: Map<Attribute>): Map<Attribute>? {
pub inflight getItem(map: Map<Attribute>): Map<Attribute>? {
let items = this.data.getJson(this.key);
let itemsMut = Util.jsonToMutArray(items);
for item in itemsMut {
Expand All @@ -66,14 +66,14 @@ class DynamoDBTableSim {
return nil;
}

inflight scan(): Array<Map<Attribute>> {
pub inflight scan(): Array<Map<Attribute>> {
let items = this.data.getJson(this.key);
return Util.jsonToArray(items);
}
}

class DynamoDBTableAws {
table: tfaws.dynamodbTable.DynamodbTable;
pub class DynamoDBTableAws {
pub table: tfaws.dynamodbTable.DynamodbTable;
tableName: str;
hashKey: str;
init(props: DynamoDBTableProps) {
Expand All @@ -95,48 +95,48 @@ class DynamoDBTableAws {
bind(host: std.IInflightHost, ops: Array<str>) {
if let host = aws.Function.from(host) {
if ops.contains("putItem") {
host.addPolicyStatements([aws.PolicyStatement {
host.addPolicyStatements(aws.PolicyStatement {
actions: ["dynamodb:PutItem"],
resources: [this.table.arn],
effect: aws.Effect.ALLOW,
}]);
});
}

if ops.contains("getItem") {
host.addPolicyStatements([aws.PolicyStatement {
host.addPolicyStatements(aws.PolicyStatement {
actions: ["dynamodb:GetItem"],
resources: [this.table.arn],
effect: aws.Effect.ALLOW,
}]);
});
}

if ops.contains("scan") {
host.addPolicyStatements([aws.PolicyStatement {
host.addPolicyStatements(aws.PolicyStatement {
actions: ["dynamodb:Scan"],
resources: [this.table.arn],
effect: aws.Effect.ALLOW,
}]);
});
}
}
}

extern "./dynamo.js" inflight _putItem(tableName: str, item: Json): void;
extern "./dynamo.js" inflight _getItem(tableName: str, key: Json): Map<Map<Map<str>>>;
extern "./dynamo.js" inflight _scan(tableName: str): Map<Array<Map<Map<str>>>>;
extern "./dynamo.js" static inflight _putItem(tableName: str, item: Json): void;
extern "./dynamo.js" static inflight _getItem(tableName: str, key: Json): Map<Map<Map<str>>>;
extern "./dynamo.js" static inflight _scan(tableName: str): Map<Array<Map<Map<str>>>>;

inflight putItem(item: Map<Attribute>) {
pub inflight putItem(item: Map<Attribute>) {
let json = this._itemToJson(item);
this._putItem(this.tableName, json);
DynamoDBTableAws._putItem(this.tableName, json);
}

inflight getItem(key: Map<Attribute>): Map<Attribute> {
pub inflight getItem(key: Map<Attribute>): Map<Attribute> {
let json = this._itemToJson(key);
let result = this._getItem(this.tableName, json);
let result = DynamoDBTableAws._getItem(this.tableName, json);
return this._rawMapToItem(result.get("Item"));
}

inflight scan(): Array<Map<Attribute>> {
let result = this._scan(this.tableName);
pub inflight scan(): Array<Map<Attribute>> {
let result = DynamoDBTableAws._scan(this.tableName);
let rawItems = result.get("Items");
let items = MutArray<Map<Attribute>>[];
for rawItem in rawItems {
Expand Down Expand Up @@ -195,7 +195,7 @@ class DynamoDBTableAws {
}
}

class DynamoDBTable {
pub class DynamoDBTable {
tableSim: DynamoDBTableSim?;
tableAws: DynamoDBTableAws?;

Expand All @@ -216,33 +216,33 @@ class DynamoDBTable {
if let tableAws = this.tableAws {
if let host = aws.Function.from(host) {
if ops.contains("putItem") {
host.addPolicyStatements([aws.PolicyStatement {
host.addPolicyStatements(aws.PolicyStatement {
actions: ["dynamodb:PutItem"],
resources: [tableAws.table.arn],
effect: aws.Effect.ALLOW,
}]);
});
}

if ops.contains("getItem") {
host.addPolicyStatements([aws.PolicyStatement {
host.addPolicyStatements(aws.PolicyStatement {
actions: ["dynamodb:GetItem"],
resources: [tableAws.table.arn],
effect: aws.Effect.ALLOW,
}]);
});
}

if ops.contains("scan") {
host.addPolicyStatements([aws.PolicyStatement {
host.addPolicyStatements(aws.PolicyStatement {
actions: ["dynamodb:Scan"],
resources: [tableAws.table.arn],
effect: aws.Effect.ALLOW,
}]);
});
}
}
}
}

inflight getItem(key: Map<Attribute>): Map<Attribute>? {
pub inflight getItem(key: Map<Attribute>): Map<Attribute>? {
assert(key.size() == 1);
if let tableSim = this.tableSim {
return tableSim.getItem(key);
Expand All @@ -253,7 +253,7 @@ class DynamoDBTable {
throw("no table instance found for getItem");
}

inflight putItem(item: Map<Attribute>) {
pub inflight putItem(item: Map<Attribute>) {
if let tableSim = this.tableSim {
tableSim.putItem(item);
return;
Expand All @@ -265,7 +265,7 @@ class DynamoDBTable {
throw("no table instance found for putItem");
}

inflight scan(): Array<Map<Attribute>> {
pub inflight scan(): Array<Map<Attribute>> {
if let tableSim = this.tableSim {
return tableSim.scan();
}
Expand Down
21 changes: 6 additions & 15 deletions main.w
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct SelectWinnerResponse {
class Util {
extern "./util.js" static inflight jsonToSelectWinnerRequest(json: Json): SelectWinnerRequest;

static inflight clamp(value: num, min: num, max: num): num {
pub static inflight clamp(value: num, min: num, max: num): num {
if value < min {
return min;
} elif value > max {
Expand All @@ -60,7 +60,7 @@ class Store {
this.table = new ddb.DynamoDBTable(hashKey: "Name") as "Entries";
}

inflight setEntry(entry: Entry) {
pub inflight setEntry(entry: Entry) {
this.table.putItem(_entryToMap(entry));
}

Expand All @@ -81,7 +81,7 @@ class Store {
}
}

inflight getRandomPair(): Array<Entry> {
pub inflight getRandomPair(): Array<Entry> {
let entries = this.table.scan();

let firstIdx = math.floor(math.random() * entries.length);
Expand All @@ -95,7 +95,7 @@ class Store {
return [first, second];
}

inflight updateScores(winner: str, loser: str): Array<num> {
pub inflight updateScores(winner: str, loser: str): Array<num> {
let entries = this.list();

let winnerEntry = this.getEntry(winner);
Expand Down Expand Up @@ -125,7 +125,7 @@ class Store {
return [winnerNewScore, loserNewScore];
}

inflight list(): Array<Entry> {
pub inflight list(): Array<Entry> {
let items = this.table.scan();
let entries = MutArray<Entry>[];
for item in items {
Expand Down Expand Up @@ -191,17 +191,11 @@ new cloud.OnDeploy(inflight () => {
}
}) as "InitializeTable";

let api = new cloud.Api() as "VotingAppApi";
let api = new cloud.Api(cors: true) as "VotingAppApi";

let website = new cloud.Website(path: "./website/build");
website.addJson("config.json", { apiUrl: api.url });

let corsHeaders = {
"Access-Control-Allow-Headers" => "*",
"Access-Control-Allow-Origin" => "*",
"Access-Control-Allow-Methods" => "OPTIONS,GET",
};

// Select two random items from the list of items for the user to choose between
api.post("/requestChoices", inflight (_) => {
let entries = store.getRandomPair();
Expand All @@ -210,7 +204,6 @@ api.post("/requestChoices", inflight (_) => {
entryNames.push(entry.name);
}
return cloud.ApiResponse {
headers: corsHeaders,
status: 200,
body: Json.stringify(entryNames),
};
Expand All @@ -220,7 +213,6 @@ api.post("/requestChoices", inflight (_) => {
api.get("/leaderboard", inflight (_) => {
let entries = store.list();
return cloud.ApiResponse {
headers: corsHeaders,
status: 200,
body: Json.stringify(entries),
};
Expand All @@ -237,7 +229,6 @@ api.post("/selectWinner", inflight (req) => {
newScores = store.updateScores(selections.winner, selections.loser);
} catch e {
return cloud.ApiResponse {
headers: corsHeaders,
status: 400,
body: "Error: " + Json.stringify(e),
};
Expand Down
Loading