Skip to content
This repository has been archived by the owner on Oct 28, 2023. It is now read-only.

Commit

Permalink
0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Flamarine committed Dec 23, 2022
1 parent e3d5f15 commit d4013aa
Show file tree
Hide file tree
Showing 13 changed files with 320 additions and 55 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: build
on: [pull_request, push]

jobs:
build:
strategy:
matrix:
# Use these Java versions
java: [17]
os: [ubuntu-20.04]
runs-on: ${{ matrix.os }}
steps:
- name: checkout repository
uses: actions/checkout@v2
- name: validate gradle wrapper
uses: gradle/wrapper-validation-action@v1
- name: setup jdk ${{ matrix.java }}
uses: actions/setup-java@v1
with:
java-version: ${{ matrix.java }}
- name: make gradle wrapper executable
if: ${{ runner.os != 'Windows' }}
run: chmod +x ./gradlew
- name: build
run: ./gradlew build
- name: capture build artifacts
if: ${{ runner.os == 'Linux' && matrix.java == '17' }} # Only upload artifacts built from latest java on one OS
uses: actions/upload-artifact@v2
with:
name: Artifacts
path: build/libs/
45 changes: 45 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: build
on:
push:
paths:
- 'gradle.properties'

jobs:
build:
strategy:
matrix:
# Use these Java versions
java: [17]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1
- name: Setup jdk ${{ matrix.java }}
uses: actions/setup-java@v1
with:
java-version: ${{ matrix.java }}
- name: Make Gradle Wrapper Executable
if: ${{ runner.os != 'Windows' }}
run: chmod +x ./gradlew
- name: Get Project Version
id: project_version
run: grep "project_version" gradle.properties | sed "s/\s//g" >> $GITHUB_OUTPUT
- name: Publish
run: ./gradlew build publish
env:
MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.project_version.outputs.project_version }}
release_name: Prism Config ${{ steps.project_version.outputs.project_version }}
body_path: CHANGELOG.md
draft: false
prerelease: false
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* Added TOML (0.4.0) serializer/deserializer via toml4j, with comment support.
* Optimized code structure
* Improved Javadoc
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Prism Config

