Add FeatureRegistry.registerSettings and the @Ref annotation for value references

Modules register settings types that no stage names in when(...) explicitly; the
id must be the module's id or below it, and repeating it or also naming the type
in when(...) changes nothing. A type its owner registered may be named by any
module. OptionsMetadata.Option now carries its field and @Ref reference.
This commit is contained in:
piexel committed 2026-10-06 12:06:46 +02:00
1 parent 8b7accd19d
commit 93c755ffe4
9 files changed
+164 -23

No files matched your search

@@ -35,7 +35,7 @@ public record LoadedModules(FeatureCatalog catalog, ResolvedGraph placement, Res
for (var discovered : modules) {
installModule(discovered, stages, registry);
}
stages.settingsTypes().forEach(registry::registerSettings);
stages.settingsTypes(registry::registeredByOwner).forEach(registry::registerStageSettings);
return new LoadedModules(registry.freeze(), stages.resolve(Pipeline.PLACEMENT), stages.resolve(Pipeline.CHUNK));
}
@@ -18,8 +18,8 @@ import java.util.Set;
/** Reference host registry. Single-threaded registration ends with freeze(); the catalog is
* immutable. IDs are unique across feature kinds; JSON fields are unique per attachment target.
* Pass existing host JSON fields to reserve them before modules register. Modules register through
* {@link #register(DiscoveredModule)}; object feature ids and mantle keys must be the registering
* module's id or start with it and '/' ({@link com.volmit.ModuleInfo#ownsId}). A failed registration leaves the registry unchanged. A failed module callback
* {@link #register(DiscoveredModule)}; object feature ids, mantle keys and explicitly registered
* settings ids must be the registering module's id or start with it and '/' ({@link com.volmit.ModuleInfo#ownsId}). A failed registration leaves the registry unchanged. A failed module callback
* invalidates the host's whole build. This does not decode JSON, discover modules or instantiate
* settings.
*/
@@ -29,6 +29,7 @@ public final class ModuleRegistry implements FeatureRegistry {
private final Map<String, MantleRegistration<?>> mantle = new HashMap<>();
private final Set<ResourceId> featureIds = new HashSet<>();
private final Set<Class<?>> featureTypes = new HashSet<>();
private final Set<Class<?>> registeredByOwner = new HashSet<>();
private final Map<Field, String> fields = new HashMap<>();
private FeatureCatalog catalog;
private DiscoveredModule registering;
@@ -81,15 +82,29 @@ public final class ModuleRegistry implements FeatureRegistry {
}
}
/** Claims a settings type named by a stage's when(...). Several stages may share one type. */
@Override
public void registerSettings(Class<? extends IrisFeature> settingsType) {
checkOpen();
var descriptor = FeatureDescriptor.of(settingsType);
requireOwnedId("Settings " + settingsType.getName() + " with feature id", descriptor.id());
catalogSettings(descriptor);
registeredByOwner.add(settingsType);
}
/** Catalogs a settings type named by a stage's when(...), whose owner {@link StageGraphBuilder}
* has checked. Several stages, and the owner's {@link #registerSettings}, may name one type.
*/
void registerStageSettings(Class<? extends IrisFeature> settingsType) {
checkOpen();
if (settings.containsKey(settingsType)) {
return;
}
var descriptor = FeatureDescriptor.of(settingsType);
claim(descriptor);
settings.put(settingsType, descriptor);
catalogSettings(FeatureDescriptor.of(settingsType));
}
/** Whether the module owning {@code settingsType} registered it through {@link #registerSettings}. */
boolean registeredByOwner(Class<?> settingsType) {
return registeredByOwner.contains(settingsType);
}
public FeatureCatalog freeze() {
@@ -99,6 +114,14 @@ public final class ModuleRegistry implements FeatureRegistry {
return catalog;
}
private void catalogSettings(FeatureDescriptor<?> descriptor) {
if (settings.containsKey(descriptor.type())) {
return;
}
claim(descriptor);
settings.put(descriptor.type(), descriptor);
}
private void requireOwnedId(String registration, ResourceId id) {
if (registering == null) {
throw new IllegalStateException(registration + " " + id + " registered outside IrisModule.register");
@@ -19,6 +19,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/** Reference host collection of module stages. Install every module, then resolve each pipeline
@@ -27,7 +28,8 @@ import java.util.stream.Collectors;
*
* <p>A module defines only stages whose ids start with its own id and '/', and a settings type
* named in {@code when(...)} must have a feature id that at least one module naming it owns: that
* module's id, or one starting with it and '/'.
* module's id, or one starting with it and '/'. A type its owner registered explicitly may be named
* by any module.
*/
public final class StageGraphBuilder implements StageGraph {
private final Map<ResourceId, StageDeclaration> declarations = new HashMap<>();
@@ -67,11 +69,14 @@ public final class StageGraphBuilder implements StageGraph {
}
/** Settings types named by when(...), ordered by feature id. Fails if no module that names a type
* owns its feature id ({@link com.volmit.ModuleInfo#ownsId}).
* owns its feature id ({@link com.volmit.ModuleInfo#ownsId}), unless {@code registeredByOwner}
* says its owner registered it explicitly.
*/
public List<Class<? extends IrisFeature>> settingsTypes() {
public List<Class<? extends IrisFeature>> settingsTypes(Predicate<Class<?>> registeredByOwner) {
var uses = settingsUses();
uses.forEach(this::requireSettingsOwner);
uses.stream()
.filter(use -> !registeredByOwner.test(use.settings().type()))
.forEach(this::requireSettingsOwner);
return uses.stream()
.<Class<? extends IrisFeature>>map(use -> use.settings().type())
.toList();