Skip to content

Commit 53a5c03

Browse files
authored
feat: create jsii@5 compatible replacement for KeyValueTableWidget (#619)
As noted in https://aws.github.io/jsii/compiler-and-rosetta-maintenance/#how-difficult-is-it-to-migrate-from-1x-to-50x, tuples aren't supported with jsii@5. `KeyValueTableWidget` will be removed in a future version for the upgrade. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 59bc237 commit 53a5c03

File tree

6 files changed

+242
-29
lines changed

6 files changed

+242
-29
lines changed

API.md

Lines changed: 173 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,10 @@
1-
import { TextWidget } from "aws-cdk-lib/aws-cloudwatch";
1+
import { KeyValueTableWidgetV2 } from "./KeyValueTableWidgetV2";
22

3-
import { FullWidth } from "../../common/widget";
4-
5-
export class KeyValueTableWidget extends TextWidget {
3+
/**
4+
* @deprecated Use {@link KeyValueTableWidgetV2} instead.
5+
*/
6+
export class KeyValueTableWidget extends KeyValueTableWidgetV2 {
67
constructor(data: [string, string][]) {
7-
super({
8-
width: FullWidth,
9-
height: 3,
10-
markdown: KeyValueTableWidget.toMarkdown(data),
11-
});
12-
}
13-
14-
private static toMarkdown(data: [string, string][]) {
15-
let headerRow = "";
16-
let subHeaderRow = "";
17-
let valueRow = "";
18-
19-
data.forEach(([key, value]) => {
20-
headerRow += "| " + key;
21-
subHeaderRow += "|---";
22-
valueRow += "| " + value;
23-
});
24-
25-
return `${headerRow}\n${subHeaderRow}\n${valueRow}`;
8+
super(data.map(([key, value]) => ({ key, value })));
269
}
2710
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { TextWidget } from "aws-cdk-lib/aws-cloudwatch";
2+
3+
import { FullWidth } from "../../common/widget";
4+
5+
export interface KeyValue {
6+
readonly key: string;
7+
readonly value: string;
8+
}
9+
10+
export class KeyValueTableWidgetV2 extends TextWidget {
11+
constructor(data: KeyValue[]) {
12+
super({
13+
width: FullWidth,
14+
height: 3,
15+
markdown: KeyValueTableWidgetV2.toMarkdown(data),
16+
});
17+
}
18+
19+
private static toMarkdown(data: KeyValue[]) {
20+
let headerRow = "";
21+
let subHeaderRow = "";
22+
let valueRow = "";
23+
24+
data.forEach(({ key, value }) => {
25+
headerRow += "| " + key;
26+
subHeaderRow += "|---";
27+
valueRow += "| " + value;
28+
});
29+
30+
return `${headerRow}\n${subHeaderRow}\n${valueRow}`;
31+
}
32+
}

lib/dashboard/widget/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from "./BitmapWidget";
33
export * from "./CustomWidget";
44
export * from "./HeaderWidget";
55
export * from "./KeyValueTableWidget";
6+
export * from "./KeyValueTableWidgetV2";
67
export * from "./MonitoringHeaderWidget";
78
export * from "./StrictGraphWidget";
89
export * from "./UnofficialWidgets";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { KeyValueTableWidgetV2, KeyValue } from "../../../lib/dashboard/widget";
2+
3+
test("snapshot test", () => {
4+
const data: KeyValue[] = [
5+
{ key: "name", value: "John Wick" },
6+
{ key: "has", value: "a dog" },
7+
];
8+
const widget = new KeyValueTableWidgetV2(data);
9+
10+
expect(widget.toJson()).toMatchSnapshot();
11+
});

test/dashboard/widget/__snapshots__/KeyValueTableWidgetV2.test.ts.snap

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)