Skip to content

Commit

Permalink
Implement VariableUsageHandler and small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Vinícius Rubido Sauda committed May 25, 2019
1 parent 4234135 commit 481ad0a
Show file tree
Hide file tree
Showing 23 changed files with 228 additions and 110 deletions.
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@ Will convert a export GTM container file to a PlantUML file.

## Usage

```
### Install
```shell
npm install --global gtmplant
gtmplant --input test/data/cookiebotGDPRConsentRecipe.json --output test/data/cookiebotGDPRConsentRecipe.puml
```

### Generate image file (svg or png)
> Internet connection is required
```shell
tplant --input test/data/cookiebotGDPRConsentRecipe.json --output test/data/cookiebotGDPRConsentRecipe.svg
tplant --input test/data/cookiebotGDPRConsentRecipe.json --output test/data/cookiebotGDPRConsentRecipe.png
```

### Generate puml file
```shell
tplant --input test/data/cookiebotGDPRConsentRecipe.json --output test/data/cookiebotGDPRConsentRecipe.puml
```

## Options
Expand Down Expand Up @@ -38,9 +50,6 @@ Will output all Tags, Triggers, Variables and the usage between them.
### -R, --triggers
Convert triggers

### -R, --triggers
Convert triggers

### -V, --variables
Convert variables

Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ module.exports = {
// notifyMode: "failure-change",

// A preset that is used as a base for Jest's configuration
// preset: null,
preset: "ts-jest",

// Run tests from one or more projects
// projects: null,
Expand Down
48 changes: 18 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gtmplant",
"version": "0.1.0",
"version": "0.1.1",
"description": "GTM to PlantUML",
"files": [
"dist"
Expand Down Expand Up @@ -40,11 +40,9 @@
"homepage": "https://github.com/marcosvrs/gtmplant#readme",
"dependencies": {
"commander": "^2.20.0",
"glob": "^7.1.4",
"plantuml-encoder": "^1.2.5"
},
"devDependencies": {
"@types/glob": "^7.1.1",
"@types/jest": "^24.0.13",
"@types/node": "^12.0.2",
"jest": "^24.8.0",
Expand Down
52 changes: 43 additions & 9 deletions src/Factory/ContainerVersionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,76 @@ import { IGTMTrigger } from "../Interface/GTM/IGTMTrigger";
import { IGTMVariable } from "../Interface/GTM/IGTMVariable";
import { IPlantUMLComposite } from "../Interface/IPlantUMLComposite";
import { IPlantUMLTag } from "../Interface/IPlantUMLTag";
import { IPlantUMLTrigger } from "../Interface/IPlantUMLTrigger";
import { ALL_PAGES_TRIGGER, ID_PREFIX } from "../GTMPlantConfig";
import { IPlantUMLContainer } from "../Interface/IPlantUMLContainer";
import { IDict } from "../Interface/IDict";
import { IGTMBuiltInVariable } from "../Interface/GTM/IGTMBuiltInVariable";

export class ContainerVersionFactory {
protected triggerPlantById: { [index: string]: IPlantUMLTrigger } = {
protected triggerPlantById: IDict<IPlantUMLComposite> = {
[ALL_PAGES_TRIGGER.id]: ALL_PAGES_TRIGGER
};
protected variablePlantByName: { [index: string]: IPlantUMLTrigger } = {};
protected variablePlantByName: IDict<IPlantUMLComposite> = {};

public create(container: IGTMContainerVersion): IPlantUMLContainer {
return {
id: container.container.containerId,
name: container.container.name,
type: '',
variables: this.createVariables(container.variable),
variables: [
...this.createVariables(container.variable),
...this.createBuiltInVariables(container.builtInVariable)
],
triggers: this.createTriggers(container.trigger),
tags: this.createTags(container.tag)
};
}

protected createBuiltInVariables(builtInVariables: IGTMBuiltInVariable[]): IPlantUMLComposite[] {
return builtInVariables.map((builtInVariable: IGTMBuiltInVariable): IPlantUMLComposite => {
const currentBuiltInVariable: IPlantUMLComposite = {
id: `"${builtInVariable.name}"`,
name: builtInVariable.name,
type: builtInVariable.type
};
this.variablePlantByName[builtInVariable.name] = currentBuiltInVariable;
return currentBuiltInVariable;
});
}

protected createVariables(variables: IGTMVariable[]): IPlantUMLComposite[] {
return variables.map((variable: IGTMVariable): IPlantUMLComposite => {
const currentVariable: IPlantUMLComposite = {
variables.forEach((variable: IGTMVariable): void => {
this.variablePlantByName[variable.name] = {
id: ID_PREFIX.VARIABLE + variable.variableId,
name: variable.name,
type: variable.type
};
this.variablePlantByName[variable.name] = currentVariable;
});

return variables.map((variable: IGTMVariable): IPlantUMLComposite => {
const currentVariable: IPlantUMLComposite = this.variablePlantByName[variable.name];
if (variable.parameter === undefined || variable.parameter.length === 0) {
return currentVariable;
}
Object.keys(this.variablePlantByName).forEach((variableName: string): void => {
if (variable.parameter !== undefined && variable.parameter.some(
(parameter: IGTMParameter): boolean =>
(parameter.value !== undefined && parameter.value.indexOf(`{{${variableName}}}`) >= 0) ||
(parameter.list !== undefined && (parameter.list.some((list: IGTMList): boolean => list.map.some((map: IGTMParameter): boolean => map.value !== undefined && map.value.indexOf(`{{${variableName}}}`) >= 0))))
)) {
if (currentVariable.variables === undefined) {
currentVariable.variables = [];
}
currentVariable.variables.push(this.variablePlantByName[variableName]);
}
});
return currentVariable;
});
}

protected createTriggers(triggers: IGTMTrigger[]): IPlantUMLComposite[] {
const plantUMLTriggers: IPlantUMLTrigger[] = triggers.map((trigger: IGTMTrigger): IPlantUMLTrigger => {
const currentTrigger: IPlantUMLTrigger = {
const plantUMLTriggers: IPlantUMLComposite[] = triggers.map((trigger: IGTMTrigger): IPlantUMLComposite => {
const currentTrigger: IPlantUMLComposite = {
id: ID_PREFIX.TRIGGER + trigger.triggerId,
name: trigger.name,
type: trigger.type
Expand Down
9 changes: 8 additions & 1 deletion src/Factory/HandlerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ContainerHandler } from "../Handler/ContainerHandler";
import { TriggerUsageHandler } from "../Handler/TriggerUsageHandler";
import { TagVariableUsageHandler } from "../Handler/TagVariableUsageHandler";
import { TriggerVariableUsageHandler } from "../Handler/TriggerVariableUsageHandler";
import { VariableUsageHandler } from "../Handler/VariableUsageHandler";

export namespace HandlerFactory {
export function createOnlyTagHandler(): IHandler {
Expand All @@ -23,6 +24,11 @@ export namespace HandlerFactory {
containerHandler.setNext(new VariableHandler());
return containerHandler;
}
export function createOnlyVariableWithUsageHandler(): IHandler {
const containerHandler: IHandler = new ContainerHandler();
containerHandler.setNext(new VariableHandler()).setNext(new VariableUsageHandler());
return containerHandler;
}
export function createOnlyTagAndTriggerHandler(): IHandler {
const containerHandler: IHandler = new ContainerHandler();
containerHandler
Expand Down Expand Up @@ -84,7 +90,8 @@ export namespace HandlerFactory {
.setNext(new VariableHandler())
.setNext(new TriggerUsageHandler())
.setNext(new TriggerVariableUsageHandler())
.setNext(new TagVariableUsageHandler());
.setNext(new TagVariableUsageHandler())
.setNext(new VariableUsageHandler());
return containerHandler;
}
}
2 changes: 1 addition & 1 deletion src/Handler/AbstractHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export abstract class AbstractHandler implements IHandler {
return [];
}

protected abstract toPlantUML(component: IPlantUMLComposite[]): string[];
protected abstract toPlantUML(...component: IPlantUMLComposite[]): string[];
}
4 changes: 2 additions & 2 deletions src/Handler/ContainerHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export class ContainerHandler extends AbstractHandler {
if (container.variables.length > 0) {
return [
'@startuml',
...this.toPlantUML([container]),
...this.toPlantUML(container),
...super.handle(container),
'@enduml'
];
}
return super.handle(container);
}

protected toPlantUML(container: IPlantUMLContainer[]): string[] {
protected toPlantUML(...container: IPlantUMLContainer[]): string[] {
return [
`title ${container[0].name}`,
'legend right',
Expand Down
4 changes: 2 additions & 2 deletions src/Handler/TagHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ export class TagHandler extends AbstractHandler {
public handle(container: IPlantUMLContainer): string[] {
if (container.tags.length > 0) {
return [
...this.toPlantUML(container.tags),
...this.toPlantUML(...container.tags),
...super.handle(container)
];
}
return super.handle(container);

}

protected toPlantUML(tags: IPlantUMLTag[]): string[] {
protected toPlantUML(...tags: IPlantUMLTag[]): string[] {
const document: string[] = [];
tags.forEach((tag: IPlantUMLTag): void => {
const tagPlantUMLObjectDeclaration: string = `class "${tag.name}" as ${tag.id}<${tag.type}> << (T, orange) >>`;
Expand Down
4 changes: 2 additions & 2 deletions src/Handler/TagVariableUsageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ export class TagVariableUsageHandler extends AbstractHandler {
public handle(container: IPlantUMLContainer): string[] {
if (container.tags.length > 0) {
return [
...this.toPlantUML(container.tags),
...this.toPlantUML(...container.tags),
...super.handle(container)
];
}
return super.handle(container);
}

protected toPlantUML(tags: IPlantUMLTag[]): string[] {
protected toPlantUML(...tags: IPlantUMLTag[]): string[] {
const usages: string[] = [];
tags.forEach((tag: IPlantUMLTag): void => {
if (tag.variables === undefined || tag.variables.length === 0) {
Expand Down
Loading

0 comments on commit 481ad0a

Please sign in to comment.