mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
Add @MinItems for list, set and array options and describe it in the pack schema
This commit is contained in:
1 parent
3c05cfea0a
commit
0f05676b7a
9 files changed
+75
-17
No files matched your search
@@ -0,0 +1,12 @@
|
||||
package com.volmit.config;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/** Inclusive lower bound on the length of a list, set or array option, such as a palette that needs a block. Studio
|
||||
* shows it ({@code minItems}); like {@link MinNumber}, enforcement belongs to the configuration loader.
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
public @interface MinItems {
|
||||
int value();
|
||||
}
|
||||
@@ -13,11 +13,11 @@ public record OptionsMetadata(String description, String snippet, List<Option> o
|
||||
options = List.copyOf(options);
|
||||
}
|
||||
|
||||
/** One field of a settings type. {@code minimum}, {@code maximum}, {@code reference} and {@code resource} are null
|
||||
* when not declared; {@code resource} is the class of {@link ResourceRef}.
|
||||
/** One field of a settings type. {@code minimum}, {@code maximum}, {@code minItems}, {@code reference} and
|
||||
* {@code resource} are null when not declared; {@code resource} is the class of {@link ResourceRef}.
|
||||
*/
|
||||
public record Option(Field field, String description, boolean required,
|
||||
Double minimum, Double maximum, Reference reference, Class<?> resource) {
|
||||
Double minimum, Double maximum, Integer minItems, Reference reference, Class<?> resource) {
|
||||
public String name() {
|
||||
return field.getName();
|
||||
}
|
||||
@@ -46,6 +46,7 @@ public record OptionsMetadata(String description, String snippet, List<Option> o
|
||||
var desc = field.getAnnotation(Desc.class);
|
||||
var min = field.getAnnotation(MinNumber.class);
|
||||
var max = field.getAnnotation(MaxNumber.class);
|
||||
var minItems = field.getAnnotation(MinItems.class);
|
||||
var ref = field.getAnnotation(Ref.class);
|
||||
var resourceRef = field.getAnnotation(ResourceRef.class);
|
||||
if ((min != null || max != null) && !isNumeric(field.getType())) {
|
||||
@@ -56,8 +57,13 @@ public record OptionsMetadata(String description, String snippet, List<Option> o
|
||||
|| (min != null && max != null && min.value() > max.value())) {
|
||||
throw new IllegalArgumentException("Invalid bounds for option: " + field.getName());
|
||||
}
|
||||
if (minItems != null && (!isCollection(field.getType()) || minItems.value() < 0)) {
|
||||
throw new IllegalArgumentException("Invalid @MinItems on option: " + field.getName()
|
||||
+ "; it fits lists, sets and arrays, and its value is not negative");
|
||||
}
|
||||
options.add(new Option(field, desc == null ? "" : desc.value(), field.isAnnotationPresent(Required.class),
|
||||
min == null ? null : min.value(), max == null ? null : max.value(),
|
||||
minItems == null ? null : minItems.value(),
|
||||
ref == null ? null : ref.value(), resourceRef == null ? null : resourceRef.value()));
|
||||
}
|
||||
}
|
||||
@@ -76,6 +82,10 @@ public record OptionsMetadata(String description, String snippet, List<Option> o
|
||||
return classes;
|
||||
}
|
||||
|
||||
private static boolean isCollection(Class<?> type) {
|
||||
return List.class.isAssignableFrom(type) || java.util.Set.class.isAssignableFrom(type) || type.isArray();
|
||||
}
|
||||
|
||||
private static boolean isNumeric(Class<?> type) {
|
||||
return Number.class.isAssignableFrom(type) || type == byte.class || type == short.class
|
||||
|| type == int.class || type == long.class || type == float.class || type == double.class;
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalDouble;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.TreeMap;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
@@ -192,7 +193,11 @@ final class PackSchemaBuilder {
|
||||
private static SchemaOption schemaOption(TypeDrafts.OptionDraft draft, DefaultValues defaults) {
|
||||
var option = draft.option();
|
||||
return new SchemaOption(option.name(), draft.kind(), option.description(), option.required(),
|
||||
bound(option.minimum()), bound(option.maximum()), defaults.of(draft.defaultValue(), draft.kind()));
|
||||
bound(option.minimum()), bound(option.maximum()), minItems(option.minItems()), defaults.of(draft.defaultValue(), draft.kind()));
|
||||
}
|
||||
|
||||
private static OptionalInt minItems(Integer value) {
|
||||
return value == null ? OptionalInt.empty() : OptionalInt.of(value);
|
||||
}
|
||||
|
||||
private static OptionalDouble bound(Double value) {
|
||||
|
||||
@@ -3,8 +3,11 @@ package com.volmit.host.schema;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalDouble;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
/** One option of an {@link ObjectType}: a field of a settings class.
|
||||
*
|
||||
* <p>{@code minItems} is the least length of a list, set or array option ({@link com.volmit.config.MinItems}).
|
||||
*
|
||||
* <p>{@code defaultValue} is the field's value in an instance made by the class's no-argument
|
||||
* constructor, as plain JSON-shaped Java values: {@code Boolean}, {@code Long} for integer kinds,
|
||||
@@ -22,6 +25,7 @@ public record SchemaOption(
|
||||
boolean required,
|
||||
OptionalDouble minimum,
|
||||
OptionalDouble maximum,
|
||||
OptionalInt minItems,
|
||||
Optional<Object> defaultValue) {
|
||||
public SchemaOption {
|
||||
Objects.requireNonNull(name, "name");
|
||||
@@ -29,6 +33,7 @@ public record SchemaOption(
|
||||
Objects.requireNonNull(description, "description");
|
||||
Objects.requireNonNull(minimum, "minimum");
|
||||
Objects.requireNonNull(maximum, "maximum");
|
||||
Objects.requireNonNull(minItems, "minItems");
|
||||
Objects.requireNonNull(defaultValue, "defaultValue");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user