From fe93d436b430d01b563318bff591e0dd408c06a4 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Mon, 20 Mar 2017 23:19:19 +0100 Subject: [PATCH] New: Added `oneofs: true` to ConversionOptions, see #710 --- src/converter.js | 5 ++++- src/type.js | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/converter.js b/src/converter.js index ac67c3d72..676514bc2 100644 --- a/src/converter.js +++ b/src/converter.js @@ -193,7 +193,7 @@ function genValuePartial_toObject(gen, field, fieldIndex, prop) { */ converter.toObject = function toObject(mtype) { /* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */ - var fields = mtype.fieldsArray; + var fields = mtype.fieldsArray.sort(util.compareFieldsById); if (!fields.length) return util.codegen()("return {}"); var gen = util.codegen("m", "o") @@ -265,6 +265,9 @@ converter.toObject = function toObject(mtype) { ("}"); } else genValuePartial_toObject(gen, field, i, prop); + if (field.partOf) gen + ("if(o.oneofs)") + ("d%s=%j", util.safeProp(field.partOf.name), field.name); gen ("}"); } diff --git a/src/type.js b/src/type.js index 65dca4a8c..b38f3f387 100644 --- a/src/type.js +++ b/src/type.js @@ -484,6 +484,7 @@ Type.prototype.from = Type.prototype.fromObject; * @property {boolean} [defaults=false] Also sets default values on the resulting object * @property {boolean} [arrays=false] Sets empty arrays for missing repeated fields even if `defaults=false` * @property {boolean} [objects=false] Sets empty objects for missing map fields even if `defaults=false` + * @property {boolean} [oneofs=false] Includes virtual oneof properties set to the present field's name, if any */ /**