import * as path from 'path' ; import * as utils from './utils' ; import { ObjectContext } from './object-context' ; export interface NamedObjectDef { name : string ; description ? : string ; private ? : boolean ; override ? : boolean ; value : any ; dependencies ? : ObjectContextDependency ; } export interface NamedObject { def : NamedObjectDef ; value : any ; readonly scope : string ; } export interface NamedObjectCollection { get ( name ) : NamedObject ; forEach ( callback ) : void ; } export class NamedObjectRegistry implements NamedObjectCollection { private _nameToObjectMap : Map < string , NamedObject > = new Map < string , NamedObject > ( ) ; public get ( name ) { return this . _nameToObjectMap . get ( name ) ; } public has ( name ) { return this . _nameToObjectMap . has ( name ) ; } public forEach ( callback ) { this . _nameToObjectMap . forEach ( object => { callback ( object ) ; } ) ; } public insert ( object ) { this . _nameToObjectMap . set ( object . def . name , object ) ; } public static fromDefinition ( scope , namedObjectDefCollection , context ) { let registry = new NamedObjectRegistry ( ) ; if ( namedObjectDefCollection != null ) { for ( let def of namedObjectDefCollection ) { let value = context . create ( def . value ) ; registry . insert ( { def : def , value : value , scope : scope } ) ; } } return registry ; } } export class ObjectContextDependency { private _dependentTypesNames : Set < string > = new Set < string > ( ) ; private _dependentObjectNames : Set < string > = new Set < string > ( ) ; private _dependentProtocolNames : Set < string > = new Set < string > ( ) ; public setTypeDependency ( typeName ) { this . _dependentTypesNames . add ( typeName ) ; } public setProtocolDependency ( protocolName ) { this . _dependentProtocolNames . add ( protocolName ) ; } public setObjectDependency ( objectName ) { this . _dependentObjectNames . add ( objectName ) ; } public get typeDependencies ( ) : Set < string > { return this . _dependentTypesNames ; } public get protocolDependencies ( ) : Set < string > { return this . _dependentProtocolNames ; } public get objectDependencies ( ) : Set < string > { return this . _dependentObjectNames ; } } ; const SCHEMA_DIR = path . resolve ( __dirname , '../schema' ) ; export class NamedObjectConfig { static readonly NAMED_OBJECT_CONFIG_SCHEMA : utils . JsonSchema = new utils . JsonSchema ( path . resolve ( SCHEMA_DIR , "named-object-config.schema.json" ) ) ; private static _transform : utils . Transform = new utils . SetDefaultValue ( { 'override' : false , 'private' : false } ) ; public static fromConfigObject ( jsValue , validateSchema = true ) { if ( validateSchema ) { utils . ensureSchema ( jsValue , this . NAMED_OBJECT_CONFIG_SCHEMA ) ; } jsValue . forEach ( obj => { this . _transform . apply ( obj ) ; } ) ; return jsValue ; } public static fromConfig ( namedObjectConfigFile ) { return utils . appendMessageOnException ( "Error found in named object definition file '" + namedObjectConfigFile + "'." , ( ) => { return this . fromConfigObject ( utils . readConfig ( namedObjectConfigFile ) ) ; } ) ; } }