Skip to content

Commit 070d755

Browse files
committed
feat: Basic hocon implementation using a legacy version of typesafe config
1 parent 151b857 commit 070d755

File tree

7 files changed

+168
-4
lines changed

7 files changed

+168
-4
lines changed

hocon-legacy/build.gradle.kts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
plugins {
2+
jaskl
3+
}
4+
5+
dependencies {
6+
api(project(":hocon"))
7+
implementation("com.typesafe:config:1.2.1")
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* JASKL - Just Another Simple Konfig Library
3+
* Copyright (C) 2023 LeStegii, Almighty-Satan
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
*/
20+
21+
package io.github.almightysatan.jaskl.hocon;
22+
23+
import com.typesafe.config.*;
24+
import io.github.almightysatan.jaskl.impl.Util;
25+
import io.github.almightysatan.jaskl.impl.WritableConfigEntry;
26+
import org.jetbrains.annotations.NotNull;
27+
import org.jetbrains.annotations.Nullable;
28+
29+
import java.io.File;
30+
import java.io.FileWriter;
31+
import java.io.IOException;
32+
import java.math.BigDecimal;
33+
import java.math.BigInteger;
34+
35+
/**
36+
* A hocon config implementation.
37+
*/
38+
public class HoconLegacyConfig extends HoconConfig {
39+
40+
protected HoconLegacyConfig(@NotNull File file, @Nullable String description) {
41+
super(file, description);
42+
}
43+
44+
@Override
45+
public void write() throws IOException {
46+
Config config = super.config;
47+
if (config == null)
48+
throw new IllegalStateException();
49+
Util.createFileAndPath(this.file);
50+
51+
for (WritableConfigEntry<?> configEntry : this.getCastedValues()) {
52+
if (configEntry.isModified()) {
53+
Object entryValue = configEntry.getValueToWrite(Object::toString);
54+
if (entryValue instanceof BigInteger)
55+
entryValue = entryValue.toString();
56+
if (entryValue instanceof BigDecimal)
57+
entryValue = entryValue.toString();
58+
ConfigValue value = ConfigValueFactory.fromAnyRef(entryValue);
59+
config = config.withValue(configEntry.getPath(), value);
60+
}
61+
}
62+
63+
this.writeIfNecessary(config, true);
64+
}
65+
66+
protected void writeIfNecessary(@NotNull Config config, boolean setDescription) throws IOException {
67+
if (config != super.config) {
68+
ConfigObject root = config.root();
69+
String output = root.render(RENDER_OPTIONS);
70+
try (FileWriter fileWriter = new FileWriter(this.file)) {
71+
fileWriter.write(output);
72+
}
73+
super.config = config;
74+
}
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* JASKL - Just Another Simple Konfig Library
3+
* Copyright (C) 2023 LeStegii, Almighty-Satan
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
*/
20+
21+
package io.github.almightysatan.jaskl.hocon;
22+
23+
import io.github.almightysatan.jaskl.Config;
24+
import io.github.almightysatan.jaskl.test.ConfigTest;
25+
26+
import java.io.File;
27+
28+
public class HoconLegacyConfigTest extends ConfigTest {
29+
30+
private static final File FILE_EMPTY = new File("src/test/resources/empty.hocon");
31+
private static final File FILE_EXAMPLE = new File("src/test/resources/example.hocon");
32+
private static final File FILE_TEST = new File("build/tmp/test/test.hocon");
33+
34+
@Override
35+
protected Config createEmptyConfig() {
36+
return HoconLegacyConfig.of(FILE_EMPTY);
37+
}
38+
39+
@Override
40+
protected Config createExampleConfig() {
41+
return HoconLegacyConfig.of(FILE_EXAMPLE);
42+
}
43+
44+
@Override
45+
protected Config createTestConfig() {
46+
return HoconLegacyConfig.of(FILE_TEST, "Test Hocon config");
47+
}
48+
49+
@Override
50+
protected void clearTestConfig() {
51+
FILE_TEST.delete();
52+
}
53+
54+
@Override
55+
protected boolean testConfigExists() {
56+
return FILE_TEST.exists();
57+
}
58+
}

hocon-legacy/src/test/resources/empty.hocon

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
example = {
3+
boolean = true,
4+
double = 1.0,
5+
float = 1.0,
6+
integer = 1,
7+
long = 1,
8+
string = modified,
9+
special-char_entry = spe-ci_al,
10+
enum = ANOTHER_EXAMPLE,
11+
list = [
12+
Example3,
13+
Example4
14+
]
15+
map = {
16+
Hello = there,
17+
abc = xyz,
18+
x = y
19+
}
20+
}
21+
}

hocon/src/main/java/io/github/almightysatan/jaskl/hocon/HoconConfig.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public class HoconConfig extends ConfigImpl {
4242

4343
private static final ConfigParseOptions PARSE_OPTIONS = ConfigParseOptions.defaults().setSyntax(ConfigSyntax.CONF)
4444
.setAllowMissing(false).setIncluder(new NopIncluder());
45-
private static final ConfigRenderOptions RENDER_OPTIONS = ConfigRenderOptions.defaults().setJson(false).setOriginComments(false);
45+
protected static final ConfigRenderOptions RENDER_OPTIONS = ConfigRenderOptions.defaults().setJson(false).setOriginComments(false);
4646

47-
private final File file;
48-
private Config config;
47+
protected final File file;
48+
protected Config config;
4949

50-
private HoconConfig(@NotNull File file, @Nullable String description) {
50+
protected HoconConfig(@NotNull File file, @Nullable String description) {
5151
super(description);
5252
this.file = Objects.requireNonNull(file);
5353
}

settings.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ include("core")
33
include("jackson")
44
include("toml")
55
include("hocon")
6+
include("hocon-legacy")
67
include("properties")
78
include("mongodb")
89
include("json")

0 commit comments

Comments
 (0)