mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 16:25:36 +00:00
Let a settings value read its own JSON forms
@JsonForms(Codec) marks a value packs write in more than one shape, such as a number, a [min, max] range or a curve object. The host hands its JSON to the codec as plain values and writes it back through it; it never creates the class or reads its fields, and the schema describes it as any JSON value. It is the one exception to the decoding contract, so module jars no longer need Gson (which the plugin relocates) for such values.
This commit is contained in:
1 parent
5e85e1e3b8
commit
321ab5b486
5 files changed
+97
-3
No files matched your search
+3
-1
@@ -62,7 +62,7 @@ A pack load works on copies of the module jars, so the originals are never held
|
||||
Modules describe their settings only through their settings classes; jars ship no schema files. The
|
||||
pack load turns every catalogued settings type into a `PackSchema` and fails, naming field, settings
|
||||
and module, on a field type it cannot describe, a `@Ref` on a field that is not text, a class without
|
||||
a working no-argument constructor, a bad or repeated snippet name, or a pack class's snippet name
|
||||
a working no-argument constructor (a `@JsonForms` class needs none: it is described as any JSON value), a bad or repeated snippet name, or a pack class's snippet name
|
||||
outside its module's folder. Defaults come from an instance made by that constructor. Options are in
|
||||
declaration order.
|
||||
|
||||
@@ -250,6 +250,8 @@ running Iris is the pack publisher's responsibility.
|
||||
same one with a higher amplifier.
|
||||
- Decode IrisApi's value types like settings: `BlockSpec`, `PaletteSpec`, `NoiseStyle`, `StyledRange`, `Range` and
|
||||
`BlockPosition` from the JSON shapes in their docs, `Object` as maps, lists, `Double`s, strings and booleans.
|
||||
Read and write a `@JsonForms` class through its codec, created once with its no-argument constructor, handing it
|
||||
those same plain values; a member written as `null` stays left out and never reaches the codec.
|
||||
Read a `"snippet/<name>"` or `"snippet/<folder>/<name>"` string of a `@Snippet` type from
|
||||
`snippet/<folder>/<name>.json`. Complete `NOISE_STYLE` references and `@Ref(kind = ...)` of the host kinds
|
||||
(`EXPRESSIONS`, `IMAGE_MAPS` among them);
|
||||
|
||||
@@ -870,6 +870,10 @@ Every settings class, value type and pack resource kind is read the same way, by
|
||||
|
||||
- The host creates the instance with the no-argument constructor and reads each JSON member into the field of the
|
||||
same name. It calls no setter and no other constructor.
|
||||
- A class annotated `@JsonForms(Codec.class)` is the one exception: a value packs write in several shapes, such as a
|
||||
number, a `[min, max]` range or a curve object. The host reads its JSON as plain values (maps, lists, `Double`s,
|
||||
strings, booleans), hands them to the codec, and writes the value back through it. It never creates the class or
|
||||
reads its fields, and studio describes it as any JSON value.
|
||||
- A member the JSON leaves out keeps the field's initialiser. A member written as `null` counts as left out.
|
||||
- The host never writes `null` into a field. An enum name the enum does not know is an error that lists the known
|
||||
names; it does not become `null` or the default.
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.volmit.config;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* A settings value packs may write in more than one JSON shape, such as a number, a {@code [min, max]} range or an
|
||||
* object. The host reads the JSON as plain values and hands it to {@link #value()}, and writes the value back
|
||||
* through it; it does not read the class's fields, create it, or describe more of it than "any JSON value". A class
|
||||
* without this annotation is read field by field (DESIGN.md, "Decoding contract").
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface JsonForms {
|
||||
/** The codec, created by the host with its no-argument constructor. */
|
||||
Class<? extends Codec<?>> value();
|
||||
|
||||
/**
|
||||
* Reads and writes one class's JSON forms as plain values: {@code Map<String, Object>}, {@code List<Object>},
|
||||
* {@code Double} for every number, {@code String} and {@code Boolean}.
|
||||
*/
|
||||
interface Codec<T> {
|
||||
/**
|
||||
* The value {@code json} writes; never null, since a member written as {@code null} counts as left out.
|
||||
*
|
||||
* @throws IllegalArgumentException when {@code json} is none of the forms, saying which forms there are
|
||||
*/
|
||||
T read(Object json);
|
||||
|
||||
/** {@code value} as plain values {@link #read} reads back as an equal value. */
|
||||
Object write(T value);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.volmit.host;
|
||||
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.config.Desc;
|
||||
import com.volmit.config.JsonForms;
|
||||
import com.volmit.config.Reference;
|
||||
import com.volmit.host.schema.TextFormat;
|
||||
import com.volmit.host.schema.ValueKind;
|
||||
@@ -42,7 +43,7 @@ import java.util.stream.Stream;
|
||||
@SuppressWarnings("removal") // host kinds are transitional, until the cut-over
|
||||
final class ValueKinds {
|
||||
static final String SUPPORTED_TYPES = "settings fields can be booleans, numbers, String, ResourceId, BlockState, enums, "
|
||||
+ "lists, sets and arrays of these, Map with String keys, Object for any JSON value and classes with a no-argument constructor";
|
||||
+ "lists, sets and arrays of these, Map with String keys, Object for any JSON value, @JsonForms classes and classes with a no-argument constructor";
|
||||
private static final Set<Class<?>> BOOLEANS = Set.of(boolean.class, Boolean.class);
|
||||
private static final Set<Class<?>> INTEGERS = Set.of(byte.class, short.class, int.class, long.class,
|
||||
Byte.class, Short.class, Integer.class, Long.class);
|
||||
@@ -109,7 +110,7 @@ final class ValueKinds {
|
||||
if (INTEGERS.contains(type)) return new IntegerKind();
|
||||
if (DECIMALS.contains(type)) return new DecimalKind();
|
||||
if (type.isEnum()) return enumKind(type);
|
||||
if (type == Object.class) return new AnyKind();
|
||||
if (type == Object.class || type.isAnnotationPresent(JsonForms.class)) return new AnyKind();
|
||||
return objects.apply(type);
|
||||
}
|
||||
|
||||
|
||||
@@ -512,6 +512,61 @@ class PackSchemaTest {
|
||||
assertEquals(List.of("palette", "range", "style", "style-range"), schema.snippets().stream().map(type -> type.snippet().orElseThrow()).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aClassWithJsonFormsHoldsAnyJsonValueAndIsNeitherCreatedNorWalked() {
|
||||
var pack = pack("forms");
|
||||
module("pack.flora.FloraModule", FLORA,
|
||||
NO_MEMBERS, "registry.registerSettings(FloraSettings.class);")
|
||||
.source("pack.flora.FloraSettings", """
|
||||
package pack.flora;
|
||||
|
||||
import com.volmit.config.Level;
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.config.Feature;
|
||||
import java.util.*;
|
||||
|
||||
@Feature(id = "example:flora", name = "Flora", key = "flora", targets = Level.BIOME)
|
||||
public final class FloraSettings implements IrisFeature {
|
||||
private Height height = new Height(4);
|
||||
private List<Height> steps = List.of();
|
||||
}
|
||||
""")
|
||||
.source("pack.flora.Height", """
|
||||
package pack.flora;
|
||||
|
||||
import com.volmit.config.JsonForms;
|
||||
|
||||
@JsonForms(Height.Forms.class)
|
||||
public final class Height {
|
||||
private final Thread notASettingsField;
|
||||
|
||||
Height(double blocks) {
|
||||
notASettingsField = null;
|
||||
}
|
||||
|
||||
public static final class Forms implements JsonForms.Codec<Height> {
|
||||
@Override
|
||||
public Height read(Object json) {
|
||||
return new Height((Double) json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object write(Height value) {
|
||||
return 4.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
""")
|
||||
.writeInto(pack, "flora.jar");
|
||||
var schema = schemaOf(pack, NO_BUILT_INS);
|
||||
var settings = schema.type("pack.flora.FloraSettings");
|
||||
|
||||
assertEquals(new AnyKind(), option(settings, "height").kind());
|
||||
assertEquals(Optional.empty(), option(settings, "height").defaultValue());
|
||||
assertEquals(new ListKind(new AnyKind(), false), option(settings, "steps").kind());
|
||||
assertTrue(schema.types().stream().noneMatch(type -> type.name().equals("pack.flora.Height")), "the host describes no more of it");
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultsLeaveOutWhatJsonCannotHold() {
|
||||
var pack = pack("defaults");
|
||||
|
||||
Reference in new issue
Block a user