winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
4.76k stars 187 forks source link

`Counter.keys()` and `Counter.entries()` methods #6555

Open Chriscbr opened 1 month ago

Chriscbr commented 1 month ago

Use Case

To visualize the entire state of Counter (https://github.com/winglang/wing/issues/6100), you need to have some way to find out what keys have values stored

Proposed Solution

bring cloud;

let c = new cloud.Counter();

test "obtain counter's keys" {
  expect.equal(c.keys() == ["default"]);
  c.inc();
  expect.equal(c.keys() == ["default"]);
  c.inc(1, "my-key");
  expect.equal(c.keys() == ["default", "my-key"]);
}

Alternative:

bring cloud;

let c = new cloud.Counter();

test "obtain counter's keys" {
  expect.equal(c.entries() == [CounterEntry { key: "default", value: 0 }]);
  c.inc();
  expect.equal(c.entries() == [CounterEntry { key: "default", value: 1 }]);
  c.inc(1, "my-key");
  expect.equal(c.entries() == [CounterEntry { key: "default", value: 1 }, CounterEntry { key: "my-key", value: 1 }]);
}

Implementation Notes

For AWS targets which implement Counter using DynamoDB, it might be possible to implement these with the Scan operation

Component

SDK

Community Notes