Skip to content

Commit

Permalink
Field names customization
Browse files Browse the repository at this point in the history
Allows the customization of the name of the
fields to fetch from the database.

Signed-off-by: Thibault Meyer <meyer.thibault@gmail.com>
  • Loading branch information
thibaultmeyer committed Apr 5, 2018
1 parent 856b4bb commit 5b0068a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ remote-configuration {
table = ""
table = ${?REMOTECONF_DYNAMODB_TABLE}
# Name of the field containing the key
field-key = "key"
field-key = ${?REMOTECONF_DYNAMODB_FIELDKEY}
# Name of the field containing the value
field-value = "value"
field-value = ${?REMOTECONF_DYNAMODB_FIELDVALUE}
# Prefix. Get only values with key beginning
# with the configured prefix
prefix = ""
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/io/playrconf/provider/DynamoDbProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public void loadData(final Config config,
final String accessSecret = config.getString("access-secret");
final String region = config.getString("region");
final String tableName = config.getString("table");
final String fieldKey = config.getString("field-key");
final String fieldValue = config.getString("field-value");
final String prefix = config.hasPath("prefix") ? config.getString("prefix") : "";
final String separator = config.hasPath("separator") ? config.getString("separator") : ".";

Expand All @@ -103,6 +105,10 @@ public void loadData(final Config config,
throw new ConfigException.BadValue(config.origin(), "access-secret", "Required");
} else if (region == null || region.isEmpty()) {
throw new ConfigException.BadValue(config.origin(), "region", "Required");
} else if (fieldKey == null || fieldKey.isEmpty()) {
throw new ConfigException.BadValue(config.origin(), "field-key", "Required");
} else if (fieldValue == null || fieldValue.isEmpty()) {
throw new ConfigException.BadValue(config.origin(), "field-value", "Required");
} else if (separator == null || separator.isEmpty()) {
throw new ConfigException.BadValue(config.origin(), "separator", "Required");
}
Expand All @@ -120,14 +126,14 @@ public void loadData(final Config config,
try {
// Retrieve items from DynamoDB
final List<Map<String, AttributeValue>> items = amazonDynamoDBClient
.scan(tableName, Arrays.asList("key", "value"))
.scan(tableName, Arrays.asList(fieldKey, fieldValue))
.getItems();

// Retrieve all available objects as "Key -> Value"
Tuple2<String, Object> currentTuple = new Tuple2<>();
for (final Map<String, AttributeValue> item : items) {
for (final Map.Entry<String, AttributeValue> data : item.entrySet()) {
if (data.getKey().compareTo("key") == 0) {
if (data.getKey().compareTo(fieldKey) == 0) {
String cfgKey = data
.getValue()
.getS()
Expand All @@ -137,7 +143,7 @@ public void loadData(final Config config,
cfgKey = cfgKey.substring(1);
}
currentTuple.setLeft(cfgKey);
} else if (data.getKey().compareTo("value") == 0) {
} else if (data.getKey().compareTo(fieldValue) == 0) {
switch (data.getValue().toString().substring(1, 2)) {
case "B": {
currentTuple.setRight(data.getValue().getBOOL());
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ remote-configuration {
table = ""
table = ${?REMOTECONF_DYNAMODB_TABLE}

# Name of the field containing the key
field-key = "key"
field-key = ${?REMOTECONF_DYNAMODB_FIELDKEY}

# Name of the field containing the value
field-value = "value"
field-value = ${?REMOTECONF_DYNAMODB_FIELDVALUE}

# Prefix. Get only values with key beginning
# with the configured prefix
prefix = ""
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/io/playrconf/provider/DynamoDbProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ private Config getInitialConfiguration(final String prefix) {
+ "dynamodb.access-secret = \"" + System.getenv("REMOTECONF_DYNAMODB_SECRET") + "\"\n"
+ "dynamodb.region = \"" + System.getenv("REMOTECONF_DYNAMODB_REGION") + "\"\n"
+ "dynamodb.table = \"play-rconf-continuous-integration\"\n"
+ "dynamodb.field-key = \"key\"\n"
+ "dynamodb.field-value = \"value\"\n"
+ "dynamodb.prefix = \"" + prefix + "\"\n"
+ "dynamodb.separator = \".\"\n"
);
Expand Down

0 comments on commit 5b0068a

Please sign in to comment.