-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rule): ENTITY_VALUE_AS_ENTITY_NAME rule implementation
Closes #471
- Loading branch information
Showing
10 changed files
with
2,994 additions
and
6 deletions.
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,47 @@ | ||
import * as path from 'path'; | ||
import { keys, includes, isString, head } from 'lodash'; | ||
import { ENTITY_VALUE_AS_ENTITY_NAME } from '../registry'; | ||
import { DdfDataSet } from '../../ddf-definitions/ddf-data-set'; | ||
import { Issue } from '../issue'; | ||
import { | ||
CONCEPT_TYPE_ENTITY_DOMAIN, | ||
CONCEPT_TYPE_ENTITY_SET, | ||
IDataPackageResourceRecord | ||
} from '../../utils/ddf-things'; | ||
|
||
|
||
const getGidByResource = (ddfDataSet: DdfDataSet, entitiesPath: string): string => { | ||
const ddfRoot = ddfDataSet.ddfRoot; | ||
const parsedEntitiesPath = path.parse(entitiesPath); | ||
const relativeDdfPath = path.relative(parsedEntitiesPath.dir, ddfRoot.dataPackageDescriptor.rootFolder); | ||
const dataPackageCompatiblePath = path.join(relativeDdfPath, parsedEntitiesPath.base); | ||
const resource: IDataPackageResourceRecord[] = ddfRoot.getDataPackageResources() | ||
.filter(record => record.path === dataPackageCompatiblePath && isString(record.schema.primaryKey)); | ||
|
||
return <string>head(resource).schema.primaryKey; | ||
}; | ||
|
||
export const rule = { | ||
rule: (ddfDataSet: DdfDataSet) => { | ||
const entityConcepts = ddfDataSet.getConceptsByType(CONCEPT_TYPE_ENTITY_DOMAIN, CONCEPT_TYPE_ENTITY_SET); | ||
const entitiesByFile = ddfDataSet.getEntity().getDataByFiles(); | ||
const issues = []; | ||
|
||
for (const entityFileName of keys(entitiesByFile)) { | ||
const gid = getGidByResource(ddfDataSet, entityFileName); | ||
|
||
for (const record of entitiesByFile[entityFileName]) { | ||
if (includes(entityConcepts, record[gid])) { | ||
issues.push(new Issue(ENTITY_VALUE_AS_ENTITY_NAME) | ||
.setPath(record.$$source) | ||
.setData({ | ||
entityName: gid, | ||
entityRecord: record | ||
})) | ||
} | ||
} | ||
} | ||
|
||
return issues; | ||
} | ||
}; |
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
114 changes: 114 additions & 0 deletions
114
test/fixtures/rules-cases/entity-value-as-entity-name/datapackage.json
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,114 @@ | ||
{ | ||
"name": "good-folder-dp", | ||
"title": "good-folder-dp", | ||
"description": "", | ||
"version": "0.0.1", | ||
"language": { | ||
"id": "en", | ||
"name": "English" | ||
}, | ||
"translations": [], | ||
"license": "", | ||
"author": "", | ||
"resources": [ | ||
{ | ||
"path": "ddf--concepts.csv", | ||
"name": "ddf--concepts", | ||
"schema": { | ||
"fields": [ | ||
{ | ||
"name": "concept" | ||
}, | ||
{ | ||
"name": "concept_type" | ||
}, | ||
{ | ||
"name": "name" | ||
} | ||
], | ||
"primaryKey": "concept" | ||
} | ||
}, | ||
{ | ||
"path": "ddf--datapoints--gas_production_bcf--by--geo--year.csv", | ||
"name": "gas_production_bcf--by--geo--year", | ||
"schema": { | ||
"fields": [ | ||
{ | ||
"name": "geo" | ||
}, | ||
{ | ||
"name": "year" | ||
}, | ||
{ | ||
"name": "gas_production_bcf" | ||
} | ||
], | ||
"primaryKey": [ | ||
"geo", | ||
"year" | ||
] | ||
} | ||
}, | ||
{ | ||
"path": "ddf--entities--geo.csv", | ||
"name": "geo", | ||
"schema": { | ||
"fields": [ | ||
{ | ||
"name": "geo" | ||
}, | ||
{ | ||
"name": "geo_name" | ||
} | ||
], | ||
"primaryKey": "geo" | ||
} | ||
} | ||
], | ||
"ddfSchema": { | ||
"datapoints": [ | ||
{ | ||
"primaryKey": [ | ||
"geo", | ||
"year" | ||
], | ||
"value": "gas_production_bcf", | ||
"resources": [ | ||
"gas_production_bcf--by--geo--year" | ||
] | ||
} | ||
], | ||
"entities": [ | ||
{ | ||
"primaryKey": [ | ||
"geo" | ||
], | ||
"value": "geo_name", | ||
"resources": [ | ||
"geo" | ||
] | ||
} | ||
], | ||
"concepts": [ | ||
{ | ||
"primaryKey": [ | ||
"concept" | ||
], | ||
"value": "concept_type", | ||
"resources": [ | ||
"ddf--concepts" | ||
] | ||
}, | ||
{ | ||
"primaryKey": [ | ||
"concept" | ||
], | ||
"value": "name", | ||
"resources": [ | ||
"ddf--concepts" | ||
] | ||
} | ||
] | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
test/fixtures/rules-cases/entity-value-as-entity-name/ddf--concepts.csv
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 @@ | ||
concept,concept_type,name | ||
geo,entity_domain,Geo | ||
gas_production_bcf,measure,Gas Production – Bcf | ||
name,string,Name | ||
geo_name,string,Name | ||
year,time,Year |
Oops, something went wrong.