![java8](https://cdn.jsdelivr.net/npm/@intergrav/devins-badges@2/assets/cozy/built-with/java8_vector.svg)
![gradle](https://cdn.jsdelivr.net/npm/@intergrav/devins-badges@2/assets/cozy/built-with/gradle_vector.svg)

A lightweight config library for Java to let you write your config elegantly and flexibly.

### Getting Started
Expand All @@ -21,13 +24,13 @@ repositories {
dependencies {
// ...
implementation "io.github.prismwork:prismconfig:0.1.1"
// Or use the full jar if you don't have the libraries included in your project (Gson, Jankson...)
// implementation "io.github.prismwork:prismconfig:0.1.1:all"
implementation "io.github.prismwork:prismconfig:0.2.0:all"
// Or use the slim jar if you have the libraries included in your project (Gson, Jankson...)
// implementation "io.github.prismwork:prismconfig:0.2.0"
}
```

Prism Config by default provides serializers and deserializers for JSON (Gson) and JSON5 (Jankson).
Prism Config by default provides serializers and deserializers for JSON (Gson), JSON5 (Jankson) and TOML 0.4.0 (toml4j).

To parse a config from string into object, you can do this:

Expand Down
25 changes: 15 additions & 10 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "io.github.prismwork"
version = "0.1.1"
version = property("project_version")!!

val include: Configuration by configurations.creating

Expand All @@ -20,33 +20,38 @@ tasks.named<ShadowJar>("shadowJar") {
configurations = listOf(include)
relocate("blue.endless.jankson", relocatePackage("jankson"))
relocate("com.google.gson", relocatePackage("gson"))
relocate("com.moandjiezana.toml", relocatePackage("toml4j"))
relocate("org.jetbrains.annotations", relocatePackage("jb.annotations"))
relocate("org.intellij.lang.annotations", relocatePackage("ij.annotations"))
}

repositories {
mavenCentral()
maven {
name = "QuiltMC Release"
name = "QuiltMC Release" // quilt-json5, though unused
url = uri("https://maven.quiltmc.org/repository/release/")
}
maven {
name = "QuiltMC Snapshot"
name = "QuiltMC Snapshot" // quilt-json5, though unused
url = uri("https://maven.quiltmc.org/repository/snapshot/")
}
maven {
name = "unascribed" // kdl4j, though unused
url = uri("https://repo.unascribed.com/")
}
}

dependencies {
/* Utilities */
implementation("org.jetbrains:annotations:23.1.0")
include("org.jetbrains:annotations:23.1.0")
include("org.jetbrains:annotations:23.1.0")?.let { implementation(it) }

/* Serialization */
implementation("com.google.code.gson:gson:2.10")
include("com.google.code.gson:gson:2.10")
implementation("blue.endless:jankson:1.2.1")
include("blue.endless:jankson:1.2.1")
// implementation("org.quiltmc:quilt-json5:1.0.2")
include("com.google.code.gson:gson:2.10")?.let { implementation(it) }
include("blue.endless:jankson:1.2.1")?.let { implementation(it) }
include("com.moandjiezana.toml:toml4j:0.7.2")?.let { implementation(it) }
// include("org.yaml:snakeyaml:1.33")?.let { implementation(it) }
// include("dev.hbeck.kdl:kdl4j:0.2.0")?.let { implementation(it) }
// include("org.quiltmc:quilt-json5:1.0.2")?.let { implementation(it) }

/* Test */
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.0")
Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Only used for version bump so that we can tell GitHub Actions when to publish a new version
project_version = 0.2.0
99 changes: 95 additions & 4 deletions src/main/java/io/github/prismwork/prismconfig/api/PrismConfig.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.github.prismwork.prismconfig.api;

import io.github.prismwork.prismconfig.impl.PrismConfigImpl;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.Objects;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -60,6 +62,31 @@ static PrismConfig getInstance() {
*/
<T> T serialize(Class<T> clazz, String content, Function<String, T> serializer);

/**
* Cast the given config content to an instance of the config whose type is specified by the "clazz" parameter and cache the serializer for the given type.
*
* @param clazz the class of the config instance type
* @param file the content of the config as a file
* @param serializer the serializer used to parse the config string
* @param <T> the type of the config instance
* @return an instance of the config
*/
default <T> T serialize(Class<T> clazz, File file, Function<String, T> serializer) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String content = Utils.readFromFile(reader);
return serialize(clazz, content, serializer);
} catch (IOException e) {
try {
return clazz.getDeclaredConstructor().newInstance();
} catch (InvocationTargetException |
InstantiationException |
IllegalAccessException |
NoSuchMethodException ex) {
throw new RuntimeException("Failed to parse config", ex);
}
}
}

/**
* Cast the given config content to an instance of the config whose type is specified by the "clazz" parameter, using the cached serializer.
* <p>If the serializer for this class is not cached, a {@link RuntimeException} is thrown.
Expand All @@ -71,6 +98,30 @@ static PrismConfig getInstance() {
*/
<T> T serializeCached(Class<T> clazz, String content);

/**
* Cast the given config content to an instance of the config whose type is specified by the "clazz" parameter and cache the serializer for the given type.
*
* @param clazz the class of the config instance type
* @param file the content of the config as a file
* @param <T> the type of the config instance
* @return an instance of the config
*/
default <T> T serializeCached(Class<T> clazz, File file) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String content = Utils.readFromFile(reader);
return serializeCached(clazz, content);
} catch (IOException e) {
try {
return clazz.getDeclaredConstructor().newInstance();
} catch (InvocationTargetException |
InstantiationException |
IllegalAccessException |
NoSuchMethodException ex) {
throw new RuntimeException("Failed to parse config", ex);
}
}
}

/**
* Convert the given config instance to a string representing the config content and cache the deserializer for the given type.
*
Expand Down Expand Up @@ -102,7 +153,10 @@ static PrismConfig getInstance() {
* @param file the target file the config is written to
* @param <T> the type of the config instance
*/
<T> void deserializeAndWrite(Class<T> clazz, T content, Function<T, String> deserializer, File file);
default <T> void deserializeAndWrite(Class<T> clazz, T content, Function<T, String> deserializer, File file) {
String string = deserialize(clazz, content, deserializer);
Utils.writeToConfigFile(file, string);
}

/**
* Write the given config instance to the target file as a string representing the config content, using the cached deserializer.
Expand All @@ -113,5 +167,42 @@ static PrismConfig getInstance() {
* @param file the target file the config is written to
* @param <T> the type of the config instance
*/
<T> void deserializeAndWriteCached(Class<T> clazz, T content, File file);
default <T> void deserializeAndWriteCached(Class<T> clazz, T content, File file) {
String string = deserializeCached(clazz, content);
Utils.writeToConfigFile(file, string);
}

@ApiStatus.Internal
class Utils {
private static @NotNull String readFromFile(@NotNull BufferedReader reader) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
String line;
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
reader.close();

return stringBuilder.toString();
}

private static void writeToConfigFile(@NotNull File file, String string) {
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
throw new RuntimeException("Failed to create file", e);
}
}
try (FileWriter writer = new FileWriter(file)) {
writer.write(""); // Empty the file
writer.write(string);
writer.flush();
} catch (IOException e) {
throw new RuntimeException("Failed to write config");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import io.github.prismwork.prismconfig.impl.config.DefaultDeserializersImpl;

import java.util.Objects;
import java.util.function.Function;

/**
* The default deserializers Prism Config provides.
* <p>For now there are three: JSON, JSON5 and TOML (0.4.0).
*
* @since 0.1.0
*/
@SuppressWarnings("unused")
public interface DefaultDeserializers {
/**
Expand All @@ -23,7 +28,7 @@ static DefaultDeserializers getInstance() {
/**
* Returns a JSON deserializer for the given config class.
*
* @param clazz the config class the serializer is going to handle
* @param clazz the config class the deserializer is going to handle
* @param <T> the type of the config class
* @return the JSON deserializer for the given config class
*/
Expand All @@ -32,9 +37,18 @@ static DefaultDeserializers getInstance() {
/**
* Returns a JSON5 deserializer for the given config class.
*
* @param clazz the config class the serializer is going to handle
* @param clazz the config class the deserializer is going to handle
* @param <T> the type of the config class
* @return the JSON5 deserializer for the given config class
*/
<T> Function<T, String> json5(Class<T> clazz);

/**
* Returns a TOML (0.4.0) deserializer for the given config class.
*
* @param clazz the config class the deserializer is going to handle
* @param <T> the type of the config class
* @return the TOML deserializer for the given config class
*/
<T> Function<T, String> toml(Class<T> clazz);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import io.github.prismwork.prismconfig.impl.config.DefaultSerializersImpl;

import java.util.Objects;
import java.util.function.Function;

/**
* The default serializers Prism Config provides.
* <p>For now there are three: JSON, JSON5 and TOML (0.4.0).
*
* @since 0.1.0
*/
@SuppressWarnings("unused")
public interface DefaultSerializers {
/**
Expand Down Expand Up @@ -37,4 +42,13 @@ static DefaultSerializers getInstance() {
* @return the JSON5 serializer for the given config class
*/
<T> Function<String, T> json5(Class<T> clazz);

/**
* Returns a TOML (0.4.0) serializer for the given config class.
*
* @param clazz the config class the serializer is going to handle
* @param <T> the type of the config class
* @return the TOML serializer for the given config class
*/
<T> Function<String, T> toml(Class<T> clazz);
}
Loading

0 comments on commit d4013aa

Please sign in to comment.