From e6caa36aa0d5696e967f9e521967cc5fcce07cc4 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Mon, 16 Apr 2018 03:38:46 -0700 Subject: [PATCH] Fix instances being created with undefined values Happens when creating instances using object templates with nested parameters. Resolves: #294 --- lib/local-cache.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/local-cache.js b/lib/local-cache.js index 1992a98..cd50c2e 100644 --- a/lib/local-cache.js +++ b/lib/local-cache.js @@ -61,6 +61,16 @@ function computeHash() { hash = h.digest("hex"); } +function flattenObject(src, prefix = "", dst = {}) { + for (let k of Object.keys(src)) { + let v = src[k]; + if (typeof v === "object" && !Array.isArray(v)) + flattenObject(v, `${prefix}${k}.`, dst); + else + dst[`${prefix}${k}`] = v; + } + return dst; +} function refresh(callback) { if (!nextRefresh) @@ -99,12 +109,17 @@ function refresh(callback) { return counter = 0; } - // If no keys are defined, consider all parameters as keys to keep the - // same behavior from v1.0 - for (let obj of objects) { + objects = objects.map(obj => { + // Flatten object + obj = flattenObject(obj); + + // If no keys are defined, consider all parameters as keys to keep the + // same behavior from v1.0 if (!obj["_keys"] || !obj["_keys"].length) obj["_keys"] = Object.keys(obj).filter(k => !k.startsWith("_")); - } + + return obj + }) res.sort((a, b) => { if (a.weight === b.weight)