8000 Fix #31 · soujava/vertx-config@48753e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 48753e6

Browse files
committed
Add the ability to list the set of keys imported by the Env Config Store.
1 parent 89ef30b commit 48753e6

File tree

9 files changed

+137
-9
lines changed

9 files changed

+137
-9
lines changed

vertx-config/src/main/asciidoc/groovy/index.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,23 @@ def json = [
210210
You can configure the `raw-data` attribute (`false` by default). If `raw-data` is `true` no attempts to convert
211211
values will be made and you'll be able to get raw values using `config.getString(key)`.
212212

213+
If you want to select the set of keys to import, use the `keys` attributes. It filters out all non selected keys. Keys
214+
must be listed individually:
215+
216+
[source, groovy]
217+
----
218+
def json = [
219+
type:"env",
220+
config:[
221+
keys:[
222+
"SERVICE1_HOST",
223+
"SERVICE2_HOST"
224+
]
225+
]
226+
]
227+
228+
----
229+
213230
=== System Properties
214231

215232
This configuration store maps system properties to a Json Object contributed to the

vertx-config/src/main/asciidoc/java/index.adoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,16 @@ ConfigStoreOptions json = new ConfigStoreOptions()
179179
You can configure the `raw-data` attribute (`false` by default). If `raw-data` is `true` no attempts to convert
180180
values will be made and you'll be able to get raw values using `config.getString(key)`.
181181

182+
If you want to select the set of keys to import, use the `keys` attributes. It filters out all non selected keys. Keys
183+
must be listed individually:
184+
185+
[source, java]
186+
----
187+
ConfigStoreOptions json = new ConfigStoreOptions()
188+
.setType("env")
189+
.setConfig(new JsonObject().put("keys", new JsonArray().add("SERVICE1_HOST").add("SERVICE2_HOST")));
190+
----
191+
182192
=== System Properties
183193

184194
This configuration store maps system properties to a Json Object contributed to the

vertx-config/src/main/asciidoc/js/index.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,23 @@ var json = {
212212
You can configure the `raw-data` attribute (`false` by default). If `raw-data` is `true` no attempts to convert
213213
values will be made and you'll be able to get raw values using `config.getString(key)`.
214214

215+
If you want to select the set of keys to import, use the `keys` attributes. It filters out all non selected keys. Keys
216+
must be listed individually:
217+
218+
[source, js]
219+
----
220+
var json = {
221+
"type" : "env",
222+
"config" : {
223+
"keys" : [
224+
"SERVICE1_HOST",
225+
"SERVICE2_HOST"
226+
]
227+
}
228+
};
229+
230+
----
231+
215232
=== System Properties
216233

217234
This configuration store maps system properties to a Json Object contributed to the

vertx-config/src/main/asciidoc/kotlin/index.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,19 @@ var json = ConfigStoreOptions(
200200
You can configure the `raw-data` attribute (`false` by default). If `raw-data` is `true` no attempts to convert
201201
values will be made and you'll be able to get raw values using `config.getString(key)`.
202202

203+
If you want to select the set of keys to import, use the `keys` attributes. It filters out all non selected keys. Keys
204+
must be listed individually:
205+
206+
[source, kotlin]
207+
----
208+
var json = ConfigStoreOptions(
209+
type = "env",
210+
config = json {
211+
obj("keys" to array("SERVICE1_HOST", "SERVICE2_HOST"))
212+
})
213+
214+
----
215+
203216
=== System Properties
204217

205218
This configuration store maps system properties to a Json Object contributed to the

vertx-config/src/main/asciidoc/ruby/index.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,23 @@ json = {
212212
You can configure the `raw-data` attribute (`false` by default). If `raw-data` is `true` no attempts to convert
213213
values will be made and you'll be able to get raw values using `config.getString(key)`.
214214

215+
If you want to select the set of keys to import, use the `keys` attributes. It filters out all non selected keys. Keys
216+
must be listed individually:
217+
218+
[source, ruby]
219+
----
220+
json = {
221+
'type' => "env",
222+
'config' => {
223+
'keys' => [
224+
"SERVICE1_HOST",
225+
"SERVICE2_HOST"
226+
]
227+
}
228+
}
229+
230+
----
231+
215232
=== System Properties
216233

217234
This configuration store maps system properties to a Json Object contributed to the

vertx-config/src/main/java/examples/Examples.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ public void env2() {
8888
.setConfig(new JsonObject().put("raw-data", true));
8989
}
9090

91+
public void env3() {
92+
ConfigStoreOptions json = new ConfigStoreOptions()
93+
.setType("env")
94+
.setConfig(new JsonObject().put("keys", new JsonArray().add("SERVICE1_HOST").add("SERVICE2_HOST")));
95+
}
96+
9197

9298

9399
public void http() {

vertx-config/src/main/java/io/vertx/config/impl/spi/EnvVariablesConfigStore.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
import io.vertx.core.Handler;
88
import io.vertx.core.Vertx;
99
import io.vertx.core.buffer.Buffer;
10+
import io.vertx.core.json.JsonArray;
1011
import io.vertx.core.json.JsonObject;
1112
import io.vertx.config.spi.ConfigStoreFactory;
1213

13-
import java.util.Map;
14+
import java.util.*;
1415

1516
/**
1617
* An implementation of configuration store loading the content from the environment variables.
@@ -21,15 +22,17 @@
2122
*/
2223
public class EnvVariablesConfigStore implements ConfigStoreFactory, ConfigStore {
2324
private final boolean rawData;
25+
private final Set<String> keys;
2426

2527
private JsonObject cached;
2628

2729
public EnvVariablesConfigStore() {
28-
this(false);
30+
this(false, null);
2931
}
3032

31-
public EnvVariablesConfigStore(boolean rawData) {
33+
public EnvVariablesConfigStore(boolean rawData, JsonArray keys) {
3234
this.rawData = rawData;
35+
this.keys = (keys == null) ? null : new HashSet<>(keys.getList());
3336
}
3437

3538
@Override
@@ -39,21 +42,25 @@ public String name() {
3942

4043
@Override
4144
public ConfigStore create(Vertx vertx, JsonObject configuration) {
42-
return new EnvVariablesConfigStore(configuration.getBoolean("raw-data", false));
45+
return new EnvVariablesConfigStore(configuration.getBoolean("raw-data", false), configuration.getJsonArray("keys"));
4346
}
4447

4548
@Override
4649
public void get(Handler<AsyncResult<Buffer>> completionHandler) {
4750
if (cached == null) {
48-
cached = all(System.getenv(), rawData);
51+
cached = all(System.getenv(), rawData, keys);
4952
}
5053
completionHandler.handle(Future.succeededFuture(Buffer.buffer(cached.encode())));
5154
}
5255

53-
private static JsonObject all(Map<String, String> env, boolean rawData) {
56+
private static JsonObject all(Map<String, String> env, boolean rawData, Set<String> keys) {
5457
JsonObject json = new JsonObject();
55-
env.entrySet().stream()
56-
.forEach(entry -> JsonObjectHelper.put(json, entry.getKey(), entry.getValue(), rawData));
58+
Collection localKeys = keys == null ? env.keySet() : keys;
59+
env.forEach((key, value) -> {
60+
if (localKeys.contains(key)) {
61+
JsonObjectHelper.put(json, key, value, rawData);
62+
}
63+
});
5764
return json;
5865
}
5966

vertx-config/src/main/java/io/vertx/config/package-info.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@
150150
* You can configure the `raw-data` attribute (`false` by default). If `raw-data` is `true` no attempts to convert
151151
* values will be made and you'll be able to get raw values using `config.getString(key)`.
152152
*
153+
* If you want to select the set of keys to import, use the `keys` attributes. It filters out all non selected keys. Keys
154+
* must be listed individually:
155+
*
156+
* [source, $lang]
157+
* ----
158+
* {@link examples.Examples#env3()}
159+
* ----
160+
*
153161
* === System Properties
154162
*
155163
* This configuration store maps system properties to a Json Object contributed to the

vertx-config/src/test/java/io/vertx/config/impl/spi/EnvVariablesConfigStoreTest.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.vertx.config.impl.spi;
22

3+
import io.vertx.core.json.JsonArray;
34
import io.vertx.core.json.JsonObject;
45
import io.vertx.ext.unit.Async;
56
import io.vertx.ext.unit.TestContext;
@@ -25,12 +26,44 @@ public void testName() {
2526
}
2627

2728
@Test
28-
public void testLoadingFromSystemProperties(TestContext context) {
29+
public void testLoadingFromEnv(TestContext context) {
2930
Async async = context.async();
3031
getJsonConfiguration(vertx, store, ar -> {
3132
assertThat(ar.succeeded()).isTrue();
3233
assertThat(ar.result().getString("PATH")).isNotNull();
3334
async.complete();
3435
});
3536
}
37+
38+
@Test
39+
public void testLoadingFromEnvWithNullKeySet(TestContext context) {
40+
Async async = context.async();
41+
store = factory.create(vertx, new JsonObject().put("keys", (JsonArray) null));
42+
getJsonConfiguration(vertx, store, ar -> {
43+
assertThat(ar.succeeded()).isTrue();
44+
assertThat(ar.result().getString("PATH")).isNotNull();
45+
async.complete();
46+
});
47+
}
48+
49+
@Test(expected = ClassCastException.class)
50+
public void testLoadingFromEnvWithInvalidKeySet() {
51+
store = factory.create(vertx, new JsonObject().put("keys", "invalid"));
52+
}
53+
54+
/**
55+
* Reproducer for https://github.com/vert-x3/vertx-config/issues/31.
56+
*/
57+
@Test
58+
public void testLoadingFromEnvWithKeySet(TestContext context) {
59+
Async async = context.async();
60+
store = factory.create(vertx, new JsonObject().put("keys", new JsonArray().add("USER").add("HOME")));
61+
getJsonConfiguration(vertx, store, ar -> {
62+
assertThat(ar.succeeded()).isTrue();
63+
assertThat(ar.result().getString("PATH")).isNull();
64+
assertThat(ar.result().getString("USER")).isNotNull();
65+
assertThat(ar.result().getString("HOME")).isNotNull();
66+
async.complete();
67+
});
68+
}
3669
}

0 commit comments

Comments
 (0)
0