Skip to content

Commit edfbfe7

Browse files
committed
Implement remote library schema loading
Also re-enable existing test for loading remote library schemas.
1 parent 9b53ed8 commit edfbfe7

File tree

4 files changed

+68
-22
lines changed

4 files changed

+68
-22
lines changed

common/schema/loader.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ const loadSchema = function (schemaDef = {}, useFallback = true) {
2424
let schemaPromise
2525
if (schemaDef.path) {
2626
schemaPromise = loadLocalSchema(schemaDef.path)
27-
} /* else if (schemaDef.library) {
27+
} else if (schemaDef.library) {
2828
return loadRemoteLibrarySchema(schemaDef.library, schemaDef.version)
29-
} */ else if (schemaDef.version) {
29+
} else if (schemaDef.version) {
3030
schemaPromise = loadRemoteBaseSchema(schemaDef.version)
3131
} else {
3232
return Promise.reject(new Error('Invalid schema definition format.'))
@@ -63,7 +63,7 @@ const loadRemoteBaseSchema = function (version = 'Latest') {
6363
* @return {Promise<object>} The library schema XML data.
6464
*/
6565
const loadRemoteLibrarySchema = function (library, version = 'Latest') {
66-
const url = `https://github.com/raw/hed-standard/hed-schema-library/master/hedxml/HED_${library}_${version}.xml`
66+
const url = `https://github.com/raw/hed-standard/hed-schema-library/main/library_schemas/${library}/hedxml/HED_${library}_${version}.xml`
6767
return loadSchemaFile(
6868
files.readHTTPSFile(url),
6969
stringTemplate`Could not load HED library schema ${1}, version "${2}", from remote repository - "${0}".`,

common/schema/types.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ class Schemas {
120120
/**
121121
* Constructor.
122122
* @param {Schema} baseSchema The base HED schema.
123+
* @param {Map<string, Schema>} librarySchemas The imported library HED schemas.
123124
*/
124-
constructor(baseSchema) {
125+
constructor(baseSchema, librarySchemas = undefined) {
125126
/**
126127
* The base HED schema.
127128
* @type {Schema}
@@ -131,7 +132,7 @@ class Schemas {
131132
* The imported library HED schemas.
132133
* @type {Map<string, Schema>}
133134
*/
134-
this.librarySchemas = new Map()
135+
this.librarySchemas = librarySchemas || new Map()
135136
}
136137

137138
/**

tests/schema.spec.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,27 @@ describe('HED schemas', () => {
2929
})
3030
})
3131

32-
describe.skip('Remote HED library schemas', () => {
32+
describe('Remote HED library schemas', () => {
3333
it('can be loaded from a central GitHub repository', () => {
34-
const remoteHedSchemaLibrary = 'test'
35-
const remoteHedSchemaVersion = '0.0.1'
34+
const remoteHedSchemaVersion = '8.0.0'
35+
const remoteHedSchemaLibrary = 'testlib'
36+
const remoteHedSchemaLibraryVersion = '1.0.2'
3637
return schema
3738
.buildSchema({
38-
library: remoteHedSchemaLibrary,
3939
version: remoteHedSchemaVersion,
40+
libraries: {
41+
testlib: {
42+
library: remoteHedSchemaLibrary,
43+
version: remoteHedSchemaLibraryVersion,
44+
},
45+
},
4046
})
4147
.then((hedSchemas) => {
4248
const hedSchema = hedSchemas.librarySchemas.get(
4349
remoteHedSchemaLibrary,
4450
)
4551
assert.strictEqual(hedSchema.library, remoteHedSchemaLibrary)
46-
assert.strictEqual(hedSchema.version, remoteHedSchemaVersion)
52+
assert.strictEqual(hedSchema.version, remoteHedSchemaLibraryVersion)
4753
})
4854
})
4955
})

validator/schema/init.js

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const zip = require('lodash/zip')
2+
13
const semver = require('semver')
24

35
const loadSchema = require('../../common/schema/loader')
@@ -9,6 +11,19 @@ const { setParent } = require('../../utils/xml2js')
911
const { Hed2SchemaParser } = require('./hed2')
1012
const { HedV8SchemaParser } = require('./hed3')
1113

14+
/**
15+
* Determine whether a HED schema is based on the HED 3 spec.
16+
*
17+
* @param {object} xmlData HED XML data.
18+
* @returns {boolean} Whether the schema is a HED 3 schema.
19+
*/
20+
const isHed3Schema = function (xmlData) {
21+
return (
22+
xmlData.HED.$.library !== undefined ||
23+
semver.gte(xmlData.HED.$.version, '8.0.0-alpha.3')
24+
)
25+
}
26+
1227
/**
1328
* Build a schema attributes object from schema XML data.
1429
*
@@ -18,32 +33,56 @@ const { HedV8SchemaParser } = require('./hed3')
1833
const buildSchemaAttributesObject = function (xmlData) {
1934
const rootElement = xmlData.HED
2035
setParent(rootElement, null)
21-
if (semver.gte(rootElement.$.version, '8.0.0-alpha.3')) {
36+
if (isHed3Schema(xmlData)) {
2237
return new HedV8SchemaParser(rootElement).parse()
2338
} else {
2439
return new Hed2SchemaParser(rootElement).parse()
2540
}
2641
}
2742

2843
/**
29-
* Build a schema container object from a base schema version or path description.
44+
* Build a single schema container object from a base schema version or path description.
45+
*
46+
* @param {object} xmlData The schema's XML data
47+
* @returns {Schema} The HED schema object.
48+
*/
49+
const buildSchemaObject = function (xmlData) {
50+
const schemaAttributes = buildSchemaAttributesObject(xmlData)
51+
const mapping = buildMappingObject(xmlData)
52+
let schema
53+
if (isHed3Schema(xmlData)) {
54+
schema = new Hed3Schema(xmlData, schemaAttributes, mapping)
55+
} else {
56+
schema = new Hed2Schema(xmlData, schemaAttributes, mapping)
57+
}
58+
return schema
59+
}
60+
61+
/**
62+
* Build a schema collection object from a schema specification.
3063
*
31-
* @param {{path: string?, version: string?}} schemaDef The description of which base schema to use.
64+
* @param {{path: string?, version: string?, libraries: object}} schemaDef The description of which base schema to use.
3265
* @param {boolean} useFallback Whether to use a bundled fallback schema if the requested schema cannot be loaded.
3366
* @return {Promise<never>|Promise<Schemas>} The schema container object or an error.
3467
*/
3568
const buildSchema = function (schemaDef = {}, useFallback = true) {
3669
return loadSchema(schemaDef, useFallback).then((xmlData) => {
37-
const schemaAttributes = buildSchemaAttributesObject(xmlData)
38-
const mapping = buildMappingObject(xmlData)
39-
const rootElement = xmlData.HED
40-
let baseSchema
41-
if (semver.gte(rootElement.$.version, '8.0.0-alpha.3')) {
42-
baseSchema = new Hed3Schema(xmlData, schemaAttributes, mapping)
43-
} else {
44-
baseSchema = new Hed2Schema(xmlData, schemaAttributes, mapping)
70+
const baseSchema = buildSchemaObject(xmlData)
71+
if (schemaDef.libraries === undefined) {
72+
return new Schemas(baseSchema)
4573
}
46-
return new Schemas(baseSchema)
74+
const [libraryKeys, libraryDefs] = zip(
75+
...Object.entries(schemaDef.libraries),
76+
)
77+
return Promise.all(
78+
libraryDefs.map((libraryDef) => {
79+
return loadSchema(libraryDef, false)
80+
}),
81+
).then((libraryXmlData) => {
82+
const librarySchemaObjects = libraryXmlData.map(buildSchemaObject)
83+
const librarySchemas = new Map(zip(libraryKeys, librarySchemaObjects))
84+
return new Schemas(baseSchema, librarySchemas)
85+
})
4786
})
4887
}
4988

0 commit comments

Comments
 (0)