-
Notifications
You must be signed in to change notification settings - Fork 32
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
Feature: IoC container #9
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ad2a5ee
feat(core): add inversify and moke container for core packages
oscar60310 9145f4a
fix(core): update test cases of core package to used container
oscar60310 5583574
fix: fix path alias and add container for build package
oscar60310 def280c
refactor: unify option interface
oscar60310 1c5e76f
feat: add class validator for all options
oscar60310 29f4a2a
fix(build): update schema name properties and add some comments
oscar60310 d1dd554
fix(build): rename the option property from schemaPath to folderPath
oscar60310 3e11bf9
fix(core): rane the option property from templatePath to folderPath
oscar60310 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Container as InversifyContainer } from 'inversify'; | ||
import { Container as CoreContainer } from '@vulcan/core'; | ||
import { IBuildOptions } from '@vulcan/build/models'; | ||
import { schemaParserModule } from './modules'; | ||
|
||
export class Container { | ||
private inversifyContainer = new InversifyContainer(); | ||
|
||
public get<T>(type: symbol) { | ||
return this.inversifyContainer.get<T>(type); | ||
} | ||
|
||
public load(options: IBuildOptions) { | ||
const coreContainer = new CoreContainer(); | ||
coreContainer.load(options); | ||
this.inversifyContainer.parent = coreContainer.getInversifyContainer(); | ||
this.inversifyContainer.load(schemaParserModule(options.schemaParser)); | ||
} | ||
|
||
public unload() { | ||
this.inversifyContainer.parent?.unbindAll(); | ||
this.inversifyContainer.unbindAll(); | ||
} | ||
|
||
public getInversifyContainer() { | ||
return this.inversifyContainer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import 'reflect-metadata'; | ||
export * from './types'; | ||
export * from './container'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './schemaParser'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ISchemaParserOptions, SchemaReaderType } from '@vulcan/build/models'; | ||
import { | ||
FileSchemaReader, | ||
SchemaParser, | ||
SchemaReader, | ||
} from '@vulcan/build/schema-parser'; | ||
import { ContainerModule, interfaces } from 'inversify'; | ||
import { SchemaParserOptions } from '../../options/schemaParser'; | ||
import { TYPES } from '../types'; | ||
|
||
export const schemaParserModule = (options: ISchemaParserOptions) => | ||
new ContainerModule((bind) => { | ||
// Options | ||
bind<ISchemaParserOptions>(TYPES.SchemaParserInputOptions).toConstantValue( | ||
options | ||
); | ||
bind<SchemaParserOptions>(TYPES.SchemaParserOptions) | ||
.to(SchemaParserOptions) | ||
.inSingletonScope(); | ||
|
||
// Schema reader | ||
bind<SchemaReader>(TYPES.SchemaReader) | ||
.to(FileSchemaReader) | ||
.inSingletonScope() | ||
.whenTargetNamed(SchemaReaderType.LocalFile); | ||
|
||
bind<interfaces.AutoNamedFactory<SchemaReader>>( | ||
TYPES.Factory_SchemaReader | ||
).toAutoNamedFactory<SchemaReader>(TYPES.SchemaReader); | ||
|
||
// Schema parser | ||
bind<SchemaParser>(TYPES.SchemaParser).to(SchemaParser).inSingletonScope(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const TYPES = { | ||
SchemaParserInputOptions: Symbol.for('SchemaParserInputOptions'), | ||
SchemaParserOptions: Symbol.for('SchemaParserOptions'), | ||
SchemaReader: Symbol.for('SchemaReader'), | ||
Factory_SchemaReader: Symbol.for('Factory_SchemaReader'), | ||
SchemaParser: Symbol.for('SchemaParser'), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export * from './lib/schema-parser'; | ||
export * from './lib/pipelline'; | ||
export * from './lib/vulcanBuilder'; | ||
export * from './containers'; | ||
export * from './options'; | ||
export * from './models'; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
3 changes: 1 addition & 2 deletions
3
packages/build/src/lib/schema-parser/middleware/checkValidator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { IBuildOptions } from '@vulcan/build/models'; | ||
import { Container, TYPES } from '@vulcan/build/containers'; | ||
import { SchemaParser } from '@vulcan/build/schema-parser'; | ||
import { | ||
TemplateEngine, | ||
TYPES as CORE_TYPES, | ||
VulcanArtifactBuilder, | ||
} from '@vulcan/core'; | ||
|
||
export class VulcanBuilder { | ||
public async build(options: IBuildOptions) { | ||
const container = new Container(); | ||
container.load(options); | ||
const schemaParser = container.get<SchemaParser>(TYPES.SchemaParser); | ||
const templateEngine = container.get<TemplateEngine>( | ||
CORE_TYPES.TemplateEngine | ||
); | ||
const artifactBuilder = container.get<VulcanArtifactBuilder>( | ||
CORE_TYPES.ArtifactBuilder | ||
); | ||
|
||
const { metadata, templates } = await templateEngine.compile(); | ||
const { schemas } = await schemaParser.parse({ metadata }); | ||
|
||
await artifactBuilder.build({ schemas, templates }); | ||
|
||
container.unload(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ICoreOptions } from '@vulcan/core'; | ||
import { ISchemaParserOptions } from './schemaParserOptions'; | ||
|
||
export interface IBuildOptions extends ICoreOptions { | ||
schemaParser: ISchemaParserOptions; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './buildOptions'; | ||
export * from './schemaParserOptions'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface ISchemaParserOptions { | ||
reader: SchemaReaderType; | ||
folderPath: string; | ||
} | ||
|
||
export enum SchemaReaderType { | ||
LocalFile = 'LocalFile', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './schemaParser'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { injectable, inject, optional } from 'inversify'; | ||
import { TYPES } from '@vulcan/build/containers'; | ||
import { ISchemaParserOptions, SchemaReaderType } from '@vulcan/build/models'; | ||
import { IsOptional, IsString, validateSync } from 'class-validator'; | ||
|
||
@injectable() | ||
export class SchemaParserOptions implements ISchemaParserOptions { | ||
@IsString() | ||
public readonly reader: SchemaReaderType = SchemaReaderType.LocalFile; | ||
|
||
@IsString() | ||
@IsOptional() | ||
public readonly folderPath!: string; | ||
|
||
constructor( | ||
@inject(TYPES.SchemaParserInputOptions) | ||
@optional() | ||
options: Partial<ISchemaParserOptions> = {} | ||
) { | ||
Object.assign(this, options); | ||
const errors = validateSync(this); | ||
if (errors.length > 0) { | ||
throw new Error('Invalid schema parser options: ' + errors.join(', ')); | ||
} | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about adding the comment to make the members know the source name is the schema file name or rename it to
schemaFileName
orfileName
?Because
sourceName
may not easy to know where the source from, even we could know thetemplateSource
will be assigned the source name if not foundtemplateSource
ingetTemapleteSource
middleware, we still do not know where the default source name from.And also, even though we found the
sourceName
is fromname
field in theSchemaData
, thename
is a general word ( maybe we could also rename thename
in theSchemaData
or just also add a comment ) until we see the code ofreadSchema
in theFileSchemaReader
, however, it may spend some time to trace.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed these two properties to "sourceName" and added some comments about them.
The sourceName here indicated the identifier of the schema, it might be file path (File provider), URL (s3 provider), UUID (database provider) ...etc. in the future, so I didn't rename it the schemaFileName or fileName.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for telling me the
sourceName
purpose in the future, thanks, no problem and thanks for you comment added 👍