97 lines
3.2 KiB
Markdown
97 lines
3.2 KiB
Markdown
# Converter Module
|
|
|
|
The Module is used to allow the convertion between different data formats.
|
|
Each converter is defined in a separate file (see `/src` - Folder) and is automatically integrated int the Converter-Container.
|
|
|
|
Therefore each converter implements the following Interface (see `/type/interface.ts`)
|
|
|
|
```typescript
|
|
export interface IConverter<Input, Return, Config> {
|
|
/** Name of the Converter */
|
|
name: AvailableFromConvertes | AvailableToConvertes;
|
|
/** Function to convert the Input to the Output */
|
|
convert (data: Input, path?: string, timeStamp?: number): Return;
|
|
/** To allow a configuration the converter the register-method is provided */
|
|
register(topic: string, config: Config): void;
|
|
}
|
|
```
|
|
|
|
# Example of Usage
|
|
|
|
An example of the usage is given in `/test/test-converters.ts`
|
|
|
|
Rules:
|
|
- Try using the Container
|
|
- use identifiers to register different converters
|
|
|
|
```typescript
|
|
/** Import the Assembly*/
|
|
import * as CONVERTERS from '../path/to/git/assembly/manual-assembly';
|
|
|
|
/** Import an INVERSIFY Container */
|
|
import { Container } from 'inversify';
|
|
|
|
/** Create a Container and include the defined Module */
|
|
const _container = new Container();
|
|
_container.load(CONVERTERS.CONTAINERMODULE);
|
|
|
|
/** Create an Instance */
|
|
const _converter = _container.get<CONVERTERS.ConverterContainer>(CONVERTERS.TYPES.Converter);
|
|
|
|
/** Register an Identifier */
|
|
_converter.register('to-grpc', 'identifier', {
|
|
/** Setting the X-Position */
|
|
fileName: 'Proto-Repository\\protos\\std_messages.proto',
|
|
packageName: 'std_package',
|
|
messageName: 'Orientation_rpy'
|
|
});
|
|
|
|
/** Convert Stuff */
|
|
const _buf = _converter.autoConvert({roll: 1, pitch: 0 , yaw: 0}, 'identifier');
|
|
|
|
```
|
|
|
|
|
|
# Extending the Converter
|
|
|
|
1. To extend the Converter adapt the following vars of the `/type/interfaces.ts`
|
|
|
|
- **AvailableToConvertes**
|
|
```typescript
|
|
export type AvailableToConvertes = 'to-grpc' | 'to-csv' | 'to-da3vid' | 'to-json' | 'to-html' | 'to-string' /** | 'add-your-name-here' */;
|
|
```
|
|
- **AvailableFromConvertes**
|
|
```typescript
|
|
export type AvailableFromConvertes = 'from-grpc' | 'from-json' /** | 'or-here' */;
|
|
```
|
|
- **AvailableConfigType**
|
|
```typescript
|
|
export type AvailableConfigType = IGrpcConfiguration | IDa3vidConfiguration | IHtmlConfiguration /** | IYourConfigInterfaceHere */ ;
|
|
```
|
|
2. Create the corresponding Class-File in `/src`
|
|
1. Implement the `IConverter<Input, Return, Config>`-*Interface*
|
|
2. Make the calss *injectable*
|
|
|
|
```typescript
|
|
@injectable()
|
|
export class YourConverter implements IConverter<any, string, number> {
|
|
...
|
|
}
|
|
```
|
|
1. Adapt the manual Assembly and add your converter ( see `/assembly/manual-assembly.ts`)
|
|
1. Import your defined Interfaces and Classes
|
|
2. Add a **Binding** for your class (use *inSingletonScope*)
|
|
```typescript
|
|
/** Binding for To String Converter */
|
|
bind<IConverter<any, string, any>>(TYPES.ToStringConverter).to(ToStringConverter).inSingletonScope();
|
|
```
|
|
3. Export your defined Converters as Type.
|
|
|
|
|
|
These steps are required to automatically include the converter into the Container.
|
|
|
|
|
|
|
|
|
|
|