Skip to content

Commit a2df648

Browse files
committed
v1.1.0
Huge changes.
1 parent f0a97c7 commit a2df648

20 files changed

+1038
-431
lines changed

README.md

Lines changed: 136 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Inscript
2-
A simple, easy and very configurable configuration language.
2+
A simple, easy configuration file library that supports multiple formats.
33
---
44
## Default Data Types:
55
| Data Type | Java | Usage Example |
@@ -14,7 +14,7 @@ A simple, easy and very configurable configuration language.
1414
| Long | Long | `100000L` |
1515
| Character | Character | `'A'C` |
1616
| UUID | UUID | `uuid(4ad4c78c-d4a4-4d25-91cf-4f001efc46c0)` |
17-
| Byte Array | byte[] (Uses Base64) | `base64(base64ValueHere...)` |
17+
| Byte Array | byte[] (Uses Base64) | `base64(base64ValueHere...)` |
1818
---
1919
## Custom Data Types
2020
You can register your own data types and also a custom section parser
@@ -64,45 +64,147 @@ registry.register(Ticket.class, InscriptValue.<Ticket>builder()
6464
```
6565
And we're done!
6666
---
67-
## Configuring the language
68-
If you want to change stuff like defining a list, or just want to change the indent, you can refer to the InscriptConstants interface.
67+
## Inscript Constants
68+
If you want to change the default indent or the root section node key you can do that easily.
6969
```java
70-
InscriptConstants.INDENT.set(" "::repeat);
70+
InscriptConstants.INDENT.set(" "::repeat);
71+
InscriptConstants.ROOT_SECTION_KEY.set("*");
7172
72-
InscriptConstants.LIST_START.set("List(");
73-
InscriptConstants.LIST_END.set(")");
73+
InscriptConstants.VERSION.get().get().ifPresent((version) -> {
74+
System.out.println("Running Inscript version `" + version + "`");
75+
});
7476
```
7577
---
7678
## Using Inscript
77-
You can load/save contents from/to files, or just load/save contents from/to strings.
78-
If you want to use files, you have to provide a `Path` or a `File`.
79+
**By default, we offer 2 file format implementations:**
80+
1. YAML/YML (.yml) `FileFormats.YAML`
81+
2. DataScript (.ds) `FileFormats.DATASCRIPT`
82+
83+
Inscript doesn't actually need a file to work. You can use it perfectly fine with just strings.
84+
85+
### Getting a new Inscript instance:
86+
```java
87+
// Auto-detect the file format from the file's extension
88+
final Inscript autoDetect = Inscript.newInscript(Path.of("file.yml"));
89+
90+
// Manually provide a file format
91+
final Inscript manual = Inscript.newInscript(FileFormats.DATASCRIPT, Path.of("file.ds"));
92+
93+
// Can't use file I/O features, must use saveToString and loadFromString. You must specify the file format.
94+
final Inscript empty = Inscript.newInscript(FileFormats.YAML);
95+
```
96+
### Saving and Loading
97+
Now, the function you should call depends on the source.
98+
If you haven't specified a Path or File in Inscript#newInscript, that means that you can't use `loadFromDisk()` and `saveToDisk()`.
99+
```java
100+
final Inscript inscript = /* ... */;
101+
102+
// Only use if you are sure you provided a path to Inscript.newInscript()
103+
inscript.loadFromDisk();
104+
```
105+
### Node Editor
106+
There are 3 node implementations:
107+
1. `ScalarNode` - a mapping of a key to a value.
108+
2. `SectionNode` - holds a bunch of children nodes.
109+
3. `RootSectionNode` - a default implementation of SectionNode with a specific root key and an empty modifiable list of children nodes.
110+
111+
Every editor is a `SectionNode` wrapped in a **`ConfigSection`**
112+
A ConfigSection is a chainable/fluent builder interface that has the following methods:
79113
```java
80-
final Path path = Path.of(/* ... */);
81-
final Inscript inscript = Inscript.newInscript(path);
114+
@NotNull
115+
@Unmodifiable
116+
Set<ConfigNode> getChildren();
117+
118+
@NotNull
119+
@Unmodifiable
120+
Set<String> getKeys();
121+
122+
boolean isRoot();
123+
124+
@NotNull
125+
Optional<ConfigNode> getNode(final @NotNull String key);
126+
127+
boolean isSection(final @NotNull String key);
128+
129+
boolean isScalar(final @NotNull String key);
130+
131+
@NotNull
132+
SectionNode getSection();
133+
134+
@NotNull
135+
Optional<ConfigSection> getSection(final @NotNull String key);
136+
137+
@NotNull
138+
ConfigSection createSection(final @NotNull String key);
139+
140+
@NotNull
141+
@CanIgnoreReturnValue
142+
ConfigSection section(final @NotNull String key, final @NotNull Consumer<ConfigSection> handler);
143+
144+
@NotNull
145+
<T> Optional<T> get(final @NotNull String key, final @NotNull Class<? extends T> ignoredType);
146+
147+
@NotNull
148+
<T> List<T> getList(final @NotNull String key, final @NotNull Class<? extends T> ignoredType);
149+
150+
@NotNull
151+
@CanIgnoreReturnValue
152+
<T> ConfigSection set(final @NotNull String key, final @Nullable T value);
153+
154+
boolean has(final @NotNull String key);
155+
156+
boolean contains(final @NotNull String key);
157+
158+
@NotNull
159+
@CanIgnoreReturnValue
160+
ConfigSection unset(final @NotNull String key);
161+
162+
@NotNull
163+
@CanIgnoreReturnValue
164+
ConfigSection reset();
165+
166+
@NotNull
167+
@CanIgnoreReturnValue
168+
ConfigSection forEachSection(final @NotNull Consumer<ConfigSection> sectionConsumer);
169+
170+
@NotNull
171+
@CanIgnoreReturnValue
172+
ConfigSection forEachScalar(final @NotNull Consumer<ScalarNode<?>> scalarConsumer);
173+
174+
@NotNull
175+
@CanIgnoreReturnValue
176+
ConfigSection forEach(final @NotNull Consumer<ScalarNode<?>> scalarConsumer, final @NotNull Consumer<ConfigSection> sectionConsumer);
177+
178+
@NotNull
179+
@CanIgnoreReturnValue
180+
ConfigSection comment(final @NotNull String key, final @NotNull Collection<? extends String> comments);
181+
182+
@NotNull
183+
Collection<String> getComments(final @NotNull String key);
184+
185+
@NotNull
186+
@CanIgnoreReturnValue
187+
ConfigSection comment(final @NotNull String key, final @NotNull String @NotNull ... comments);
82188
```
83-
If you want to use the latter, you don't need to provide anything:
189+
To access the root section editor, you can use:
84190
```java
85-
final Inscript inscript = Inscript.emptyInscript();
191+
final Inscript inscript = /* ... */;
192+
final ConfigSection root = inscript.getRoot();
86193
```
87194

88-
To obtain the inscript editor, you use `Inscript.getEditor()`.
89-
90-
Loading:
91-
1. Files
92-
```java
93-
inscript.loadFromDisk();
94-
```
95-
2. String
96-
```java
97-
inscript.loadFromString("gender = \"male\"")
98-
```
99-
100-
Saving:
101-
1. Files
102-
```java
103-
inscript.saveToDisk();
104-
```
105-
2. String
106-
```java
107-
final String saved = inscript.saveToString()
108-
```
195+
### Miscellaneous
196+
> ![NOTE]
197+
> By default, Inscript does not run anything asynchronously for you.
198+
199+
> ![CAUTION]
200+
> Any loading and saving operations from Inscript **should be run asynchronously**.
201+
>
202+
> Make sure to run the string based methods (not the disk ones) asynchronously as well.
203+
> They are basically the same as the disk based ones without the file writing and reading.
204+
>
205+
> **Example:**
206+
> ```java
207+
> CompletableFuture.runAsync(inscript::loadFromDisk).thenRun(() -> {
208+
> // ...
209+
> });
210+
> ```

build.gradle

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

55
group = 'dev.manere.inscript'
6-
version = '1.0.2'
6+
version = '1.1.0'
77

88
repositories {
99
mavenCentral()
@@ -18,6 +18,6 @@ dependencies {
1818

1919
jar {
2020
manifest {
21-
attributes('Implementation-Version': project.version)
21+
attributes('Implementation-Version': version)
2222
}
2323
}

src/main/java/dev/manere/inscript/InscriptEditor.java renamed to src/main/java/dev/manere/inscript/ConfigSection.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.manere.inscript;
22

33
import com.google.errorprone.annotations.CanIgnoreReturnValue;
4-
import dev.manere.inscript.node.InscriptNode;
4+
import dev.manere.inscript.node.ConfigNode;
55
import dev.manere.inscript.node.RootSectionNode;
66
import dev.manere.inscript.node.ScalarNode;
77
import dev.manere.inscript.node.SectionNode;
@@ -12,10 +12,10 @@
1212
import java.util.*;
1313
import java.util.function.Consumer;
1414

15-
public interface InscriptEditor {
15+
public interface ConfigSection {
1616
@NotNull
1717
@Unmodifiable
18-
default Set<InscriptNode> getChildren() {
18+
default Set<ConfigNode> getChildren() {
1919
return Set.copyOf(getSection().getChildren());
2020
}
2121

@@ -24,7 +24,7 @@ default Set<InscriptNode> getChildren() {
2424
default Set<String> getKeys() {
2525
final Set<String> keys = new HashSet<>();
2626

27-
for (final InscriptNode node : getChildren()) {
27+
for (final ConfigNode node : getChildren()) {
2828
keys.add(node.getKey());
2929
}
3030

@@ -36,8 +36,8 @@ default boolean isRoot() {
3636
}
3737

3838
@NotNull
39-
default Optional<InscriptNode> getNode(final @NotNull String key) {
40-
for (final InscriptNode node : getChildren()) if (node.getKey().equals(key)) return Optional.of(node);
39+
default Optional<ConfigNode> getNode(final @NotNull String key) {
40+
for (final ConfigNode node : getChildren()) if (node.getKey().equals(key)) return Optional.of(node);
4141
return Optional.empty();
4242
}
4343

@@ -53,14 +53,14 @@ default boolean isScalar(final @NotNull String key) {
5353
SectionNode getSection();
5454

5555
@NotNull
56-
Optional<InscriptEditor> getSection(final @NotNull String key);
56+
Optional<ConfigSection> getSection(final @NotNull String key);
5757

5858
@NotNull
59-
InscriptEditor createSection(final @NotNull String key);
59+
ConfigSection createSection(final @NotNull String key);
6060

6161
@NotNull
6262
@CanIgnoreReturnValue
63-
default InscriptEditor section(final @NotNull String key, final @NotNull Consumer<InscriptEditor> handler) {
63+
default ConfigSection section(final @NotNull String key, final @NotNull Consumer<ConfigSection> handler) {
6464
handler.accept(getSection(key).orElse(createSection(key)));
6565
return this;
6666
}
@@ -73,7 +73,7 @@ default InscriptEditor section(final @NotNull String key, final @NotNull Consume
7373

7474
@NotNull
7575
@CanIgnoreReturnValue
76-
<T> InscriptEditor set(final @NotNull String key, final @Nullable T value);
76+
<T> ConfigSection set(final @NotNull String key, final @Nullable T value);
7777

7878
default boolean has(final @NotNull String key) {
7979
return contains(key);
@@ -85,22 +85,22 @@ default boolean contains(final @NotNull String key) {
8585

8686
@NotNull
8787
@CanIgnoreReturnValue
88-
default InscriptEditor unset(final @NotNull String key) {
88+
default ConfigSection unset(final @NotNull String key) {
8989
getNode(key).ifPresent(node -> getSection().getChildren().remove(node));
9090
return this;
9191
}
9292

9393
@NotNull
9494
@CanIgnoreReturnValue
95-
default InscriptEditor reset() {
95+
default ConfigSection reset() {
9696
getSection().getChildren().clear();
9797
return this;
9898
}
9999

100100
@NotNull
101101
@CanIgnoreReturnValue
102-
default InscriptEditor forEachSection(final @NotNull Consumer<InscriptEditor> sectionConsumer) {
103-
for (final InscriptNode node : getSection().getChildren()) {
102+
default ConfigSection forEachSection(final @NotNull Consumer<ConfigSection> sectionConsumer) {
103+
for (final ConfigNode node : getSection().getChildren()) {
104104
getSection(node.getKey()).ifPresent(sectionConsumer);
105105
}
106106

@@ -109,8 +109,8 @@ default InscriptEditor forEachSection(final @NotNull Consumer<InscriptEditor> se
109109

110110
@NotNull
111111
@CanIgnoreReturnValue
112-
default InscriptEditor forEachScalar(final @NotNull Consumer<ScalarNode<?>> scalarConsumer) {
113-
for (final InscriptNode node : getSection().getChildren()) {
112+
default ConfigSection forEachScalar(final @NotNull Consumer<ScalarNode<?>> scalarConsumer) {
113+
for (final ConfigNode node : getSection().getChildren()) {
114114
if (node instanceof ScalarNode<?> scalar) scalarConsumer.accept(scalar);
115115
}
116116

@@ -119,12 +119,12 @@ default InscriptEditor forEachScalar(final @NotNull Consumer<ScalarNode<?>> scal
119119

120120
@NotNull
121121
@CanIgnoreReturnValue
122-
default InscriptEditor forEach(final @NotNull Consumer<ScalarNode<?>> scalarConsumer, final @NotNull Consumer<InscriptEditor> sectionConsumer) {
123-
for (final InscriptNode node : getSection().getChildren()) {
122+
default ConfigSection forEach(final @NotNull Consumer<ScalarNode<?>> scalarConsumer, final @NotNull Consumer<ConfigSection> sectionConsumer) {
123+
for (final ConfigNode node : getSection().getChildren()) {
124124
if (node instanceof ScalarNode<?> scalar) {
125125
scalarConsumer.accept(scalar);
126126
} else if (node instanceof SectionNode section) {
127-
sectionConsumer.accept(new SimpleInscriptEditor(section));
127+
sectionConsumer.accept(new SimpleConfigSection(section));
128128
}
129129
}
130130

@@ -133,7 +133,7 @@ default InscriptEditor forEach(final @NotNull Consumer<ScalarNode<?>> scalarCons
133133

134134
@NotNull
135135
@CanIgnoreReturnValue
136-
default InscriptEditor setComments(final @NotNull String key, final @NotNull Collection<? extends String> comments) {
136+
default ConfigSection comment(final @NotNull String key, final @NotNull Collection<? extends String> comments) {
137137
getNode(key).ifPresent(node -> {
138138
node.getComments().clear();
139139
node.getComments().addAll(comments);
@@ -144,15 +144,15 @@ default InscriptEditor setComments(final @NotNull String key, final @NotNull Col
144144

145145
@NotNull
146146
default Collection<String> getComments(final @NotNull String key) {
147-
final InscriptNode node = getNode(key).orElse(null);
147+
final ConfigNode node = getNode(key).orElse(null);
148148
if (node == null) return Set.of();
149149

150150
return Set.copyOf(node.getComments());
151151
}
152152

153153
@NotNull
154154
@CanIgnoreReturnValue
155-
default InscriptEditor setComments(final @NotNull String key, final @NotNull String @NotNull ... comments) {
156-
return setComments(key, Arrays.asList(comments));
155+
default ConfigSection comment(final @NotNull String key, final @NotNull String @NotNull ... comments) {
156+
return comment(key, Arrays.asList(comments));
157157
}
158158
}

0 commit comments

Comments
 (0)