-
-
Notifications
You must be signed in to change notification settings - Fork 776
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multi tags covering all tags with the fixed prefix
- Loading branch information
Showing
7 changed files
with
191 additions
and
9 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,42 @@ | ||
'use strict'; | ||
|
||
/*eslint-disable no-console*/ | ||
|
||
var fs = require('fs'); | ||
var path = require('path'); | ||
var util = require('util'); | ||
var yaml = require('../'); | ||
|
||
|
||
var tags = [ 'scalar', 'sequence', 'mapping' ].map(function (kind) { | ||
// first argument here is a prefix, so this type will handle anything starting with ! | ||
return new yaml.Type('!', { | ||
kind: kind, | ||
multi: true, | ||
construct: function (data, type) { | ||
return { type: type, data: data }; | ||
} | ||
}); | ||
}); | ||
|
||
var SCHEMA = yaml.DEFAULT_SCHEMA.extend(tags); | ||
|
||
// do not execute the following if file is required (http://stackoverflow.com/a/6398335) | ||
if (require.main === module) { | ||
|
||
// And read a document using that schema. | ||
fs.readFile(path.join(__dirname, 'handle_unknown_types.yml'), 'utf8', function (error, data) { | ||
var loaded; | ||
|
||
if (!error) { | ||
loaded = yaml.load(data, { schema: SCHEMA }); | ||
console.log(util.inspect(loaded, false, 20, true)); | ||
} else { | ||
console.error(error.stack || error.message || String(error)); | ||
} | ||
}); | ||
} | ||
|
||
// There are some exports to play with this example interactively. | ||
module.exports.tags = tags; | ||
module.exports.SCHEMA = SCHEMA; |
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,4 @@ | ||
subject: Handling unknown types in JS-YAML | ||
scalar: !unknown_scalar_tag 123 | ||
sequence: !unknown_sequence_tag [ 1, 2, 3 ] | ||
mapping: !unknown_mapping_tag { foo: 1, bar: 2 } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
'use strict'; | ||
|
||
|
||
const assert = require('assert'); | ||
const yaml = require('../../'); | ||
|
||
|
||
describe('Multi tag', function () { | ||
it('should process multi tags', function () { | ||
let tags = [ 'scalar', 'mapping', 'sequence' ].map(kind => | ||
new yaml.Type('!', { | ||
kind, | ||
multi: true, | ||
resolve: function () { | ||
return true; | ||
}, | ||
construct: function (value, tag) { | ||
return { kind, tag, value }; | ||
} | ||
}) | ||
); | ||
|
||
let schema = yaml.DEFAULT_SCHEMA.extend(tags); | ||
|
||
let expected = [ | ||
{ | ||
kind: 'scalar', | ||
tag: '!t1', | ||
value: '123' | ||
}, | ||
{ | ||
kind: 'sequence', | ||
tag: '!t2', | ||
value: [ 1, 2, 3 ] | ||
}, | ||
{ | ||
kind: 'mapping', | ||
tag: '!t3', | ||
value: { a: 1, b: 2 } | ||
} | ||
]; | ||
|
||
assert.deepStrictEqual(yaml.load(` | ||
- !t1 123 | ||
- !t2 [ 1, 2, 3 ] | ||
- !t3 { a: 1, b: 2 } | ||
`, { | ||
schema: schema | ||
}), expected); | ||
}); | ||
|
||
|
||
it('should process tags depending on prefix', function () { | ||
let tags = [ '!foo', '!bar', '!' ].map(prefix => | ||
new yaml.Type(prefix, { | ||
kind: 'scalar', | ||
multi: true, | ||
resolve: function () { | ||
return true; | ||
}, | ||
construct: function (value, tag) { | ||
return { prefix, tag, value }; | ||
} | ||
}) | ||
); | ||
|
||
tags.push( | ||
new yaml.Type('!bar', { | ||
kind: 'scalar', | ||
resolve: function () { | ||
return true; | ||
}, | ||
construct: function (value) { | ||
return { single: true, value }; | ||
} | ||
}) | ||
); | ||
|
||
let schema = yaml.DEFAULT_SCHEMA.extend(tags); | ||
|
||
let expected = [ | ||
{ prefix: '!foo', tag: '!foo', value: '1' }, | ||
{ prefix: '!foo', tag: '!foo2', value: '2' }, | ||
{ single: true, value: '3' }, | ||
{ prefix: '!bar', tag: '!bar2', value: '4' }, | ||
{ prefix: '!', tag: '!baz', value: '5' } | ||
]; | ||
|
||
assert.deepStrictEqual(yaml.load(` | ||
- !foo 1 | ||
- !foo2 2 | ||
- !bar 3 | ||
- !bar2 4 | ||
- !baz 5 | ||
`, { | ||
schema: schema | ||
}), expected); | ||
}); | ||
}); |