Skip to content

Commit

Permalink
feat(context): consolidate uuid generation and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed May 4, 2020
1 parent c760966 commit 5abe25e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
7 changes: 3 additions & 4 deletions packages/context/src/__tests__/unit/binding-key.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import {expect} from '@loopback/testlab';
import {BindingKey} from '../..';
import {UUID_PATTERN} from '../../value-promise';

describe('BindingKey', () => {
describe('create', () => {
Expand Down Expand Up @@ -62,17 +63,15 @@ describe('BindingKey', () => {
describe('generate', () => {
it('generates binding key without namespace', () => {
const key1 = BindingKey.generate().key;
expect(key1).to.match(
/^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
);
expect(key1).to.match(UUID_PATTERN);
const key2 = BindingKey.generate().key;
expect(key1).to.not.eql(key2);
});

it('generates binding key with namespace', () => {
const key1 = BindingKey.generate('services').key;
expect(key1).to.match(
/^services\.[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
new RegExp(`^services\\.${UUID_PATTERN.source}$`, 'i'),
);
const key2 = BindingKey.generate('services').key;
expect(key1).to.not.eql(key2);
Expand Down
7 changes: 3 additions & 4 deletions packages/context/src/__tests__/unit/context.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
isPromiseLike,
Provider,
} from '../..';
import {UUID_PATTERN} from '../../value-promise';

/**
* Create a subclass of context so that we can access parents and registry
Expand Down Expand Up @@ -45,15 +46,13 @@ class TestContext extends Context {
describe('Context constructor', () => {
it('generates uuid name if not provided', () => {
const ctx = new Context();
expect(ctx.name).to.match(
/^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
);
expect(ctx.name).to.match(UUID_PATTERN);
});

it('adds subclass name as the prefix', () => {
const ctx = new TestContext();
expect(ctx.name).to.match(
/^TestContext-[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
new RegExp(`^TestContext-${UUID_PATTERN.source}$`, 'i'),
);
});

Expand Down
13 changes: 13 additions & 0 deletions packages/context/src/__tests__/unit/interceptor.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
Provider,
} from '../..';
import {registerInterceptor} from '../../interceptor';
import {UUID_PATTERN} from '../../value-promise';

describe('mergeInterceptors', () => {
it('removes duplicate entries from the spec', () => {
Expand Down Expand Up @@ -259,6 +260,18 @@ describe('globalInterceptors', () => {
expect(keys).to.eql(['globalInterceptors.logInterceptor']);
});

it('infers binding key from the interceptor function', () => {
const binding = registerInterceptor(ctx, logInterceptor);
expect(binding.key).to.eql('interceptors.logInterceptor');
});

it('generates binding key for the interceptor function', () => {
const binding = registerInterceptor(ctx, () => {});
expect(binding.key).to.match(
new RegExp(`interceptors.${UUID_PATTERN.source}`, 'i'),
);
});

class MyController {
greet(name: string) {
return `Hello, ${name}`;
Expand Down
7 changes: 4 additions & 3 deletions packages/context/src/binding-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {v4} from 'uuid';
import {uuid} from './value-promise';

export type BindingAddress<T = unknown> = string | BindingKey<T>;

export class BindingKey<ValueType> {
Expand Down Expand Up @@ -127,7 +128,7 @@ export class BindingKey<ValueType> {
*/
static generate<T>(namespace = ''): BindingKey<T> {
const prefix = namespace ? `${namespace}.` : '';
const uuid = v4();
return BindingKey.create(`${prefix}${uuid}`);
const name = uuid();
return BindingKey.create(`${prefix}${name}`);
}
}
4 changes: 2 additions & 2 deletions packages/context/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import debugFactory, {Debugger} from 'debug';
import {EventEmitter} from 'events';
import {v4 as uuidv4} from 'uuid';
import {Binding, BindingInspectOptions, BindingTag} from './binding';
import {
ConfigurationResolver,
Expand Down Expand Up @@ -37,6 +36,7 @@ import {
Constructor,
getDeepProperty,
isPromiseLike,
uuid,
ValueOrPromise,
} from './value-promise';

Expand Down Expand Up @@ -151,7 +151,7 @@ export class Context extends EventEmitter {
}

private generateName() {
const id = uuidv4();
const id = uuid();
if (this.constructor === Context) return id;
return `${this.constructor.name}-${id}`;
}
Expand Down
13 changes: 13 additions & 0 deletions packages/context/src/value-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* utility methods to handle values and/or promises.
*/

import {v4 as uuidv4} from 'uuid';
/**
* A class constructor accepting arbitrary arguments.
*/
Expand Down Expand Up @@ -270,3 +271,15 @@ export function transformValueOrPromise<T, V>(
return transformer(valueOrPromise);
}
}

/**
* A utility to generate uuid v4
*/
export function uuid() {
return uuidv4();
}

/**
* A regular expression for testing uuid v4 PATTERN
*/
export const UUID_PATTERN = /[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/i;

0 comments on commit 5abe25e

Please sign in to comment.