Skip to content

Commit e51bb7e

Browse files
committed
Added new ConfigRenderOptions entry to control whether to sort value in objects.
1 parent 9c39077 commit e51bb7e

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

config/src/main/java/com/typesafe/config/ConfigRenderOptions.java

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ public final class ConfigRenderOptions {
2121
private final boolean comments;
2222
private final boolean formatted;
2323
private final boolean json;
24+
private final boolean sortObjects;
2425

2526
private ConfigRenderOptions(boolean originComments, boolean comments, boolean formatted,
26-
boolean json) {
27+
boolean json, boolean sortObjects) {
2728
this.originComments = originComments;
2829
this.comments = comments;
2930
this.formatted = formatted;
3031
this.json = json;
32+
this.sortObjects = sortObjects;
3133
}
3234

3335
/**
@@ -38,7 +40,7 @@ private ConfigRenderOptions(boolean originComments, boolean comments, boolean fo
3840
* @return the default render options
3941
*/
4042
public static ConfigRenderOptions defaults() {
41-
return new ConfigRenderOptions(true, true, true, true);
43+
return new ConfigRenderOptions(true, true, true, true, true);
4244
}
4345

4446
/**
@@ -48,7 +50,7 @@ public static ConfigRenderOptions defaults() {
4850
* @return the concise render options
4951
*/
5052
public static ConfigRenderOptions concise() {
51-
return new ConfigRenderOptions(false, false, false, true);
53+
return new ConfigRenderOptions(false, false, false, true, false);
5254
}
5355

5456
/**
@@ -64,7 +66,7 @@ public ConfigRenderOptions setComments(boolean value) {
6466
if (value == comments)
6567
return this;
6668
else
67-
return new ConfigRenderOptions(originComments, value, formatted, json);
69+
return new ConfigRenderOptions(originComments, value, formatted, json, sortObjects);
6870
}
6971

7072
/**
@@ -97,7 +99,7 @@ public ConfigRenderOptions setOriginComments(boolean value) {
9799
if (value == originComments)
98100
return this;
99101
else
100-
return new ConfigRenderOptions(value, comments, formatted, json);
102+
return new ConfigRenderOptions(value, comments, formatted, json, sortObjects);
101103
}
102104

103105
/**
@@ -122,7 +124,7 @@ public ConfigRenderOptions setFormatted(boolean value) {
122124
if (value == formatted)
123125
return this;
124126
else
125-
return new ConfigRenderOptions(originComments, comments, value, json);
127+
return new ConfigRenderOptions(originComments, comments, value, json, sortObjects);
126128
}
127129

128130
/**
@@ -150,7 +152,7 @@ public ConfigRenderOptions setJson(boolean value) {
150152
if (value == json)
151153
return this;
152154
else
153-
return new ConfigRenderOptions(originComments, comments, formatted, value);
155+
return new ConfigRenderOptions(originComments, comments, formatted, value, sortObjects);
154156
}
155157

156158
/**
@@ -163,6 +165,32 @@ public boolean getJson() {
163165
return json;
164166
}
165167

168+
/**
169+
* Returns options with object sorting enabled. Object sorting means that
170+
* child values of object nodes will be processed in order of their key
171+
* values, i.e. the keys will be sorted before being iterated over.
172+
*
173+
* @param value
174+
* true to sort object values in the render
175+
* @return options with the requested setting for object sorting
176+
*/
177+
public ConfigRenderOptions setSortObjects(boolean value) {
178+
if (value == sortObjects)
179+
return this;
180+
else
181+
return new ConfigRenderOptions(originComments, comments, formatted, json, value);
182+
}
183+
184+
/**
185+
* Returns whether the options enable object key sorting. This method is
186+
* mostly used by the config lib internally, not by applications.
187+
*
188+
* @return true if objects should be sorted by their keys.
189+
*/
190+
public boolean getSortObjects() {
191+
return sortObjects;
192+
}
193+
166194
@Override
167195
public String toString() {
168196
StringBuilder sb = new StringBuilder("ConfigRenderOptions(");
@@ -174,6 +202,8 @@ public String toString() {
174202
sb.append("formatted,");
175203
if (json)
176204
sb.append("json,");
205+
if (sortObjects)
206+
sb.append("sortObjects,");
177207
if (sb.charAt(sb.length() - 1) == ',')
178208
sb.setLength(sb.length() - 1);
179209
sb.append(")");

config/src/main/java/com/typesafe/config/impl/SimpleConfigObject.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,15 @@ protected void render(StringBuilder sb, int indent, boolean atRoot, ConfigRender
382382
}
383383

384384
int separatorCount = 0;
385-
for (String k : keySet()) {
385+
Iterable<String> keys;
386+
if (options.getSortObjects()) {
387+
List<String> list = new ArrayList<String>(keySet());
388+
Collections.sort(list);
389+
keys = list;
390+
} else {
391+
keys = keySet();
392+
}
393+
for (String k : keys) {
386394
AbstractConfigValue v;
387395
v = value.get(k);
388396

0 commit comments

Comments
 (0)