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

Replace properties which is already present in property object #35

Open
wants to merge 2 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
18 changes: 7 additions & 11 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ function getObjectKeys (rawData, overlappingArrays = 'merge') {
}

function replacer (ctx) {
return (val) => typeof val === 'string' ? val.replace(
return (val, properties) => typeof val === 'string' ? val.replace(
/\$\{(.+?)(?::(.+?))?\}/g,
(match, group, def) => ctx[group] || def || match) : val
(match, group, def) => ctx[group] || properties[group] || def || match) : val
}
/**
* Configuration handler
Expand All @@ -110,15 +110,11 @@ class Config {
constructor (data, context) {
this._raw = data
const properties = getSources(this._raw).reduce((accum, value) => Object.assign({}, value, accum), {})
if (context) {
const rep = replacer(context)
this._properties = Object.keys(properties).reduce((acc, key) => {
acc[key] = rep(acc[key])
return acc
}, properties)
} else {
this._properties = properties
}
const rep = replacer(context)
this._properties = Object.keys(properties).reduce((acc, key) => {
acc[key] = rep(acc[key], acc)
return acc
}, properties)
}

/**
Expand Down
22 changes: 21 additions & 1 deletion tests/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { describe, it, before, after } = require('mocha')
const Client = require('..')
const { equal, deepEqual, ok, rejects } = require('assert').strict
const AUTH = 'Basic dXNlcm5hbWU6cGFzc3dvcmQ='
const { DATA, COMPLEX_DATA_1, COMPLEX_DATA_2, SUBSTITUTION, OVERLAPPING_LISTS } =
const { DATA, COMPLEX_DATA_1, COMPLEX_DATA_2, SUBSTITUTION, OVERLAPPING_LISTS, DYNAMIC_PROPERTY } =
require('./fixtures.json')

let lastURL = null
Expand Down Expand Up @@ -39,6 +39,8 @@ describe('Spring Cloud Configuration Node Client', function () {
res.end(JSON.stringify(SUBSTITUTION))
} else if (lastURL.startsWith('/overlapping_lists')) {
res.end(JSON.stringify(OVERLAPPING_LISTS))
} else if (lastURL.startsWith('/dynamic_property')) {
res.end(JSON.stringify(DYNAMIC_PROPERTY))
} else res.end(JSON.stringify(DATA))
}).listen(port, done)
server.on('clientError', (err, socket) => {
Expand Down Expand Up @@ -295,6 +297,24 @@ describe('Spring Cloud Configuration Node Client', function () {
.load({ endpoint, name: 'substitution', context })
deepEqual(config.toObject(), expectation)
})

it('replaces references with a existing property object', async function () {
const expectation = {
key01: 'Hello',
key03: 42,
key04: 'Javier',
key05: 'Javier-SecretWord',
key06: false,
key07: null,
key08: 'super.password',
key09: '${MISSING_KEY}', // eslint-disable-line
key10: 'Hello'
}
const context = { MY_USERNAME: 'Javier', MY_PASSWORD: 'SecretWord' }
const config = await Client
.load({ endpoint, name: 'dynamic_property', context })
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't work if the context is not defined

deepEqual(config.toObject(), expectation)
})
})

describe('HTTPS Server', function () {
Expand Down
17 changes: 17 additions & 0 deletions tests/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,22 @@
}
}
]
},
"DYNAMIC_PROPERTY": {
"propertySources": [
{
"source": {
"key01": "Hello",
"key03": 42,
"key04": "${MY_USERNAME}",
"key05": "${MY_USERNAME}-${MY_PASSWORD}",
"key06": false,
"key07": null,
"key08": "${MY_OLD_PASSWORD:super.password}",
"key09": "${MISSING_KEY}",
"key10": "${key01}"
}
}
]
}
}