From 9ee10ba0fd5094a274100aeaa13a983ed3c1bc34 Mon Sep 17 00:00:00 2001 From: Sebastian Korfmann Date: Wed, 25 Oct 2023 15:07:12 +0200 Subject: [PATCH 1/2] Fixes for latest winglang version --- dynamodb.w | 74 +++++++++++++++++++++++++++--------------------------- main.w | 21 +++++----------- 2 files changed, 43 insertions(+), 52 deletions(-) diff --git a/dynamodb.w b/dynamodb.w index 027358b..6e957d6 100644 --- a/dynamodb.w +++ b/dynamodb.w @@ -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>; - extern "./util.js" static inflight jsonToArray(json: Json): Array>; - extern "./util.js" static inflight mutArrayToJson(json: MutArray>): Json; +pub class Util { + extern "./util.js" pub static inflight jsonToMutArray(json: Json): MutArray>; + extern "./util.js" pub static inflight jsonToArray(json: Json): Array>; + extern "./util.js" pub static inflight mutArrayToJson(json: MutArray>): Json; } // TODO: https://github.com/winglang/wing/issues/3350 @@ -29,7 +29,7 @@ struct DynamoDBTableProps { hashKey: str; } -class DynamoDBTableSim { +pub class DynamoDBTableSim { key: str; data: cloud.Bucket; @@ -39,14 +39,14 @@ class DynamoDBTableSim { this.data.addObject(this.key, "[]"); } - inflight putItem(item: Map) { + pub inflight putItem(item: Map) { 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): Map? { + pub inflight getItem(map: Map): Map? { let items = this.data.getJson(this.key); let itemsMut = Util.jsonToMutArray(items); for item in itemsMut { @@ -66,14 +66,14 @@ class DynamoDBTableSim { return nil; } - inflight scan(): Array> { + pub inflight scan(): Array> { 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) { @@ -95,48 +95,48 @@ class DynamoDBTableAws { bind(host: std.IInflightHost, ops: Array) { 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>>; - extern "./dynamo.js" inflight _scan(tableName: str): Map>>>; + extern "./dynamo.js" static inflight _putItem(tableName: str, item: Json): void; + extern "./dynamo.js" static inflight _getItem(tableName: str, key: Json): Map>>; + extern "./dynamo.js" static inflight _scan(tableName: str): Map>>>; - inflight putItem(item: Map) { + pub inflight putItem(item: Map) { let json = this._itemToJson(item); - this._putItem(this.tableName, json); + DynamoDBTableAws._putItem(this.tableName, json); } - inflight getItem(key: Map): Map { + pub inflight getItem(key: Map): Map { 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> { - let result = this._scan(this.tableName); + pub inflight scan(): Array> { + let result = DynamoDBTableAws._scan(this.tableName); let rawItems = result.get("Items"); let items = MutArray>[]; for rawItem in rawItems { @@ -195,7 +195,7 @@ class DynamoDBTableAws { } } -class DynamoDBTable { +pub class DynamoDBTable { tableSim: DynamoDBTableSim?; tableAws: DynamoDBTableAws?; @@ -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): Map? { + pub inflight getItem(key: Map): Map? { assert(key.size() == 1); if let tableSim = this.tableSim { return tableSim.getItem(key); @@ -253,7 +253,7 @@ class DynamoDBTable { throw("no table instance found for getItem"); } - inflight putItem(item: Map) { + pub inflight putItem(item: Map) { if let tableSim = this.tableSim { tableSim.putItem(item); return; @@ -265,7 +265,7 @@ class DynamoDBTable { throw("no table instance found for putItem"); } - inflight scan(): Array> { + pub inflight scan(): Array> { if let tableSim = this.tableSim { return tableSim.scan(); } diff --git a/main.w b/main.w index 706cba2..ccac495 100644 --- a/main.w +++ b/main.w @@ -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 { @@ -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)); } @@ -81,7 +81,7 @@ class Store { } } - inflight getRandomPair(): Array { + pub inflight getRandomPair(): Array { let entries = this.table.scan(); let firstIdx = math.floor(math.random() * entries.length); @@ -95,7 +95,7 @@ class Store { return [first, second]; } - inflight updateScores(winner: str, loser: str): Array { + pub inflight updateScores(winner: str, loser: str): Array { let entries = this.list(); let winnerEntry = this.getEntry(winner); @@ -125,7 +125,7 @@ class Store { return [winnerNewScore, loserNewScore]; } - inflight list(): Array { + pub inflight list(): Array { let items = this.table.scan(); let entries = MutArray[]; for item in items { @@ -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(); @@ -210,7 +204,6 @@ api.post("/requestChoices", inflight (_) => { entryNames.push(entry.name); } return cloud.ApiResponse { - headers: corsHeaders, status: 200, body: Json.stringify(entryNames), }; @@ -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), }; @@ -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), }; From 25cec82fb049f0d8356373309c6233ffa91d2cf2 Mon Sep 17 00:00:00 2001 From: Chris Rybicki Date: Wed, 25 Oct 2023 11:58:28 -0400 Subject: [PATCH 2/2] fix --- main.w | 1 - 1 file changed, 1 deletion(-) diff --git a/main.w b/main.w index ccac495..0a4ebab 100644 --- a/main.w +++ b/main.w @@ -239,7 +239,6 @@ api.post("/selectWinner", inflight (req) => { }; return cloud.ApiResponse { - headers: corsHeaders, status: 200, body: Json.stringify(payload), };