Skip to content

Commit 7f427c7

Browse files
authored
Merge branch 'master' into DerkSchooltink/lambda-ruby-2.7-support
2 parents 113b2d1 + 1186227 commit 7f427c7

File tree

5 files changed

+77
-51
lines changed

5 files changed

+77
-51
lines changed

.mergify.yml

+1-7
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,11 @@ pull_request_rules:
5656
- status-success~=AWS CodeBuild us-east-1
5757
- status-success=Semantic Pull Request
5858
- status-success=mandatory-changes
59-
- name: when changes are requested
60-
actions:
61-
comment:
62-
message: Once all the requested changes have been addressed, and the PR is ready for another review, remember to [dismiss the review](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/dismissing-a-pull-request-review).
63-
conditions:
64-
- "#changes-requested-reviews-by>=1"
6559
- name: remove stale reviews
6660
actions:
6761
dismiss_reviews:
6862
approved: true
69-
changes_requested: false
63+
changes_requested: true
7064
conditions:
7165
- author!=dependabot[bot]
7266
- author!=dependabot-preview[bot]

packages/@aws-cdk/aws-kinesis/lib/stream.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as iam from '@aws-cdk/aws-iam';
22
import * as kms from '@aws-cdk/aws-kms';
3-
import { Construct, IResource, Resource, Stack } from '@aws-cdk/core';
3+
import { Construct, Duration, IResource, Resource, Stack } from '@aws-cdk/core';
44
import { CfnStream } from './kinesis.generated';
55

66
/**
@@ -185,9 +185,9 @@ export interface StreamProps {
185185

186186
/**
187187
* The number of hours for the data records that are stored in shards to remain accessible.
188-
* @default 24
188+
* @default Duration.hours(24)
189189
*/
190-
readonly retentionPeriodHours?: number;
190+
readonly retentionPeriod?: Duration;
191191

