Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(service-spec-types): index for scrutinizable resources #669

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/@aws-cdk/service-spec-types/src/types/database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { promises as fs } from 'fs';
import { gunzipSync } from 'zlib';
import { Database, entityCollection, fieldIndex, stringCmp } from '@cdklabs/tskb';
import { Database, entityCollection, fieldIndex, fieldIndexWithDefault, stringCmp } from '@cdklabs/tskb';
import { IsAugmentedResource, ResourceAugmentation } from './augmentations';
import {
DimensionSet,
Expand All @@ -21,13 +21,15 @@ import {
RegionHasResource,
RegionHasService,
UsesType,
ResourceScrutinyType,
} from './resource';

export function emptyDatabase() {
return new Database(
{
resource: entityCollection<Resource>().index({
cloudFormationType: fieldIndex('cloudFormationType', stringCmp),
scrutinizable: fieldIndexWithDefault('scrutinizable', stringCmp, ResourceScrutinyType.None),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ended up doing this instead of using an index that can understand undefined because querying db.lookup('resource', 'scrutinizable', 'equals', ResourceScrutinyType.None) is nicer.

}),
region: entityCollection<Region>().index({
name: fieldIndex('name', stringCmp),
Expand Down
8 changes: 8 additions & 0 deletions packages/@cdklabs/tskb/src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ export function fieldIndex<A extends Entity, P extends keyof A>(
return calculatedIndex((x) => x[propName], comparator);
}

export function fieldIndexWithDefault<A extends Entity, P extends keyof A>(
propName: P,
comparator: sortedMap.Comparator<NonNullable<A[P]>>,
defaultValue: NonNullable<A[P]>,
): EntityIndex<A, A[P]> {
return calculatedIndex((x) => x[propName] ?? defaultValue!, comparator);
}

/**
* An index that is calculated based on a function applied to an entity
*/
Expand Down