Skip to content

Commit 2bf232b

Browse files
author
Vitaly Puzrin
authored
Merge pull request #300 from monken/master
support polymorphism for tags
2 parents c76b837 + 2c6e0ae commit 2bf232b

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

lib/js-yaml/dumper.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ function compileStyleMap(schema, map) {
7171
if (tag.slice(0, 2) === '!!') {
7272
tag = 'tag:yaml.org,2002:' + tag.slice(2);
7373
}
74-
75-
type = schema.compiledTypeMap[tag];
74+
type = schema.compiledTypeMap['fallback'][tag];
7675

7776
if (type && _hasOwnProperty.call(type.styleAliases, style)) {
7877
style = type.styleAliases[style];

lib/js-yaml/loader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,8 +1378,8 @@ function composeNode(state, parentIndent, nodeContext, allowToSeek, allowCompact
13781378
break;
13791379
}
13801380
}
1381-
} else if (_hasOwnProperty.call(state.typeMap, state.tag)) {
1382-
type = state.typeMap[state.tag];
1381+
} else if (_hasOwnProperty.call(state.typeMap[state.kind || 'fallback'], state.tag)) {
1382+
type = state.typeMap[state.kind || 'fallback'][state.tag];
13831383

13841384
if (state.result !== null && type.kind !== state.kind) {
13851385
throwError(state, 'unacceptable node kind for !<' + state.tag + '> tag; it should be "' + type.kind + '", not "' + state.kind + '"');

lib/js-yaml/schema.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function compileList(schema, name, result) {
1616

1717
schema[name].forEach(function (currentType) {
1818
result.forEach(function (previousType, previousIndex) {
19-
if (previousType.tag === currentType.tag) {
19+
if (previousType.tag === currentType.tag && previousType.kind === currentType.kind) {
2020
exclude.push(previousIndex);
2121
}
2222
});
@@ -31,16 +31,20 @@ function compileList(schema, name, result) {
3131

3232

3333
function compileMap(/* lists... */) {
34-
var result = {}, index, length;
34+
var result = {
35+
scalar: {},
36+
sequence: {},
37+
mapping: {},
38+
fallback: {}
39+
}, index, length;
3540

3641
function collectType(type) {
37-
result[type.tag] = type;
42+
result[type.kind][type.tag] = result['fallback'][type.tag] = type;
3843
}
3944

4045
for (index = 0, length = arguments.length; index < length; index += 1) {
4146
arguments[index].forEach(collectType);
4247
}
43-
4448
return result;
4549
}
4650

test/units/tagmultikind.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
4+
var assert = require('assert');
5+
var yaml = require('../../');
6+
7+
var tags = [ {
8+
tag: 'Include',
9+
type: 'scalar'
10+
}, {
11+
tag: 'Include',
12+
type: 'mapping'
13+
} ].map(function (fn) {
14+
return new yaml.Type('!' + fn.tag, {
15+
kind: fn.type,
16+
resolve: function () {
17+
return true;
18+
},
19+
construct: function (obj) {
20+
return obj;
21+
}
22+
});
23+
});
24+
25+
var schema = yaml.Schema.create(tags);
26+
27+
28+
test('Process tag with kind: scalar', function () {
29+
assert.deepEqual(yaml.safeLoad('!Include foobar', {
30+
schema: schema
31+
}), 'foobar');
32+
});
33+
34+
35+
test('Process tag with kind: mapping', function () {
36+
assert.deepEqual(yaml.safeLoad('!Include\n location: foobar', {
37+
schema: schema
38+
}), { location: 'foobar' });
39+
});

0 commit comments

Comments
 (0)