Skip to content

Commit

Permalink
Merge pull request #513 from rsksmart/migration_tool_new_keys
Browse files Browse the repository at this point in the history
Add support for new configuration keys with default value
  • Loading branch information
aeidelman authored Mar 19, 2018
2 parents cdedf67 + 6343964 commit c11d663
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 12 additions & 1 deletion rskj-core/src/main/java/co/rsk/cli/config/Migrator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

public class Migrator {

private static final String NEW_CONFIG_PREFIX = "[new]";
private static final String MINIMAL_CONFIG_FORMAT = "{%s = %s}";

private final MigratorConfiguration configuration;

public Migrator(MigratorConfiguration configuration) {
Expand All @@ -31,7 +34,15 @@ public static String migrateConfiguration(Reader sourceReader, Properties migrat
Enumeration migrationPaths = migrationConfiguration.propertyNames();
while (migrationPaths.hasMoreElements()) {
String originalPath = (String) migrationPaths.nextElement();
if (migratedConfig.hasPath(originalPath)) {
if (originalPath.startsWith(NEW_CONFIG_PREFIX)) {
try {
String newConfigPath = originalPath.substring(NEW_CONFIG_PREFIX.length()).trim();
Config newConfiguration = ConfigFactory.parseString(String.format(MINIMAL_CONFIG_FORMAT, newConfigPath, migrationConfiguration.getProperty(originalPath)));
migratedConfig = migratedConfig.withFallback(newConfiguration);
} catch (ConfigException e) {
throw new IllegalArgumentException(String.format("Unable to parse value for the %s property", originalPath), e);
}
} else if (migratedConfig.hasPath(originalPath)) {
ConfigValue configurationValueToMigrate = migratedConfig.getValue(originalPath);
migratedConfig = migratedConfig.withValue(migrationConfiguration.getProperty(originalPath), configurationValueToMigrate).withoutPath(originalPath);
}
Expand Down
4 changes: 4 additions & 0 deletions rskj-core/src/test/java/co/rsk/cli/config/MigratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public void migrateConfiguration() {
migrationProperties.put("inline.config.name", "inline.config.new.name");
migrationProperties.put("nested.nested.config", "nested.nested.new.config");
migrationProperties.put("unknown.config", "none");
migrationProperties.put("[new]new.key", "new value");
migrationProperties.put("[new] other.new.key", "12");

String migratedConfiguration = Migrator.migrateConfiguration(initialConfiguration, migrationProperties);
Config config = ConfigFactory.parseString(migratedConfiguration);
Expand All @@ -37,5 +39,7 @@ public void migrateConfiguration() {
assertThat(config.getInt("nested.nested.new.config"), is(13));
assertThat(config.hasPath("unknown.config"), is(false));
assertThat(config.getString("another.config"), is("don't change"));
assertThat(config.getString("new.key"), is("new value"));
assertThat(config.getInt("other.new.key"), is(12));
}
}

0 comments on commit c11d663

Please sign in to comment.