Skip to content

Commit ebf6126

Browse files
committed
Fixes issue #14
1 parent 611f593 commit ebf6126

4 files changed

Lines changed: 31 additions & 5 deletions

File tree

library/src/main/java/io/codearte/props2yaml/Props2YAML.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,14 @@ public static Props2YAML fromFile(Path path) {
4646
return new Props2YAML(path.toFile());
4747
}
4848

49-
public String convert() {
50-
PropertyTree tree = new TreeBuilder(properties).build();
49+
public String convert(boolean useNumericKeysAsArrayIndexes) {
50+
PropertyTree tree = new TreeBuilder(properties,useNumericKeysAsArrayIndexes).build();
5151
tree = new ArrayProcessor(tree).apply();
5252
return tree.toYAML();
5353
}
54+
public String convert() {
55+
return convert(true);
56+
}
5457

5558
private void reportError(IOException e) {
5659
LOG.error("Conversion failed", e);

library/src/main/java/io/codearte/props2yaml/TreeBuilder.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ class TreeBuilder {
1616

1717
private final Properties properties;
1818
private final static Pattern pattern = Pattern.compile("[0-9]+");
19+
private final boolean useNumericKeysAsArrayIndexes;
1920

20-
public TreeBuilder(Properties properties) {
21+
public TreeBuilder(Properties properties, boolean useNumericKeysAsArrayIndexes) {
2122
this.properties = properties;
23+
this.useNumericKeysAsArrayIndexes = useNumericKeysAsArrayIndexes;
24+
}
25+
26+
public TreeBuilder(Properties properties) {
27+
this(properties,true);
2228
}
2329

2430
public PropertyTree build() {
@@ -34,7 +40,7 @@ public PropertyTree build() {
3440

3541
private List<String> splitPropertyName(String property) {
3642
List<String> strings = Arrays.asList(property.split("\\."));
37-
List<String> result = applyArrayNotation(strings);
43+
List<String> result = useNumericKeysAsArrayIndexes ? applyArrayNotation(strings) : strings;
3844
Collections.reverse(result);
3945
return result;
4046
}

library/src/test/groovy/io/codearte/props2yaml/KeyConvertingSpec.groovy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,18 @@ class KeyConvertingSpec extends Specification {
4141
yaml != ~'a: 1'
4242
}
4343

44+
def "Should read numeric subkeys"() {
45+
given:
46+
String props = '''some.1.key = somevalue
47+
some.1.key2 = somevalue2'''
48+
when:
49+
String yaml = new Props2YAML(props).convert(false);
50+
System.out.println( yaml );
51+
then:
52+
yaml =~ 'some:'
53+
yaml =~ '\'1\':'
54+
yaml =~ 'key: somevalue'
55+
yaml =~ 'key2: somevalue2'
56+
}
57+
4458
}

maven-plugin/src/main/java/io/codearte/props2yaml/maven/ConvertMojo.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public class ConvertMojo extends AbstractMojo {
2222
@Parameter(property = "properties", required = false)
2323
private String properties;
2424

25+
@Parameter(property = "useNumericKeysAsArrayIndexes", required = false, defaultValue = "true")
26+
private boolean useNumericKeysAsArrayIndexes;
27+
2528
@Override
2629
public void execute() throws MojoExecutionException, MojoFailureException {
2730
if (properties == null || properties.isEmpty()) {
@@ -32,7 +35,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
3235
try {
3336
getLog().info("Properties to convert: " + propertiesPath);
3437
String content = new String(Files.readAllBytes(propertiesPath));
35-
String yaml = Props2YAML.fromContent(content).convert();
38+
String yaml = Props2YAML.fromContent(content).convert(useNumericKeysAsArrayIndexes);
3639
Path destinationPath = propertiesPath.getParent().resolve(getFileName());
3740
getLog().info("Write YAML to: " + destinationPath);
3841
try (BufferedWriter writer = Files.newBufferedWriter(destinationPath)) {

0 commit comments

Comments
 (0)