Skip to content

Commit 684901c

Browse files
committed
Code review Entity* and IotTemplate*
1 parent bc421a5 commit 684901c

File tree

12 files changed

+193
-74
lines changed

12 files changed

+193
-74
lines changed
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# for https://github.com/redhat-developer/vscode-yaml
12
id:
23
type: string
34
required: true

schemas/template.schema.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# for https://www.npmjs.com/package/yaml-schema-validator-fork
12
id:
23
type: string
34
required: true

src/Entity/EntityBase.ts

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { EntityType } from './EntityType';
66
import { EntityBaseAttribute } from './EntityBaseAttribute';
77
import { IoTHelper } from '../Helper/IoTHelper';
88
import { IotResult,StatusResult } from '../IotResult';
9+
import { FilesValidator } from '../Validator/FilesValidator';
910

1011
export abstract class EntityBase<T extends EntityBaseAttribute> {
1112
protected readonly _entityLabel:string; //for understandable log
@@ -38,17 +39,23 @@ export abstract class EntityBase<T extends EntityBaseAttribute> {
3839
public Attributes: T;
3940
public Type:EntityType=EntityType.none;
4041

41-
protected _pathFolderSchemas: string;
42+
protected readonly _pathFolderSchema: string;
43+
protected readonly _fileNameSchemaRootYaml: string;
44+
protected readonly _fileNameSchemaDirStructure: string;
4245

43-
constructor( entityLabel:string, entitiesLabel:string,
44-
TCreator: new(pathFolderSchemas?: string) => T,
45-
pathFolderSchemas: string
46+
constructor(entityLabel:string, entitiesLabel:string,
47+
TCreator: new(pathFolderSchema:string,fileNameSchemaRootYaml:string) => T,
48+
pathFolderSchema: string,
49+
fileNameSchemaRootYaml: string, fileNameSchemaDirStructure: string
4650
){
4751
this._entityLabel=entityLabel;
4852
this._entitiesLabel=entitiesLabel;
49-
this.Attributes=new TCreator(pathFolderSchemas);
53+
this.Attributes=new TCreator(pathFolderSchema,fileNameSchemaRootYaml);
54+
//
55+
this._pathFolderSchema=pathFolderSchema;
56+
this._fileNameSchemaRootYaml=fileNameSchemaRootYaml;
57+
this._fileNameSchemaDirStructure=fileNameSchemaDirStructure;
5058
//
51-
this._pathFolderSchemas=pathFolderSchemas;
5259
this._validationErrors.push("non");
5360
}
5461

@@ -58,31 +65,36 @@ export abstract class EntityBase<T extends EntityBaseAttribute> {
5865
this.Type= type;
5966
this._recoverySourcePath=recoverySourcePath;
6067
this._yamlFilePath=yamlFilePath;
61-
this.ValidateEntityBase();
62-
if(!this.IsValid) return;
63-
//if(this.IsValid) this.Parse(path);
64-
let attributes = this.Attributes as any;
65-
attributes.Init(this.YAMLFilePath);
66-
if(attributes.IsValid) {
67-
//ok
68-
//check if folder matches entity and id
69-
if(this.RootNameDir!=attributes.Id)
70-
this._validationErrors.push(`${this._entityLabel} folder name ${this.RootNameDir} `+
71-
`does not match id value ${attributes.Id}.`+
72-
`You need to rename the folder or change the ${this._entityLabel} id.`);
73-
}else{
68+
//Attributes
69+
//let attributes = this.Attributes as any;
70+
this._validationErrors=[];
71+
const isValidAttributes = this.Attributes.InitBaseAttribute(this.YAMLFilePath);
72+
if(!isValidAttributes)
7473
//error
75-
this._validationErrors = attributes.ValidationErrors.slice();
76-
}
74+
this._validationErrors = this.Attributes.ValidationErrors.slice();
75+
if(!this.IsValid) return;
76+
//Base
77+
this.ValidateBase();
78+
if(!this.IsValid) return;
7779
} catch (err: any){
7880
this._validationErrors.push(`Error Init ${this._entitiesLabel}. Error: ${err}`);
7981
}
8082
}
8183

82-
private ValidateEntityBase(){
84+
private ValidateBase(){
8385
this._validationErrors=[];
84-
if (!fs.existsSync(this.YAMLFilePath))
85-
this._validationErrors.push(`${this.YAMLFilePath} file does not exist`);
86+
//checking folder structure
87+
//FilesValidator
88+
let filesValidator=new FilesValidator(this._pathFolderSchema);
89+
let result = filesValidator.ValidateFiles(this.RootDir,this._fileNameSchemaDirStructure);
90+
const validationErrors=<Array<string>>result.returnObject;
91+
this._validationErrors = validationErrors.slice();
92+
//check if folder matches entity and id
93+
if(this.RootNameDir!=this.Attributes.Id)
94+
this._validationErrors.push(`${this._entityLabel} folder name ${this.RootNameDir} `+
95+
`does not match id value ${this.Attributes.Id}.`+
96+
`You need to rename the folder or change the ${this._entityLabel} id.`);
97+
//
8698
}
8799

88100
public Move(destDir:string):IotResult {

src/Entity/EntityBaseAttribute.ts

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as vscode from 'vscode';
22
import * as fs from 'fs';
33
import * as path from 'path';
44
import YAML from 'yaml';
5-
import { YamlSchemaValidator } from '../Validator/YamlSchemaValidator';
5+
import { YamlValidatorRedHat } from '../Validator/YamlValidatorRedHat';
6+
import { IYamlValidator } from '../Validator/IYamlValidator';
67

78
export class EntityBaseAttribute {
89
private _id:string="";
@@ -40,27 +41,39 @@ export class EntityBaseAttribute {
4041
public get Tags(): Array<string> {
4142
return this._tags;}
4243
//Validation
43-
public get IsValid(): boolean {
44+
protected get IsValid(): boolean {
4445
if(this._validationErrors.length==0) return true;else return false;}
4546
protected _validationErrors:Array<string>=[];
4647
public get ValidationErrors(): Array<string> {
4748
return this._validationErrors;}
4849

49-
protected _schemasFolderPath: string;
50+
protected readonly _pathFolderSchema: string;
51+
protected readonly _fileNameSchemaRootYaml: string;
5052

51-
constructor(schemasFolderPath: string|undefined
53+
constructor(pathFolderSchema:string,fileNameSchemaRootYaml:string
5254
){
55+
this._pathFolderSchema=pathFolderSchema;
56+
this._fileNameSchemaRootYaml=fileNameSchemaRootYaml;
5357
this._validationErrors.push("non");
54-
this._schemasFolderPath=schemasFolderPath ?? "non";
5558
}
5659

57-
protected Parse(filePath:string){
60+
public InitBaseAttribute(yamlFilePath:string):boolean
61+
{
62+
let result:boolean;
63+
result=this.ParseBaseAttribute(yamlFilePath);
64+
if(!result) false;
65+
result=this.PostCheckAfterParse(yamlFilePath);
66+
//
67+
return result;
68+
}
69+
70+
private ParseBaseAttribute(yamlFilePath:string):boolean{
5871
try {
5972
//validate
60-
this.ValidateBaseAttribute(filePath);
61-
if(!this.IsValid) return;
73+
this.ValidateBaseAttribute(yamlFilePath);
74+
if(!this.IsValid) return false;
6275
//
63-
const file = fs.readFileSync(filePath, 'utf8');
76+
const file = fs.readFileSync(yamlFilePath, 'utf8');
6477
const obj=YAML.parse(file);
6578
//one value
6679
this._id=obj.id;
@@ -109,16 +122,33 @@ export class EntityBaseAttribute {
109122
while(true)
110123
//next
111124
} catch (err: any){
112-
this._validationErrors.push(`File: ${filePath} Error parsing attributes: ${err}`);
125+
this._validationErrors.push(`File: ${yamlFilePath} Error parsing attributes: ${err}`);
126+
}
127+
return this.IsValid;
128+
}
129+
130+
private PostCheckAfterParse(yamlFilePath:string):boolean{
131+
try {
132+
//check id=""
133+
if (this.Id=="") this._validationErrors.push("id cannot be empty");
134+
} catch (err: any){
135+
this._validationErrors.push(`File: ${yamlFilePath} Error PostCheckAfterParce: ${err}`);
113136
}
137+
return this.IsValid;
114138
}
115139

116140
private ValidateBaseAttribute(yamlFilePath:string)
117141
{
118142
this._validationErrors=[];
119-
//YamlSchemaValidator
120-
let yamlSchemaValidator=new YamlSchemaValidator(this._schemasFolderPath);
121-
let result = yamlSchemaValidator.ValidateSchema(yamlFilePath,"entitybase.schema.yaml");
143+
//exist
144+
if (!fs.existsSync(yamlFilePath))
145+
{
146+
this._validationErrors.push(`${yamlFilePath} file does not exist`);
147+
return;
148+
}
149+
//YamlValidator
150+
let yamlValidator:IYamlValidator=new YamlValidatorRedHat(this._pathFolderSchema);
151+
let result = yamlValidator.ValidateSchema(yamlFilePath,this._fileNameSchemaRootYaml);
122152
const validationErrors=<Array<string>>result.returnObject;
123153
this._validationErrors = validationErrors.slice();
124154
}

src/Entity/EntityCollection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
411411

412412
}
413413

414-
//TODO убрать, заменить на Interface IConfig
414+
//TODO убрать, заменить на Interface IConfig. Все еще под ???
415415

416416
export interface IConfigEntityCollection {
417417
extVersion: string;

src/Templates/IotTemplate.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@ import { IotDevice } from '../IotDevice';
1313
import { IotConfiguration } from '../Configuration/IotConfiguration';
1414
import { FilesValidator } from '../Validator/FilesValidator';
1515

16+
/*
17+
load order:
18+
Create:
19+
1) IotTemplate - constructor
20+
2) EntityBase - constructor
21+
3) IotTemplateAttribute - constructor
22+
4) EntityBaseAttribute - constructor
23+
Init:
24+
1) IotTemplate - Init
25+
2) EntityBase - Init
26+
3) EntityBaseAttribute - InitBaseAttribute
27+
4) EntityBaseAttribute - ParseBaseAttribute
28+
5) EntityBaseAttribute - ValidateBaseAttribute
29+
6) EntityBaseAttribute - PostCheckAfterParse
30+
7) EntityBase - ValidateBase
31+
8) IotTemplateAttribute - Init
32+
9) IotTemplateAttribute - Parse
33+
10) IotTemplateAttribute - Validate
34+
11) IotTemplate - Validate
35+
*/
36+
1637
export class IotTemplate extends EntityBase<IotTemplateAttribute> {
1738
public get StoragePath(): string {
1839
return this.RootDir+"\\storage";}
@@ -23,30 +44,27 @@ export class IotTemplate extends EntityBase<IotTemplateAttribute> {
2344

2445
private _mergeDictionary:Map<string,string>= new Map<string,string>();
2546

26-
constructor(pathFolderSchemas: string
47+
constructor(pathFolderSchema: string
2748
){
28-
super("template","templates",IotTemplateAttribute,pathFolderSchemas);
49+
super("template","templates",IotTemplateAttribute,
50+
pathFolderSchema,"template.fastiot.schema.yaml","template.fastiot.files.schema.json");
2951
}
3052

3153
public Init(type:EntityType,yamlFilePath:string,recoverySourcePath?:string)
3254
{
3355
super.Init(type,yamlFilePath,recoverySourcePath);
3456
if(!this.IsValid) return;
35-
//next
57+
//Attributes
58+
const isValidAttributes = this.Attributes.Init(yamlFilePath);
59+
if(!isValidAttributes)
60+
//error
61+
this._validationErrors = this.Attributes.ValidationErrors.slice();
62+
if(!this.IsValid) return;
3663
this.Validate();
37-
//if(this.IsValid) this.Parse(filePath);
3864
}
3965

4066
protected Validate(){
4167
this._validationErrors=[];
42-
//checking folder structure
43-
//FilesValidator
44-
let filesValidator=new FilesValidator(this._pathFolderSchemas);
45-
let result = filesValidator.ValidateFiles(this.RootDir,"template.files.schema.json");
46-
const validationErrors=<Array<string>>result.returnObject;
47-
this._validationErrors = validationErrors.slice();
48-
//check id=""
49-
if (this.Attributes.Id=="") this._validationErrors.push("id cannot be empty");
5068
// TODO: проверка наличия файлов для dotnetapp.csproj которые внутри
5169
}
5270

src/Templates/IotTemplateAttribute.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import * as vscode from 'vscode';
22
import * as fs from 'fs';
33
import * as path from 'path';
44
import YAML from 'yaml';
5-
65
import { EntityBaseAttribute } from '../Entity/EntityBaseAttribute';
76
import { IoTHelper } from '../Helper/IoTHelper';
8-
import { YamlSchemaValidator } from '../Validator/YamlSchemaValidator';
97

108
export class IotTemplateAttribute extends EntityBaseAttribute {
119
private _typeProj:string="";
@@ -30,33 +28,29 @@ export class IotTemplateAttribute extends EntityBaseAttribute {
3028
//dotnetapp.csproj => .csproj
3129
return IoTHelper.GetFileExtensions(this.MainFileProj);}
3230

33-
constructor(schemasFolderPath?: string){
34-
super(schemasFolderPath);
31+
constructor(pathFolderSchema:string,fileNameSchemaRootYaml:string){
32+
super(pathFolderSchema,fileNameSchemaRootYaml);
3533
}
3634

37-
public Init(filePath:string)
35+
public Init(yamlFilePath:string):boolean
3836
{
39-
//Base Init
40-
super.Parse(filePath);
41-
//
42-
if(!this.IsValid) return;
43-
//next
44-
this.Validate(filePath);
45-
if(this.IsValid) this.Parse(filePath);
37+
//Init
38+
return this.Parse(yamlFilePath);
4639
}
4740

48-
protected Validate(yamlFilePath:string){
41+
private Validate(yamlFilePath:string){
4942
this._validationErrors=[];
50-
//YamlSchemaValidator
51-
let yamlSchemaValidator=new YamlSchemaValidator(this._schemasFolderPath);
52-
let result = yamlSchemaValidator.ValidateSchema(yamlFilePath,"template.schema.yaml");
53-
const validationErrors=<Array<string>>result.returnObject;
54-
this._validationErrors = validationErrors.slice();
43+
//custom Validator
44+
5545
}
5646

57-
protected Parse(filePath:string){
47+
private Parse(yamlFilePath:string):boolean{
5848
try {
59-
const file = fs.readFileSync(filePath, 'utf8');
49+
//validate
50+
this.Validate(yamlFilePath);
51+
if(!this.IsValid) return false;
52+
//
53+
const file = fs.readFileSync(yamlFilePath, 'utf8');
6054
const obj=YAML.parse(file);
6155
//one value
6256
this._typeProj=obj.typeProj;
@@ -93,8 +87,9 @@ export class IotTemplateAttribute extends EntityBaseAttribute {
9387
}
9488
while(true)
9589
//next
96-
} catch (err: any){
97-
this._validationErrors.push(`File: ${filePath} Error parsing attributes: ${err}`);
90+
} catch (err: any){
91+
this._validationErrors.push(`File: ${yamlFilePath} Error parsing attributes: ${err}`);
9892
}
93+
return this.IsValid;
9994
}
10095
}

src/Validator/IYamlValidator.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as vscode from 'vscode';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
import { IotResult,StatusResult } from '../IotResult';
5+
import { IotDevice } from '../IotDevice';
6+
import { IotTemplate } from '../Templates/IotTemplate';
7+
import { LogLevel } from '../shared/LogLevel';
8+
9+
export interface IYamlValidator {
10+
ValidateSchema (yamlFilePath:string, schemaFileName:string):IotResult;
11+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import * as vscode from 'vscode';
22
import * as fs from 'fs';
33
import * as path from 'path';
44
import { IotResult,StatusResult } from '../IotResult';
5+
import { IYamlValidator } from './IYamlValidator';
56

6-
export class YamlSchemaValidator {
7+
export class YamlValidatorFork implements IYamlValidator {
78
private readonly _schemasFolderPath: string;
89

910
constructor(schemasFolderPath: string){

0 commit comments

Comments
 (0)