Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support for number and boolean when using custom tag objects #722

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions dist/js-yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.jsyaml = {}));
}(this, (function (exports) { 'use strict';
})(this, (function (exports) { 'use strict';

function isNothing(subject) {
return (typeof subject === 'undefined') || (subject === null);
Expand Down Expand Up @@ -3632,6 +3632,7 @@
var type = _toString.call(state.dump);
var inblock = block;
var tagStr;
var simpleTag = false;

if (block) {
block = (state.flowLevel < 0 || state.flowLevel > level);
Expand Down Expand Up @@ -3688,8 +3689,14 @@
if (state.tag !== '?') {
writeScalar(state, state.dump, level, iskey, inblock);
}
} else if ((type === '[object Number]') || (type === '[object Boolean]')) {
if (state.tag !== '?') {
state.dump = String(state.dump);
}
} else if (type === '[object Undefined]') {
return false;
} else if (state.tag !== null && state.tag !== '?' && type === '[object Null]') {
simpleTag = true;
} else {
if (state.skipInvalid) return false;
throw new exception('unacceptable kind of an object to dump ' + type);
Expand Down Expand Up @@ -3721,7 +3728,7 @@
tagStr = '!<' + tagStr + '>';
}

state.dump = tagStr + ' ' + state.dump;
return simpleTag ? (state.dump = tagStr) : (state.dump = tagStr + ' ' + state.dump);
}
}

Expand Down Expand Up @@ -3860,7 +3867,7 @@
exports.Schema = Schema;
exports.Type = Type;
exports.YAMLException = YAMLException;
exports.default = jsYaml;
exports["default"] = jsYaml;
exports.dump = dump;
exports.load = load;
exports.loadAll = loadAll;
Expand All @@ -3871,4 +3878,4 @@

Object.defineProperty(exports, '__esModule', { value: true });

})));
}));
2 changes: 1 addition & 1 deletion dist/js-yaml.min.js

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions dist/js-yaml.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3626,6 +3626,7 @@ function writeNode(state, level, object, block, compact, iskey, isblockseq) {
var type = _toString.call(state.dump);
var inblock = block;
var tagStr;
var simpleTag = false;

if (block) {
block = (state.flowLevel < 0 || state.flowLevel > level);
Expand Down Expand Up @@ -3682,8 +3683,14 @@ function writeNode(state, level, object, block, compact, iskey, isblockseq) {
if (state.tag !== '?') {
writeScalar(state, state.dump, level, iskey, inblock);
}
} else if ((type === '[object Number]') || (type === '[object Boolean]')) {
if (state.tag !== '?') {
state.dump = String(state.dump);
}
} else if (type === '[object Undefined]') {
return false;
} else if (state.tag !== null && state.tag !== '?' && type === '[object Null]') {
simpleTag = true;
} else {
if (state.skipInvalid) return false;
throw new exception('unacceptable kind of an object to dump ' + type);
Expand Down Expand Up @@ -3715,7 +3722,7 @@ function writeNode(state, level, object, block, compact, iskey, isblockseq) {
tagStr = '!<' + tagStr + '>';
}

state.dump = tagStr + ' ' + state.dump;
return simpleTag ? (state.dump = tagStr) : (state.dump = tagStr + ' ' + state.dump);
}
}

Expand Down Expand Up @@ -3847,5 +3854,4 @@ var jsYaml = {
safeDump: safeDump
};

export default jsYaml;
export { CORE_SCHEMA, DEFAULT_SCHEMA, FAILSAFE_SCHEMA, JSON_SCHEMA, Schema, Type, YAMLException, dump, load, loadAll, safeDump, safeLoad, safeLoadAll, types };
export { CORE_SCHEMA, DEFAULT_SCHEMA, FAILSAFE_SCHEMA, JSON_SCHEMA, Schema, Type, YAMLException, jsYaml as default, dump, load, loadAll, safeDump, safeLoad, safeLoadAll, types };
9 changes: 8 additions & 1 deletion lib/dumper.js
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ function writeNode(state, level, object, block, compact, iskey, isblockseq) {
var type = _toString.call(state.dump);
var inblock = block;
var tagStr;
var simpleTag = false;

if (block) {
block = (state.flowLevel < 0 || state.flowLevel > level);
Expand Down Expand Up @@ -861,8 +862,14 @@ function writeNode(state, level, object, block, compact, iskey, isblockseq) {
if (state.tag !== '?') {
writeScalar(state, state.dump, level, iskey, inblock);
}
} else if ((type === '[object Number]') || (type === '[object Boolean]')) {
if (state.tag !== '?') {
state.dump = String(state.dump);
}
} else if (type === '[object Undefined]') {
return false;
} else if (state.tag !== null && state.tag !== '?' && type === '[object Null]') {
simpleTag = true;
} else {
if (state.skipInvalid) return false;
throw new YAMLException('unacceptable kind of an object to dump ' + type);
Expand Down Expand Up @@ -894,7 +901,7 @@ function writeNode(state, level, object, block, compact, iskey, isblockseq) {
tagStr = '!<' + tagStr + '>';
}

state.dump = tagStr + ' ' + state.dump;
return simpleTag ? (state.dump = tagStr) : (state.dump = tagStr + ' ' + state.dump);
}
}

Expand Down
50 changes: 50 additions & 0 deletions test/issues/0385.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,54 @@ describe('Multi tag', function () {
schema: schema
}), 'test: !<foo> bar\n');
});

it('should dump multi types with custom tag and number and boolean', function () {
let tags = [
new yaml.Type('!', {
kind: 'scalar',
multi: true,
predicate: function (obj) {
return !!obj.tag;
},
representName: function (obj) {
return obj.tag;
},
represent: function (obj) {
return obj.value;
}
})
];

let schema = yaml.DEFAULT_SCHEMA.extend(tags);

assert.strictEqual(yaml.dump({ number: { tag: 'foo', value: 3 }, boolean: { tag: 'foo', value: true } }, {
schema: schema
}), 'number: !<foo> 3\nboolean: !<foo> true\n');
});


it('should dump simple tags (enums) with no data', function () {
let tags = [
new yaml.Type('!', {
kind: 'scalar',
multi: true,
predicate: function (obj) {
return !!obj.tag;
},
representName: function (obj) {
return obj.tag;
},
represent: function (obj) {
return obj.value;
}
})
];

let schema = yaml.DEFAULT_SCHEMA.extend(tags);

assert.strictEqual(yaml.dump({ number: { tag: 'foo', value: null } }, {
schema: schema
}), 'number: !<foo>\n');
});

});