Skip to content

Commit 872d306

Browse files
committed
v1.1.5
v1.1.5 - Added inline comments - Added chained keys/paths - Fixed small issues
1 parent 54dc950 commit 872d306

14 files changed

Lines changed: 356 additions & 28 deletions

File tree

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/markdown.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
group = 'dev.manere.inscript'
6-
version = '1.1.4'
6+
version = '1.1.5'
77

88
repositories {
99
mavenCentral()
@@ -14,6 +14,13 @@ dependencies {
1414
annotationProcessor('org.jetbrains:annotations:24.0.0')
1515
implementation('com.google.errorprone:error_prone_annotations:2.36.0')
1616
annotationProcessor('com.google.errorprone:error_prone_annotations:2.36.0')
17+
18+
testImplementation(platform('org.junit:junit-bom:5.10.0'))
19+
testImplementation('org.junit.jupiter:junit-jupiter')
20+
}
21+
22+
test {
23+
useJUnitPlatform()
1724
}
1825

1926
jar {

src/main/java/dev/manere/inscript/ConfigSection.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ default boolean isRoot() {
4444

4545
@NotNull
4646
default Optional<ConfigNode> getNode(final @NotNull String key) {
47+
if (key.contains(".")) {
48+
String[] parts = key.split("\\.");
49+
ConfigSection current = this;
50+
for (int i = 0; i < parts.length - 1; i++) {
51+
Optional<ConfigSection> next = current.getSection(parts[i]);
52+
if (next.isEmpty()) return Optional.empty();
53+
current = next.get();
54+
}
55+
return current.getNode(parts[parts.length - 1]);
56+
}
57+
4758
for (final ConfigNode node : getChildren()) if (node.getKey().equals(key)) return Optional.of(node);
4859
return Optional.empty();
4960
}
@@ -68,6 +79,16 @@ default boolean isScalar(final @NotNull String key) {
6879
@NotNull
6980
@CanIgnoreReturnValue
7081
default ConfigSection section(final @NotNull String key, final @NotNull Consumer<ConfigSection> handler) {
82+
if (key.contains(".")) {
83+
String[] parts = key.split("\\.");
84+
ConfigSection current = this;
85+
for (String part : parts) {
86+
current = current.getSection(part).orElse(current.createSection(part));
87+
}
88+
handler.accept(current);
89+
return this;
90+
}
91+
7192
handler.accept(getSection(key).orElse(createSection(key)));
7293
return this;
7394
}
@@ -93,6 +114,18 @@ default boolean contains(final @NotNull String key) {
93114
@NotNull
94115
@CanIgnoreReturnValue
95116
default ConfigSection unset(final @NotNull String key) {
117+
if (key.contains(".")) {
118+
String[] parts = key.split("\\.");
119+
ConfigSection current = this;
120+
for (int i = 0; i < parts.length - 1; i++) {
121+
Optional<ConfigSection> next = current.getSection(parts[i]);
122+
if (next.isEmpty()) return this;
123+
current = next.get();
124+
}
125+
current.unset(parts[parts.length - 1]);
126+
return this;
127+
}
128+
96129
getNode(key).ifPresent(node -> getSection().getChildren().remove(node));
97130
return this;
98131
}
@@ -141,6 +174,18 @@ default ConfigSection forEach(final @NotNull Consumer<ScalarNode<?>> scalarConsu
141174
@NotNull
142175
@CanIgnoreReturnValue
143176
default ConfigSection comment(final @NotNull String key, final @NotNull Collection<? extends String> comments) {
177+
if (key.contains(".")) {
178+
String[] parts = key.split("\\.");
179+
ConfigSection current = this;
180+
for (int i = 0; i < parts.length - 1; i++) {
181+
Optional<ConfigSection> next = current.getSection(parts[i]);
182+
if (next.isEmpty()) return this;
183+
current = next.get();
184+
}
185+
current.comment(parts[parts.length - 1], comments);
186+
return this;
187+
}
188+
144189
getNode(key).ifPresent(node -> {
145190
node.getComments().clear();
146191
node.getComments().addAll(comments);
@@ -151,6 +196,17 @@ default ConfigSection comment(final @NotNull String key, final @NotNull Collecti
151196

152197
@NotNull
153198
default Collection<String> getComments(final @NotNull String key) {
199+
if (key.contains(".")) {
200+
String[] parts = key.split("\\.");
201+
ConfigSection current = this;
202+
for (int i = 0; i < parts.length - 1; i++) {
203+
Optional<ConfigSection> next = current.getSection(parts[i]);
204+
if (next.isEmpty()) return Set.of();
205+
current = next.get();
206+
}
207+
return current.getComments(parts[parts.length - 1]);
208+
}
209+
154210
final ConfigNode node = getNode(key).orElse(null);
155211
if (node == null) return Set.of();
156212

@@ -163,6 +219,55 @@ default ConfigSection comment(final @NotNull String key, final @NotNull String @
163219
return comment(key, Arrays.asList(comments));
164220
}
165221

222+
@NotNull
223+
@CanIgnoreReturnValue
224+
default ConfigSection inlineComment(final @NotNull String key, final @NotNull Collection<? extends String> comments) {
225+
if (key.contains(".")) {
226+
String[] parts = key.split("\\.");
227+
ConfigSection current = this;
228+
for (int i = 0; i < parts.length - 1; i++) {
229+
Optional<ConfigSection> next = current.getSection(parts[i]);
230+
if (next.isEmpty()) return this;
231+
current = next.get();
232+
}
233+
current.inlineComment(parts[parts.length - 1], comments);
234+
return this;
235+
}
236+
237+
getNode(key).ifPresent(node -> {
238+
node.getInlineComments().clear();
239+
node.getInlineComments().addAll(comments);
240+
});
241+
242+
return this;
243+
}
244+
245+
@NotNull
246+
@CanIgnoreReturnValue
247+
default ConfigSection inlineComment(final @NotNull String key, final @NotNull String @NotNull ... comments) {
248+
return inlineComment(key, Arrays.asList(comments));
249+
}
250+
251+
@NotNull
252+
@Unmodifiable
253+
default Collection<String> getInlineComments(final @NotNull String key) {
254+
if (key.contains(".")) {
255+
String[] parts = key.split("\\.");
256+
ConfigSection current = this;
257+
for (int i = 0; i < parts.length - 1; i++) {
258+
Optional<ConfigSection> next = current.getSection(parts[i]);
259+
if (next.isEmpty()) return Set.of();
260+
current = next.get();
261+
}
262+
return current.getInlineComments(parts[parts.length - 1]);
263+
}
264+
265+
final ConfigNode node = getNode(key).orElse(null);
266+
if (node == null) return Set.of();
267+
268+
return Set.copyOf(node.getInlineComments());
269+
}
270+
166271
@NotNull
167272
default String getKey() {
168273
return getSection().getKey();

src/main/java/dev/manere/inscript/Inscript.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static Inscript newInscript(final @NotNull Path source) {
3434
for (final FileFormat format : FileFormats.FORMATS) {
3535
final Collection<String> extensions = format.getValidFileExtensions();
3636
for (final String extension : extensions) {
37-
if (file.endsWith("." + extension)) {
37+
if (file.toString().endsWith("." + extension)) {
3838
return new Inscript(source, format);
3939
}
4040
}
@@ -61,6 +61,11 @@ public static Inscript newInscript(final @NotNull FileFormat format) {
6161
return new Inscript(null, format);
6262
}
6363

64+
@NotNull
65+
public static Builder builder() {
66+
return new Builder();
67+
}
68+
6469
@NotNull
6570
public static ValueRegistry getRegistry() {
6671
return ValueRegistry.REGISTRY;
@@ -136,4 +141,26 @@ public void loadFromString(final @NotNull String configString) {
136141
throw new InscriptException(e);
137142
}
138143
}
144+
145+
public static class Builder {
146+
private Path path;
147+
private FileFormat format;
148+
149+
public Builder path(Path path) {
150+
this.path = path;
151+
return this;
152+
}
153+
154+
public Builder format(FileFormat format) {
155+
this.format = format;
156+
return this;
157+
}
158+
159+
public Inscript build() {
160+
if (format == null) {
161+
throw new InscriptException("Format must be specified");
162+
}
163+
return new Inscript(path, format);
164+
}
165+
}
139166
}

src/main/java/dev/manere/inscript/SimpleConfigSection.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.jetbrains.annotations.Nullable;
1010

1111
import java.util.*;
12-
import java.util.concurrent.ConcurrentHashMap;
1312

1413
public record SimpleConfigSection(@NotNull SectionNode sectionNode) implements ConfigSection {
1514
@Override
@@ -19,12 +18,32 @@ public record SimpleConfigSection(@NotNull SectionNode sectionNode) implements C
1918

2019
@Override
2120
public @NotNull Optional<ConfigSection> getSection(final @NotNull String key) {
21+
if (key.contains(".")) {
22+
String[] parts = key.split("\\.");
23+
ConfigSection current = this;
24+
for (String part : parts) {
25+
Optional<ConfigSection> next = current.getSection(part);
26+
if (next.isEmpty()) return Optional.empty();
27+
current = next.get();
28+
}
29+
return Optional.of(current);
30+
}
31+
2232
final ConfigNode node = getNode(key).orElse(null);
2333
return !(node instanceof SectionNode childSection) ? Optional.empty() : Optional.of(new SimpleConfigSection(childSection));
2434
}
2535

2636
@Override
2737
public @NotNull ConfigSection createSection(final @NotNull String key) {
38+
if (key.contains(".")) {
39+
String[] parts = key.split("\\.");
40+
ConfigSection current = this;
41+
for (String part : parts) {
42+
current = current.getSection(part).orElse(current.createSection(part));
43+
}
44+
return current;
45+
}
46+
2847
if (key.equalsIgnoreCase(InscriptConstants.ROOT_SECTION_KEY.getValue())) throw new IllegalArgumentException("Illegal attempt to create a root section.");
2948

3049
final Optional<ConfigSection> sectionFound = getSection(key);
@@ -50,6 +69,17 @@ public String getKey() {
5069

5170
@Override
5271
public @NotNull <T> Optional<T> get(final @NotNull String key, final @NotNull Class<? extends T> ignoredType) {
72+
if (key.contains(".")) {
73+
String[] parts = key.split("\\.");
74+
ConfigSection current = this;
75+
for (int i = 0; i < parts.length - 1; i++) {
76+
Optional<ConfigSection> next = current.getSection(parts[i]);
77+
if (next.isEmpty()) return Optional.empty();
78+
current = next.get();
79+
}
80+
return current.get(parts[parts.length - 1], ignoredType);
81+
}
82+
5383
final ConfigNode node = getNode(key).orElse(null);
5484
if (node == null) return Optional.empty();
5585

@@ -75,6 +105,17 @@ public String getKey() {
75105

76106
@Override
77107
public @NotNull <T> List<T> getList(final @NotNull String key, final @NotNull Class<? extends T> ignoredType) {
108+
if (key.contains(".")) {
109+
String[] parts = key.split("\\.");
110+
ConfigSection current = this;
111+
for (int i = 0; i < parts.length - 1; i++) {
112+
Optional<ConfigSection> next = current.getSection(parts[i]);
113+
if (next.isEmpty()) return Collections.synchronizedList(new ArrayList<>());
114+
current = next.get();
115+
}
116+
return current.getList(parts[parts.length - 1], ignoredType);
117+
}
118+
78119
final ConfigNode node = getNode(key).orElse(null);
79120
if (node == null) return Collections.synchronizedList(new ArrayList<>());
80121
if (!(node instanceof ScalarNode<?> scalar)) return Collections.synchronizedList(new ArrayList<>());
@@ -88,6 +129,16 @@ public String getKey() {
88129

89130
@Override
90131
public @NotNull <T> ConfigSection set(final @NotNull String key, final @Nullable T value) {
132+
if (key.contains(".")) {
133+
String[] parts = key.split("\\.");
134+
ConfigSection current = this;
135+
for (int i = 0; i < parts.length - 1; i++) {
136+
current = current.getSection(parts[i]).orElse(current.createSection(parts[i]));
137+
}
138+
current.set(parts[parts.length - 1], value);
139+
return this;
140+
}
141+
91142
unset(key);
92143
if (value == null) return this;
93144

0 commit comments

Comments
 (0)