Skip to content

Commit

Permalink
fix: set value error if type no exist (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
JerrysShan authored Aug 15, 2022
1 parent db330fa commit 919eba3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
2 changes: 0 additions & 2 deletions src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export const CLASS_PROPERTY = Symbol.for('injection:property');

export const CLASS_CONSTRUCTOR_ARGS = Symbol.for('injection:constructor_args');

export const CLASS_ASYNC_INIT_METHOD = Symbol.for('injection:async_init_method');

export const EXECUTION_CONTEXT_KEY = 'ctx';

export const CLASS_TAG = Symbol.for('injection:class_tag');
Expand Down
3 changes: 0 additions & 3 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
CLASS_CONSTRUCTOR,
CLASS_PROPERTY,
CLASS_CONSTRUCTOR_ARGS,
CLASS_ASYNC_INIT_METHOD,
CLASS_TAG,
INJECT_HANDLER_ARGS,
INJECT_HANDLER_PROPS,
Expand Down Expand Up @@ -85,7 +84,6 @@ export default class Container implements ContainerType {
if (type) {
const args = getMetadata(CLASS_CONSTRUCTOR_ARGS, type) as ReflectMetadataType[];
const props = recursiveGetMetadata(CLASS_PROPERTY, type) as ReflectMetadataType[];
const initMethodMd = getMetadata(CLASS_ASYNC_INIT_METHOD, type) as ReflectMetadataType;
const handlerArgs = getMetadata(INJECT_HANDLER_ARGS, type) as ReflectMetadataType[];
const handlerProps = recursiveGetMetadata(
INJECT_HANDLER_PROPS,
Expand All @@ -94,7 +92,6 @@ export default class Container implements ContainerType {

md.constructorArgs = (args ?? []).concat(handlerArgs ?? []);
md.properties = (props ?? []).concat(handlerProps ?? []);
md.initMethod = initMethodMd?.propertyName ?? 'init';
/**
* compatible with inject type identifier when identifier is string
*/
Expand Down
11 changes: 9 additions & 2 deletions src/execution_container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EXECUTION_CONTEXT_KEY } from './constant';
import Container from './container';
import { ContainerType, HandlerFunction, Identifier, InjectableMetadata, ScopeEnum } from './types';
import { NotFoundError } from './error';
import { isUndefined } from './util';

export default class ExecutionContainer extends Container {
private parent: ContainerType;
Expand Down Expand Up @@ -35,13 +36,19 @@ export default class ExecutionContainer extends Container {
}

private setValue(md: InjectableMetadata, value: any) {
if (md.id !== md.type) {
if (!isUndefined(md.value)) {
return;
}

if (md.type && md.id !== md.type) {
this.set({
id: md.type!,
...md,
id: md.type,
value,
});
}
this.set({
...md,
id: md.id,
value,
});
Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ export interface InjectableDefinition<T = unknown> {
export interface InjectableMetadata<T = any> extends InjectableDefinition<T> {
properties?: ReflectMetadataType[];
constructorArgs?: ReflectMetadataType[];
initMethod?: string | symbol;
}

export interface ReflectMetadataType {
id: Identifier;
scope?: ScopeEnum;
index?: number;
lazy?: boolean;
defaultValue?: boolean;
noThrow?: boolean;
propertyName?: string | symbol;
Expand Down
7 changes: 7 additions & 0 deletions test/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ describe('container', () => {
execContainer.get('config.emails');
}).toThrowError('identifier was not found in the container');
});

it('should not set value if metadata value exist', () => {
const execContainer = new ExecutionContainer(ctx, container);
execContainer.set({ id: 'hasValue', factory: () => 'hello', scope: ScopeEnum.EXECUTION });
expect(execContainer.get('hasValue')).toBe('hello');
expect(execContainer.get('hasValue')).toBe('hello');
});
});
});

Expand Down

0 comments on commit 919eba3

Please sign in to comment.