-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.w
117 lines (94 loc) · 2.75 KB
/
main.w
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
bring cloud;
interface Storage {
inflight putObject(key: str, contents: str): void;
inflight getObject(key: str): str;
onObjectCreated(callback: inflight (str): void): void;
}
class BucketStorage impl Storage {
bucket: cloud.Bucket;
new() {
this.bucket = new cloud.Bucket();
}
pub inflight putObject(key: str, contents: str) {
this.bucket.put(key, contents);
}
pub inflight getObject(key: str): str {
return this.bucket.get(key);
}
pub onObjectCreated(callback: inflight (str): void) {
this.bucket.onCreate(inflight (key) => {
callback(key);
});
}
}
bring dynamodb;
class TableStorage impl Storage {
table: dynamodb.Table;
topic: cloud.Topic;
new() {
this.table = new dynamodb.Table(
hashKey: "key",
attributes: [
{ name: "key", type: "S" },
],
);
this.topic = new cloud.Topic();
}
pub inflight putObject(key: str, contents: str) {
this.table.put(
Item: {
key,
contents,
},
);
this.topic.publish(key);
}
pub inflight getObject(key: str): str {
let item = this.table.get(
Key: {
key,
},
).Item!;
return item.get("contents").asStr();
}
pub onObjectCreated(callback: inflight (str): void) {
this.topic.onMessage(inflight (key) => {
callback(key);
});
}
}
class FileProcessor {
uploads: Storage;
processedObjects: Storage;
new(uploads: Storage, processed: Storage) {
this.uploads = uploads;
this.processedObjects = processed;
this.uploads.onObjectCreated(inflight (key) => {
let contents = this.uploads.getObject(key);
let uppercaseContents = contents.uppercase();
this.processedObjects.putObject(key, uppercaseContents);
});
}
pub inflight processObject(key: str, contents: str) {
this.uploads.putObject(key, contents);
}
pub inflight getProcessedObject(key: str): str {
return this.processedObjects.getObject(key);
}
pub onFileProcessed(callback: inflight (str): void) {
this.processedObjects.onObjectCreated(inflight (key) => {
callback(key);
});
}
}
let processor = new FileProcessor(
new TableStorage() as "UploadsStorage",
new BucketStorage() as "ProcessedStorage",
);
processor.onFileProcessed(inflight (key) => {
let fileContents = processor.getProcessedObject(key);
log("File [{key}] was processed: {fileContents}");
});
new cloud.Function(inflight () => {
processor.processObject("hello", "world");
}) as "ProcessHelloWorldFile";