Skip to content

Commit

Permalink
add config.set(flatKey) method
Browse files Browse the repository at this point in the history
  • Loading branch information
megastef committed May 16, 2017
1 parent 6f9cc82 commit f769b4b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/util/spmconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

'use strict'
var flatten = require('flat')
var unflatten = require('flat').unflatten
var util = require('util')
var RC = require('rc-yaml-2')
var spmDefaultConfig = {
Expand Down Expand Up @@ -44,8 +45,20 @@ var SpmConfig = function (appType) {
return this
}

SpmConfig.prototype.get = function (key) {
return this.rcFlat[key]
SpmConfig.prototype.get = function (flatKey) {
return this.rcFlat[flatKey]
}

SpmConfig.prototype.set = function (flatKey, value) {
var kv = {}
kv[flatKey] = value
kv.object = true
var result = unflatten(kv)
delete result.object
delete this.rcFlat
util._extend(this, result)
this.rcFlat = flatten(this)
return this
}

module.exports = new SpmConfig(process.env.SPM_AGENT_APP_TYPE || 'spmagent')
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,16 @@ describe('SPM for NodeJS tests', function () {
done(err)
}
})
it('SPM Config "set" accepts flat key', function (done) {
try {
config.set('a.b.0.name', 'test')
if (config.a.b[0].name === 'test') {
done()
} else {
done(new Error('set flat key: a.b.0.name != test'))
}
} catch (err) {
done(err)
}
})
})

0 comments on commit f769b4b

Please sign in to comment.