Skip to content

Commit 5041c34

Browse files
committed
fixed stuff, added forEach methods.
1 parent 11217bf commit 5041c34

File tree

6 files changed

+90
-7
lines changed

6 files changed

+90
-7
lines changed

build.gradle

Lines changed: 1 addition & 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.0.0'
6+
version = '1.0.1'
77

88
repositories {
99
mavenCentral()

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,16 @@ public Optional<Path> getPath() {
5252
}
5353

5454
public void saveToDisk() {
55-
if (!getPath().orElseThrow(() -> new InscriptError("Attempted to save to disk with a null path")).toFile().exists()) {
56-
throw new InscriptError("Failed to save Inscript - file not found");
55+
if (getPath().isEmpty()) {
56+
throw new InscriptError("Attempted to save to disk with a null path");
57+
}
58+
59+
final File file = getPath().get().toFile();
60+
61+
if (!file.exists()) try {
62+
if (!file.createNewFile()) throw new InscriptError("Failed to save " + file + " to disk, couldn't create file.");
63+
} catch (final IOException e) {
64+
throw new InscriptError(e);
5765
}
5866

5967
try (final BufferedWriter writer = Files.newBufferedWriter(getPath().get())) {
@@ -145,9 +153,8 @@ private void writeNode(final @NotNull BufferedWriter writer, final @NotNull Insc
145153
}
146154

147155
public void loadFromDisk() {
148-
if (!getPath().orElseThrow(() -> new InscriptError("Attempted to load from disk with a null path")).toFile().exists()) {
149-
throw new InscriptError("Failed to load Inscript - file not found");
150-
}
156+
if (getPath().isEmpty()) throw new InscriptError("Attempted to load from disk with a null path");
157+
if (!getPath().get().toFile().exists()) return;
151158

152159
try (final BufferedReader reader = Files.newBufferedReader(getPath().get())) {
153160
getEditor().reset();

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,38 @@ default InscriptEditor reset() {
9494
getSection().getChildren().clear();
9595
return this;
9696
}
97+
98+
@NotNull
99+
@CanIgnoreReturnValue
100+
default InscriptEditor forEachSection(final @NotNull Consumer<InscriptEditor> sectionConsumer) {
101+
for (final InscriptNode node : getSection().getChildren()) {
102+
getSection(node.getKey()).ifPresent(sectionConsumer);
103+
}
104+
105+
return this;
106+
}
107+
108+
@NotNull
109+
@CanIgnoreReturnValue
110+
default InscriptEditor forEachScalar(final @NotNull Consumer<ScalarNode<?>> scalarConsumer) {
111+
for (final InscriptNode node : getSection().getChildren()) {
112+
if (node instanceof ScalarNode<?> scalar) scalarConsumer.accept(scalar);
113+
}
114+
115+
return this;
116+
}
117+
118+
@NotNull
119+
@CanIgnoreReturnValue
120+
default InscriptEditor forEach(final @NotNull Consumer<ScalarNode<?>> scalarConsumer, final @NotNull Consumer<InscriptEditor> sectionConsumer) {
121+
for (final InscriptNode node : getSection().getChildren()) {
122+
if (node instanceof ScalarNode<?> scalar) {
123+
scalarConsumer.accept(scalar);
124+
} else if (node instanceof SectionNode section) {
125+
sectionConsumer.accept(new SimpleInscriptEditor(section));
126+
}
127+
}
128+
129+
return this;
130+
}
97131
}

src/main/java/dev/manere/inscript/node/ScalarNode.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@
22

33
import org.jetbrains.annotations.NotNull;
44

5+
import java.util.Optional;
6+
57
public abstract class ScalarNode<V> extends InscriptNode {
68
@NotNull
79
public abstract V getValue();
810

11+
@NotNull
12+
public <T> Optional<T> getValueAs(final @NotNull Class<T> type) {
13+
try {
14+
return (Optional<T>) type.cast(getValue());
15+
} catch (final Exception e) {
16+
return Optional.empty();
17+
}
18+
}
19+
920
@NotNull
1021
@Override
1122
public String toString() {

src/main/java/dev/manere/inscript/value/ValueRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class ValueRegistry {
1515

1616
static {
1717
REGISTRY = new ValueRegistry()
18+
.register(byte[].class, new ByteArrayValue())
1819
.register(Boolean.class, new BooleanValue())
1920
.register(Byte.class, new ByteValue())
2021
.register(Short.class, new ShortValue())
@@ -25,7 +26,6 @@ public class ValueRegistry {
2526
.register(UUID.class, new UUIDValue())
2627
.register(Character.class, new CharacterValue());
2728

28-
// Ensure String is always registered last.
2929
REGISTRY.register(String.class, new StringValue());
3030
}
3131

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package dev.manere.inscript.value.impl;
2+
3+
import dev.manere.inscript.value.InlineValue;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
import java.util.Base64;
8+
9+
public class ByteArrayValue implements InlineValue<byte[]> {
10+
@Override
11+
public boolean matches(final @NotNull String text) {
12+
return text.startsWith("base64(") || text.endsWith(")");
13+
}
14+
15+
@Override
16+
public byte @Nullable [] deserialize(final @NotNull String text) {
17+
if (!text.startsWith("base64(") && !text.endsWith(")")) return null;
18+
final String content = text.substring(7, text.length() - 1);
19+
20+
try {
21+
return Base64.getDecoder().decode(content);
22+
} catch (final Exception e) {
23+
return null;
24+
}
25+
}
26+
27+
@Override
28+
public @Nullable String serialize(final byte @NotNull [] bytes) {
29+
return "base64(" + Base64.getEncoder().encodeToString(bytes) + ")";
30+
}
31+
}

0 commit comments

Comments
 (0)