diff --git a/CONTRACTS.md b/CONTRACTS.md index e816519c9..2c56acfff 100644 --- a/CONTRACTS.md +++ b/CONTRACTS.md @@ -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/"` or `"snippet//"` string of a `@Snippet` type from `snippet//.json`. Complete `NOISE_STYLE` references and `@Ref(kind = ...)` of the host kinds (`EXPRESSIONS`, `IMAGE_MAPS` among them); diff --git a/DESIGN.md b/DESIGN.md index 57f81f6aa..04a0b5443 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -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. diff --git a/api/src/main/java/com/volmit/config/JsonForms.java b/api/src/main/java/com/volmit/config/JsonForms.java new file mode 100644 index 000000000..62dacc24b --- /dev/null +++ b/api/src/main/java/com/volmit/config/JsonForms.java @@ -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> value(); + + /** + * Reads and writes one class's JSON forms as plain values: {@code Map}, {@code List}, + * {@code Double} for every number, {@code String} and {@code Boolean}. + */ + interface Codec { + /** + * 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); + } +} diff --git a/host/src/main/java/com/volmit/host/ValueKinds.java b/host/src/main/java/com/volmit/host/ValueKinds.java index bfc5640b0..ced6d9e42 100644 --- a/host/src/main/java/com/volmit/host/ValueKinds.java +++ b/host/src/main/java/com/volmit/host/ValueKinds.java @@ -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> BOOLEANS = Set.of(boolean.class, Boolean.class); private static final Set> 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); } diff --git a/host/src/test/java/com/volmit/host/PackSchemaTest.java b/host/src/test/java/com/volmit/host/PackSchemaTest.java index 45347ff09..670b17788 100644 --- a/host/src/test/java/com/volmit/host/PackSchemaTest.java +++ b/host/src/test/java/com/volmit/host/PackSchemaTest.java @@ -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 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 { + @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");