192192
/**
193193
* The number of shards for the stream.
@@ -261,9 +261,9 @@ export class Stream extends StreamBase {
261261
});
262262

263263
const shardCount = props.shardCount || 1;
264-
const retentionPeriodHours = props.retentionPeriodHours || 24;
265-
if (retentionPeriodHours < 24 && retentionPeriodHours > 168) {
266-
throw new Error("retentionPeriodHours must be between 24 and 168 hours");
264+
const retentionPeriodHours = props.retentionPeriod?.toHours() ?? 24;
265+
if (retentionPeriodHours < 24 || retentionPeriodHours > 168) {
266+
throw new Error(`retentionPeriod must be between 24 and 168 hours. Received ${retentionPeriodHours}`);
267267
}
268268

269269
const { streamEncryption, encryptionKey } = this.parseEncryption(props);

packages/@aws-cdk/aws-kinesis/test/test.stream.ts

+12-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from '@aws-cdk/assert';
22
import * as iam from '@aws-cdk/aws-iam';
33
import * as kms from '@aws-cdk/aws-kms';
4-
import { App, Stack } from '@aws-cdk/core';
4+
import { App, Duration, Stack } from '@aws-cdk/core';
55
import { Test } from 'nodeunit';
66
import { Stream, StreamEncryption } from '../lib';
77

@@ -64,8 +64,8 @@ export = {
6464
"uses explicit retention period"(test: Test) {
6565
const stack = new Stack();
6666

67-
new Stream(stack, 'MyStream', {
68-
retentionPeriodHours: 168
67+
new Stream(stack, "MyStream", {
68+
retentionPeriod: Duration.hours(168)
6969
});
7070

7171
expect(stack).toMatch({
@@ -83,23 +83,17 @@ export = {
8383
test.done();
8484
},
8585
"retention period must be between 24 and 168 hours"(test: Test) {
86-
test.throws({
87-
block: () => {
88-
new Stream(new Stack(), 'MyStream', {
89-
retentionPeriodHours: 169
90-
});
91-
},
92-
message: "retentionPeriodHours must be between 24 and 168 hours"
93-
});
86+
test.throws(() => {
87+
new Stream(new Stack(), 'MyStream', {
88+
retentionPeriod: Duration.hours(169)
89+
});
90+
}, /retentionPeriod must be between 24 and 168 hours. Received 169/);
9491

95-
test.throws({
96-
block: () => {
92+
test.throws(() => {
9793
new Stream(new Stack(), 'MyStream', {
98-
retentionPeriodHours: 23
99-
});
100-
},
101-
message: "retentionPeriodHours must be between 24 and 168 hours"
102-
});
94+
retentionPeriod: Duration.hours(23)
95+
});
96+
}, /retentionPeriod must be between 24 and 168 hours. Received 23/);
10397

10498
test.done();
10599
},

packages/@aws-cdk/core/lib/size.ts

+35-14
Original file line numberDiff line numberDiff line change
@@ -63,49 +63,61 @@ export class Size {
6363
/**
6464
* Return this storage as a total number of kibibytes.
6565
*/
66-
public toKibibytes(opts: StorageConversionOptions = {}): number {
66+
public toKibibytes(opts: SizeConversionOptions = {}): number {
6767
return convert(this.amount, this.unit, StorageUnit.Kibibytes, opts);
6868
}
6969

7070
/**
7171
* Return this storage as a total number of mebibytes.
7272
*/
73-
public toMebibytes(opts: StorageConversionOptions = {}): number {
73+
public toMebibytes(opts: SizeConversionOptions = {}): number {
7474
return convert(this.amount, this.unit, StorageUnit.Mebibytes, opts);
7575
}
7676

7777
/**
7878
* Return this storage as a total number of gibibytes.
7979
*/
80-
public toGibibytes(opts: StorageConversionOptions = {}): number {
80+
public toGibibytes(opts: SizeConversionOptions = {}): number {
8181
return convert(this.amount, this.unit, StorageUnit.Gibibytes, opts);
8282
}
8383

8484
/**
8585
* Return this storage as a total number of tebibytes.
8686
*/
87-
public toTebibytes(opts: StorageConversionOptions = {}): number {
87+
public toTebibytes(opts: SizeConversionOptions = {}): number {
8888
return convert(this.amount, this.unit, StorageUnit.Tebibytes, opts);
8989
}
9090

9191
/**
9292
* Return this storage as a total number of pebibytes.
9393
*/
94-
public toPebibytes(opts: StorageConversionOptions = {}): number {
94+
public toPebibytes(opts: SizeConversionOptions = {}): number {
9595
return convert(this.amount, this.unit, StorageUnit.Pebibytes, opts);
9696
}
9797
}
9898

99+
/**
100+
* Rouding behaviour when converting between units of `Size`.
101+
*/
102+
export enum SizeRoundingBehavior {
103+
/** Fail the conversion if the result is not an integer. */
104+
FAIL,
105+
/** If the result is not an integer, round it to the closest integer less than the result */
106+
FLOOR,
107+
/** Don't round. Return even if the result is a fraction. */
108+
NONE,
109+
110+
}
111+
99112
/**
100113
* Options for how to convert time to a different unit.
101114
*/
102-
export interface StorageConversionOptions {
115+
export interface SizeConversionOptions {
103116
/**
104-
* If `true`, conversions into a larger storage units (e.g. `Kibibytes` to `Mebibytes`) will fail if the result is not
105-
* an integer.
106-
* @default true
117+
* How conversions should behave when it encounters a non-integer result
118+
* @default SizeRoundingBehavior.FAIL
107119
*/
108-
readonly integral?: boolean;
120+
readonly rounding?: SizeRoundingBehavior;
109121
}
110122

111123
class StorageUnit {
@@ -125,16 +137,25 @@ class StorageUnit {
125137
}
126138
}
127139

128-
function convert(amount: number, fromUnit: StorageUnit, toUnit: StorageUnit, { integral = true }: StorageConversionOptions) {
140+
function convert(amount: number, fromUnit: StorageUnit, toUnit: StorageUnit, options: SizeConversionOptions = {}) {
141+
const rounding = options.rounding ?? SizeRoundingBehavior.FAIL;
129142
if (fromUnit.inKibiBytes === toUnit.inKibiBytes) { return amount; }
130143
if (Token.isUnresolved(amount)) {
131144
throw new Error(`Unable to perform time unit conversion on un-resolved token ${amount}.`);
132145
}
133146

134147
const multiplier = fromUnit.inKibiBytes / toUnit.inKibiBytes;
135148
const value = amount * multiplier;
136-
if (!Number.isInteger(value) && integral) {
137-
throw new Error(`'${amount} ${fromUnit}' cannot be converted into a whole number of ${toUnit}.`);
149+
switch (rounding) {
150+
case SizeRoundingBehavior.NONE:
151+
return value;
152+
case SizeRoundingBehavior.FLOOR:
153+
return Math.floor(value);
154+
default:
155+
case SizeRoundingBehavior.FAIL:
156+
if (!Number.isInteger(value)) {
157+
throw new Error(`'${amount} ${fromUnit}' cannot be converted into a whole number of ${toUnit}.`);
158+
}
159+
return value;
138160
}
139-
return value;
140161
}

packages/@aws-cdk/core/test/test.size.ts

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Test } from 'nodeunit';
2-
import { Size, Stack, Token } from '../lib';
2+
import { Size, SizeRoundingBehavior, Stack, Token } from '../lib';
33

44
export = {
55
'negative amount'(test: Test) {
@@ -28,7 +28,7 @@ export = {
2828
test.equal(size.toGibibytes(), 4_096);
2929
test.equal(size.toTebibytes(), 4);
3030
test.throws(() => size.toPebibytes(), /'4294967296 kibibytes' cannot be converted into a whole number/);
31-
floatEqual(test, size.toPebibytes({ integral: false }), 4_294_967_296 / (1024 * 1024 * 1024 * 1024));
31+
floatEqual(test, size.toPebibytes({ rounding: SizeRoundingBehavior.NONE }), 4_294_967_296 / (1024 * 1024 * 1024 * 1024));
3232

3333
test.equal(Size.kibibytes(4 * 1024 * 1024).toGibibytes(), 4);
3434

@@ -43,7 +43,7 @@ export = {
4343
test.equal(size.toGibibytes(), 4_096);
4444
test.equal(size.toTebibytes(), 4);
4545
test.throws(() => size.toPebibytes(), /'4194304 mebibytes' cannot be converted into a whole number/);
46-
floatEqual(test, size.toPebibytes({ integral: false }), 4_194_304 / (1024 * 1024 * 1024));
46+
floatEqual(test, size.toPebibytes({ rounding: SizeRoundingBehavior.NONE }), 4_194_304 / (1024 * 1024 * 1024));
4747

4848
test.equal(Size.mebibytes(4 * 1024).toGibibytes(), 4);
4949

@@ -57,9 +57,9 @@ export = {
5757
test.equal(size.toMebibytes(), 5_120);
5858
test.equal(size.toGibibytes(), 5);
5959
test.throws(() => size.toTebibytes(), /'5 gibibytes' cannot be converted into a whole number/);
60-
floatEqual(test, size.toTebibytes({ integral: false }), 5 / 1024);
60+
floatEqual(test, size.toTebibytes({ rounding: SizeRoundingBehavior.NONE }), 5 / 1024);
6161
test.throws(() => size.toPebibytes(), /'5 gibibytes' cannot be converted into a whole number/);
62-
floatEqual(test, size.toPebibytes({ integral: false }), 5 / (1024 * 1024));
62+
floatEqual(test, size.toPebibytes({ rounding: SizeRoundingBehavior.NONE }), 5 / (1024 * 1024));
6363

6464
test.equal(Size.gibibytes(4096).toTebibytes(), 4);
6565

@@ -74,7 +74,7 @@ export = {
7474
test.equal(size.toGibibytes(), 5_120);
7575
test.equal(size.toTebibytes(), 5);
7676
test.throws(() => size.toPebibytes(), /'5 tebibytes' cannot be converted into a whole number/);
77-
floatEqual(test, size.toPebibytes({ integral: false }), 5 / 1024);
77+
floatEqual(test, size.toPebibytes({ rounding: SizeRoundingBehavior.NONE }), 5 / 1024);
7878

7979
test.equal(Size.tebibytes(4096).toPebibytes(), 4);
8080

@@ -92,6 +92,23 @@ export = {
9292

9393
test.done();
9494
},
95+
96+
'rounding behavior'(test: Test) {
97+
const size = Size.mebibytes(5_200);
98+
99+
test.throws(() => size.toGibibytes(), /cannot be converted into a whole number/);
100+
test.throws(() => size.toGibibytes({ rounding: SizeRoundingBehavior.FAIL }), /cannot be converted into a whole number/);
101+
102+
test.equals(size.toGibibytes({ rounding: SizeRoundingBehavior.FLOOR }), 5);
103+
test.equals(size.toTebibytes({ rounding: SizeRoundingBehavior.FLOOR }), 0);
104+
floatEqual(test, size.toKibibytes({ rounding: SizeRoundingBehavior.FLOOR }), 5_324_800);
105+
106+
test.equals(size.toGibibytes({ rounding: SizeRoundingBehavior.NONE}), 5.078125);
107+
test.equals(size.toTebibytes({ rounding: SizeRoundingBehavior.NONE}), 5200 / (1024 * 1024));
108+
test.equals(size.toKibibytes({ rounding: SizeRoundingBehavior.NONE}), 5_324_800);
109+
110+
test.done();
111+
}
95112
};
96113

97114
function floatEqual(test: Test, actual: number, expected: number) {

0 commit comments

Comments
 (0)