From 2263d2236eb21c7e931a0d44f081f37fa45bddd6 Mon Sep 17 00:00:00 2001 From: Rashid Khan Date: Wed, 4 Mar 2015 12:04:05 -0700 Subject: [PATCH] Fix _timestamp and arrays in advanced settings, add test for value type --- .../components/index_patterns/_flatten_hit.js | 2 +- .../settings/sections/advanced/index.js | 4 ++- .../sections/advanced/lib/get_val_type.js | 22 +++++++++++++ .../sections/advanced/lib/get_val_type.js | 32 +++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/kibana/plugins/settings/sections/advanced/lib/get_val_type.js create mode 100644 test/unit/specs/plugins/settings/sections/advanced/lib/get_val_type.js diff --git a/src/kibana/components/index_patterns/_flatten_hit.js b/src/kibana/components/index_patterns/_flatten_hit.js index a1857f5d4840dd..5c2275faf03743 100644 --- a/src/kibana/components/index_patterns/_flatten_hit.js +++ b/src/kibana/components/index_patterns/_flatten_hit.js @@ -16,6 +16,6 @@ define(function (require) { } }); - return hit.$$_flattened = _.merge(source, fields, _.pick(hit, self.metaFields)); + return hit.$$_flattened = _.merge(source, fields, _.pick(hit, self.metaFields), _.pick(hit.fields, self.metaFields)); }; }); diff --git a/src/kibana/plugins/settings/sections/advanced/index.js b/src/kibana/plugins/settings/sections/advanced/index.js index d202021988072a..8c5f8f210cbdcd 100644 --- a/src/kibana/plugins/settings/sections/advanced/index.js +++ b/src/kibana/plugins/settings/sections/advanced/index.js @@ -1,6 +1,8 @@ define(function (require) { var _ = require('lodash'); var configDefaults = require('components/config/defaults'); + var getValType = require('plugins/settings/sections/advanced/lib/get_val_type'); + require('plugins/settings/sections/advanced/advanced_row'); @@ -38,7 +40,7 @@ define(function (require) { var conf = { name: name, defVal: def.value, - type: (def.type || _.isArray(val) || typeof val), + type: getValType(def, val), description: def.description, value: val, }; diff --git a/src/kibana/plugins/settings/sections/advanced/lib/get_val_type.js b/src/kibana/plugins/settings/sections/advanced/lib/get_val_type.js new file mode 100644 index 00000000000000..2ba250c72cf858 --- /dev/null +++ b/src/kibana/plugins/settings/sections/advanced/lib/get_val_type.js @@ -0,0 +1,22 @@ +define(function (require) { + var _ = require('lodash'); + + /** + * @param {object} advanced setting definition object + * @param {?} current value of the setting + * @returns {string} the type to use for determining the display and editor + */ + function getValType(def, value) { + if (def.type) { + return def.type; + } + + if (_.isArray(value) || _.isArray(def.value)) { + return 'array'; + } + + return (typeof def.value); + } + + return getValType; +}); diff --git a/test/unit/specs/plugins/settings/sections/advanced/lib/get_val_type.js b/test/unit/specs/plugins/settings/sections/advanced/lib/get_val_type.js new file mode 100644 index 00000000000000..5d64ae59f61d90 --- /dev/null +++ b/test/unit/specs/plugins/settings/sections/advanced/lib/get_val_type.js @@ -0,0 +1,32 @@ +define(function (require) { + var getValType = require('plugins/settings/sections/advanced/lib/get_val_type'); + describe('Settings', function () { + describe('Advanced', function () { + describe('getValType(def, val)', function () { + it('should be a function', function () { + expect(getValType).to.be.a(Function); + }); + + it('should return the explicitly defined type of a setting', function () { + expect(getValType({type: 'string'})).to.be('string'); + expect(getValType({type: 'json'})).to.be('json'); + expect(getValType({type: 'string', value: 5})).to.be('string'); + }); + + it('should return array if the value is an Array and there is no defined type', function () { + expect(getValType({type: 'string'}, [1, 2, 3])).to.be('string'); + expect(getValType({type: 'json', value: [1, 2, 3]})).to.be('json'); + + expect(getValType({value: 'someString'}, [1, 2, 3])).to.be('array'); + expect(getValType({value: [1, 2, 3]}, 'someString')).to.be('array'); + + }); + + it('should return the type of the default value if there is no type and it is not an array', function () { + expect(getValType({value: 'someString'})).to.be('string'); + expect(getValType({value: 'someString'}, 42)).to.be('string'); + }); + }); + }); + }); +});