Skip to content

Commit ed26731

Browse files
authored
feat(core): add size.isUnresolved (#19569)
There is a `Duration.isUnresolved()`, and I'm adding a similar function for `Size`. See discussion here for why this is necessary: https://github.com/aws/aws-cdk/pull/19550/files#r835542214 The td:dr; is that `Token.isUnresolved()` does not check for the resolvability of object properties, so something like: ```ts Token.isUnresolved(Size.mebibytes(Lazy.number({ produce: () => 10 })); ``` returns (to me, unexpectedly,) false. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [ ] Did you use `cdk-integ` to deploy the infrastructure and generate the snapshot (i.e. `cdk-integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 18a6b0c commit ed26731

File tree

3 files changed

+13
-41
lines changed

3 files changed

+13
-41
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ export class Size {
144144
public toPebibytes(opts: SizeConversionOptions = {}): number {
145145
return convert(this.amount, this.unit, StorageUnit.Pebibytes, opts);
146146
}
147+
148+
/**
149+
* Checks if size is a token or a resolvable object
150+
*/
151+
public isUnresolved() {
152+
return Token.isUnresolved(this.amount);
153+
}
147154
}
148155

149156
/**

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

-25
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ describe('duration', () => {
1818
() => stack.resolve(lazyDuration.toMinutes())).toThrow(
1919
/Unable to perform time unit conversion on un-resolved token/,
2020
);
21-
22-
2321
});
2422

2523
test('Duration in seconds', () => {
@@ -31,8 +29,6 @@ describe('duration', () => {
3129
floatEqual(duration.toDays({ integral: false }), 300 / 86_400);
3230

3331
expect(Duration.seconds(60 * 60 * 24).toDays()).toEqual(1);
34-
35-
3632
});
3733

3834
test('Duration in minutes', () => {
@@ -44,8 +40,6 @@ describe('duration', () => {
4440
floatEqual(duration.toDays({ integral: false }), 300 / 86_400);
4541

4642
expect(Duration.minutes(60 * 24).toDays()).toEqual(1);
47-
48-
4943
});
5044

5145
test('Duration in hours', () => {
@@ -57,16 +51,12 @@ describe('duration', () => {
5751
floatEqual(duration.toDays({ integral: false }), 5 / 24);
5852

5953
expect(Duration.hours(24).toDays()).toEqual(1);
60-
61-
6254
});
6355

6456
test('seconds to milliseconds', () => {
6557
const duration = Duration.seconds(5);
6658

6759
expect(duration.toMilliseconds()).toEqual(5_000);
68-
69-
7060
});
7161

7262
test('Duration in days', () => {
@@ -75,8 +65,6 @@ describe('duration', () => {
7565
expect(duration.toSeconds()).toEqual(86_400);
7666
expect(duration.toMinutes()).toEqual(1_440);
7767
expect(duration.toDays()).toEqual(1);
78-
79-
8068
});
8169

8270
testDeprecated('toISOString', () => {
@@ -93,8 +81,6 @@ describe('duration', () => {
9381
expect(Duration.days(5).toISOString()).toEqual('P5D');
9482

9583
expect(Duration.seconds(1 + 60 * (1 + 60 * (1 + 24))).toISOString()).toEqual('P1DT1H1M1S');
96-
97-
9884
});
9985

10086
test('toIsoString', () => {
@@ -112,8 +98,6 @@ describe('duration', () => {
11298

11399
expect(Duration.seconds(65).toIsoString()).toEqual('PT1M5S');
114100
expect(Duration.seconds(1 + 60 * (1 + 60 * (1 + 24))).toIsoString()).toEqual('P1DT1H1M1S');
115-
116-
117101
});
118102

119103
test('parse', () => {
@@ -128,8 +112,6 @@ describe('duration', () => {
128112
expect(Duration.parse('P5D').toSeconds()).toEqual(432_000);
129113

130114
expect(Duration.parse('P1DT1H1M1S').toSeconds()).toEqual(1 + 60 * (1 + 60 * (1 + 24)));
131-
132-
133115
});
134116

135117
test('reject illegal parses', () => {
@@ -141,8 +123,6 @@ describe('duration', () => {
141123
expect(() => {
142124
Duration.parse('P5S');
143125
}).toThrow(err);
144-
145-
146126
});
147127

148128
test('to human string', () => {
@@ -165,8 +145,6 @@ describe('duration', () => {
165145
expect(Duration.millis(3666).toHumanString()).toEqual('3 seconds 666 millis');
166146

167147
expect(Duration.millis(3.6).toHumanString()).toEqual('3.6 millis');
168-
169-
170148
});
171149

172150
test('add two durations', () => {
@@ -188,7 +166,6 @@ describe('duration', () => {
188166
expect(Duration.millis(1).unitLabel()).toEqual('millis');
189167
expect(Duration.hours(1000).unitLabel()).toEqual('hours');
190168
expect(Duration.days(2).unitLabel()).toEqual('days');
191-
192169
});
193170

194171
test('format number token to number', () => {
@@ -197,14 +174,12 @@ describe('duration', () => {
197174
expect(stack.resolve(lazyDuration.formatTokenToNumber())).toEqual('10 minutes');
198175
expect(Duration.hours(10).formatTokenToNumber()).toEqual('10 hours');
199176
expect(Duration.days(5).formatTokenToNumber()).toEqual('5 days');
200-
201177
});
202178

203179
test('duration is unresolved', () => {
204180
const lazyDuration = Duration.minutes(Lazy.number({ produce: () => 10 }));
205181
expect(lazyDuration.isUnresolved()).toEqual(true);
206182
expect(Duration.hours(10).isUnresolved()).toEqual(false);
207-
208183
});
209184
});
210185

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

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { Size, SizeRoundingBehavior, Stack, Token } from '../lib';
1+
import { Size, SizeRoundingBehavior, Stack, Token, Lazy } from '../lib';
22

33
describe('size', () => {
44
test('negative amount', () => {
55
expect(() => Size.kibibytes(-1)).toThrow(/negative/);
6-
7-
86
});
97

108
test('unresolved amount', () => {
@@ -15,8 +13,6 @@ describe('size', () => {
1513
() => stack.resolve(lazySize.toMebibytes())).toThrow(
1614
/Unable to perform time unit conversion on un-resolved token/,
1715
);
18-
19-
2016
});
2117

2218
test('Size in kibibytes', () => {
@@ -30,8 +26,6 @@ describe('size', () => {
3026
floatEqual(size.toPebibytes({ rounding: SizeRoundingBehavior.NONE }), 4_294_967_296 / (1024 * 1024 * 1024 * 1024));
3127

3228
expect(Size.kibibytes(4 * 1024 * 1024).toGibibytes()).toEqual(4);
33-
34-
3529
});
3630

3731
test('Size in mebibytes', () => {
@@ -45,8 +39,6 @@ describe('size', () => {
4539
floatEqual(size.toPebibytes({ rounding: SizeRoundingBehavior.NONE }), 4_194_304 / (1024 * 1024 * 1024));
4640

4741
expect(Size.mebibytes(4 * 1024).toGibibytes()).toEqual(4);
48-
49-
5042
});
5143

5244
test('Size in gibibyte', () => {
@@ -61,8 +53,6 @@ describe('size', () => {
6153
floatEqual(size.toPebibytes({ rounding: SizeRoundingBehavior.NONE }), 5 / (1024 * 1024));
6254

6355
expect(Size.gibibytes(4096).toTebibytes()).toEqual(4);
64-
65-
6656
});
6757

6858
test('Size in tebibyte', () => {
@@ -76,8 +66,6 @@ describe('size', () => {
7666
floatEqual(size.toPebibytes({ rounding: SizeRoundingBehavior.NONE }), 5 / 1024);
7767

7868
expect(Size.tebibytes(4096).toPebibytes()).toEqual(4);
79-
80-
8169
});
8270

8371
test('Size in pebibytes', () => {
@@ -88,8 +76,6 @@ describe('size', () => {
8876
expect(size.toGibibytes()).toEqual(5_242_880);
8977
expect(size.toTebibytes()).toEqual(5_120);
9078
expect(size.toPebibytes()).toEqual(5);
91-
92-
9379
});
9480

9581
test('rounding behavior', () => {
@@ -105,8 +91,12 @@ describe('size', () => {
10591
expect(size.toGibibytes({ rounding: SizeRoundingBehavior.NONE })).toEqual(5.078125);
10692
expect(size.toTebibytes({ rounding: SizeRoundingBehavior.NONE })).toEqual(5200 / (1024 * 1024));
10793
expect(size.toKibibytes({ rounding: SizeRoundingBehavior.NONE })).toEqual(5_324_800);
94+
});
10895

109-
96+
test('size is unresolved', () => {
97+
const lazySize = Size.pebibytes(Lazy.number({ produce: () => 10 }));
98+
expect(lazySize.isUnresolved()).toEqual(true);
99+
expect(Size.mebibytes(10).isUnresolved()).toEqual(false);
110100
});
111101
});
112102

0 commit comments

Comments
 (0)