Skip to content

Commit

Permalink
feat(core): add size.isUnresolved (#19569)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
kaizencc authored Mar 31, 2022
1 parent 18a6b0c commit ed26731
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 41 deletions.
7 changes: 7 additions & 0 deletions packages/@aws-cdk/core/lib/size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ export class Size {
public toPebibytes(opts: SizeConversionOptions = {}): number {
return convert(this.amount, this.unit, StorageUnit.Pebibytes, opts);
}

/**
* Checks if size is a token or a resolvable object
*/
public isUnresolved() {
return Token.isUnresolved(this.amount);
}
}

/**
Expand Down
25 changes: 0 additions & 25 deletions packages/@aws-cdk/core/test/duration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ describe('duration', () => {
() => stack.resolve(lazyDuration.toMinutes())).toThrow(
/Unable to perform time unit conversion on un-resolved token/,
);


});

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

expect(Duration.seconds(60 * 60 * 24).toDays()).toEqual(1);


});

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

expect(Duration.minutes(60 * 24).toDays()).toEqual(1);


});

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

expect(Duration.hours(24).toDays()).toEqual(1);


});

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

expect(duration.toMilliseconds()).toEqual(5_000);


});

test('Duration in days', () => {
Expand All @@ -75,8 +65,6 @@ describe('duration', () => {
expect(duration.toSeconds()).toEqual(86_400);
expect(duration.toMinutes()).toEqual(1_440);
expect(duration.toDays()).toEqual(1);


});

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

expect(Duration.seconds(1 + 60 * (1 + 60 * (1 + 24))).toISOString()).toEqual('P1DT1H1M1S');


});

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

expect(Duration.seconds(65).toIsoString()).toEqual('PT1M5S');
expect(Duration.seconds(1 + 60 * (1 + 60 * (1 + 24))).toIsoString()).toEqual('P1DT1H1M1S');


});

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

expect(Duration.parse('P1DT1H1M1S').toSeconds()).toEqual(1 + 60 * (1 + 60 * (1 + 24)));


});

test('reject illegal parses', () => {
Expand All @@ -141,8 +123,6 @@ describe('duration', () => {
expect(() => {
Duration.parse('P5S');
}).toThrow(err);


});

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

expect(Duration.millis(3.6).toHumanString()).toEqual('3.6 millis');


});

test('add two durations', () => {
Expand All @@ -188,7 +166,6 @@ describe('duration', () => {
expect(Duration.millis(1).unitLabel()).toEqual('millis');
expect(Duration.hours(1000).unitLabel()).toEqual('hours');
expect(Duration.days(2).unitLabel()).toEqual('days');

});

test('format number token to number', () => {
Expand All @@ -197,14 +174,12 @@ describe('duration', () => {
expect(stack.resolve(lazyDuration.formatTokenToNumber())).toEqual('10 minutes');
expect(Duration.hours(10).formatTokenToNumber()).toEqual('10 hours');
expect(Duration.days(5).formatTokenToNumber()).toEqual('5 days');

});

test('duration is unresolved', () => {
const lazyDuration = Duration.minutes(Lazy.number({ produce: () => 10 }));
expect(lazyDuration.isUnresolved()).toEqual(true);
expect(Duration.hours(10).isUnresolved()).toEqual(false);

});
});

Expand Down
22 changes: 6 additions & 16 deletions packages/@aws-cdk/core/test/size.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Size, SizeRoundingBehavior, Stack, Token } from '../lib';
import { Size, SizeRoundingBehavior, Stack, Token, Lazy } from '../lib';

describe('size', () => {
test('negative amount', () => {
expect(() => Size.kibibytes(-1)).toThrow(/negative/);


});

test('unresolved amount', () => {
Expand All @@ -15,8 +13,6 @@ describe('size', () => {
() => stack.resolve(lazySize.toMebibytes())).toThrow(
/Unable to perform time unit conversion on un-resolved token/,
);


});

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

expect(Size.kibibytes(4 * 1024 * 1024).toGibibytes()).toEqual(4);


});

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

expect(Size.mebibytes(4 * 1024).toGibibytes()).toEqual(4);


});

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

expect(Size.gibibytes(4096).toTebibytes()).toEqual(4);


});

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

expect(Size.tebibytes(4096).toPebibytes()).toEqual(4);


});

test('Size in pebibytes', () => {
Expand All @@ -88,8 +76,6 @@ describe('size', () => {
expect(size.toGibibytes()).toEqual(5_242_880);
expect(size.toTebibytes()).toEqual(5_120);
expect(size.toPebibytes()).toEqual(5);


});

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


test('size is unresolved', () => {
const lazySize = Size.pebibytes(Lazy.number({ produce: () => 10 }));
expect(lazySize.isUnresolved()).toEqual(true);
expect(Size.mebibytes(10).isUnresolved()).toEqual(false);
});
});

Expand Down

0 comments on commit ed26731

Please sign in to comment.