Skip to content

Commit

Permalink
Merge pull request #22 from winglang/fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriscbr authored Oct 25, 2023
2 parents 4f09b14 + 25cec82 commit ea7018e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 53 deletions.
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
22 changes: 6 additions & 16 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 All @@ -248,7 +239,6 @@ api.post("/selectWinner", inflight (req) => {
};

return cloud.ApiResponse {
headers: corsHeaders,
status: 200,
body: Json.stringify(payload),
};
Expand Down

0 comments on commit ea7018e

Please sign in to comment.