Skip to content

Commit

Permalink
Convert 1 or 2 arguments model actions
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Lacasse <[email protected]>
  • Loading branch information
lacasseio committed May 23, 2023
1 parent 6e6ee42 commit f7a4dcd
Show file tree
Hide file tree
Showing 23 changed files with 1,084 additions and 844 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.google.common.collect.ImmutableSet;
import dev.nokee.model.internal.core.ModelActionWithInputs;
import dev.nokee.model.internal.core.ModelComponentReference;
import dev.nokee.model.internal.core.ModelNode;
import dev.nokee.model.internal.core.ParentComponent;
import dev.nokee.model.internal.core.ParentUtils;
Expand All @@ -29,10 +28,10 @@
public abstract class AncestryCapabilityPlugin<T extends ExtensionAware & PluginAware> implements Plugin<T> {
@Override
public void apply(T target) {
target.getExtensions().getByType(ModelConfigurer.class).configure(ModelActionWithInputs.of(ModelComponentReference.of(ParentComponent.class), AncestryCapabilityPlugin::calculateAncestorsFromParent));
}

private static void calculateAncestorsFromParent(ModelNode entity, ParentComponent parent) {
entity.addComponent(new AncestorsComponent(new Ancestors(ParentUtils.stream(parent).map(AncestorRef::of).collect(ImmutableSet.toImmutableSet()))));
target.getExtensions().getByType(ModelConfigurer.class).configure(/*calculateAncestorsFromParent*/new ModelActionWithInputs.ModelAction1<ParentComponent>() {
protected void execute(ModelNode entity, ParentComponent parent) {
entity.addComponent(new AncestorsComponent(new Ancestors(ParentUtils.stream(parent).map(AncestorRef::of).collect(ImmutableSet.toImmutableSet()))));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import dev.nokee.model.internal.core.ModelAction;
import dev.nokee.model.internal.core.ModelActionWithInputs;
import dev.nokee.model.internal.core.ModelComponent;
import dev.nokee.model.internal.core.ModelComponentReference;
import dev.nokee.model.internal.core.ModelComponentType;
import dev.nokee.model.internal.core.ModelElement;
import dev.nokee.model.internal.core.ModelEntityId;
Expand Down Expand Up @@ -79,11 +78,10 @@ public DefaultModelRegistry(Instantiator instantiator) {
this.instantiator = instantiator;
this.elementFactory = new ModelElementFactory(instantiator);
this.bindingService = new BindManagedProjectionService(instantiator);
configure(ModelActionWithInputs.of(ModelComponentReference.of(ModelPathComponent.class), ModelComponentReference.of(ModelState.class), new ModelActionWithInputs.A2<ModelPathComponent, ModelState>() {
configure(new ModelActionWithInputs.ModelAction2<ModelPathComponent, ModelState>() {
private final Set<ModelEntityId> alreadyExecuted = new HashSet<>();

@Override
public void execute(ModelNode node, ModelPathComponent path, ModelState state) {
protected void execute(ModelNode node, ModelPathComponent path, ModelState state) {
if (state.isAtLeast(ModelState.Created) && alreadyExecuted.add(node.getId())) {
if (!path.get().equals(ModelPath.root()) && (!path.get().getParent().isPresent() || !nodes.containsKey(path.get().getParent().get()))) {
throw new IllegalArgumentException(String.format("Model %s has to be direct descendant", path.get()));
Expand All @@ -102,18 +100,17 @@ public void execute(ModelNode node, ModelPathComponent path, ModelState state) {
});
}
}
}));
configure(ModelActionWithInputs.of(ModelComponentReference.of(ModelPathComponent.class), ModelComponentReference.of(ModelState.class), new ModelActionWithInputs.A2<ModelPathComponent, ModelState>() {
public final Set<ModelEntityId> alreadyExecuted = new HashSet<>();
});
configure(new ModelActionWithInputs.ModelAction2<ModelPathComponent, ModelState>() {
private final Set<ModelEntityId> alreadyExecuted = new HashSet<>();

@Override
public void execute(ModelNode node, ModelPathComponent path, ModelState state) {
protected void execute(ModelNode node, ModelPathComponent path, ModelState state) {
if (state.isAtLeast(ModelState.Registered) && alreadyExecuted.add(node.getId())) {
assert !nodes.values().contains(node) : "duplicated registered notification";
nodes.put(path.get(), node);
}
}
}));
});
rootNode = ModelStates.register(createRootNode());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,30 @@
package dev.nokee.model.internal.core;

import dev.nokee.model.internal.registry.DefaultModelRegistry;
import dev.nokee.model.internal.type.ModelType;
import lombok.val;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.function.BiConsumer;

import static dev.nokee.internal.testing.util.ProjectTestUtils.objectFactory;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;

class ModelNodeCallbackTest {
private final DefaultModelRegistry registry = new DefaultModelRegistry(objectFactory()::newInstance);
private final ModelNode subject = ModelNodes.of(registry.register(ModelRegistration.of("foo", MyProjection.class)));
private final ModelActionWithInputs.A1<MyComponent> action = Mockito.mock(ModelActionWithInputs.A1.class);
private final BiConsumer<ModelNode, MyComponent> action = Mockito.mock(BiConsumer.class);

@BeforeEach
void registerListener() {
registry.configure(ModelActionWithInputs.of(ModelComponentReference.of(MyComponent.class), action));
registry.configure(new ModelActionWithInputs.ModelAction1<MyComponent>() {
protected void execute(ModelNode entity, MyComponent myComponent) {
action.accept(entity, myComponent);
}
});
}

@Nested
Expand All @@ -48,15 +53,15 @@ void addComponent() {

@Test
void callsBackWhenInputMatches() {
Mockito.verify(action).execute(subject, component);
Mockito.verify(action).accept(subject, component);
}

@Test
void callsBackWhenInputChanges() {
val newComponent = new MyComponent();
Mockito.reset(action);
subject.setComponent(MyComponent.class, newComponent);
Mockito.verify(action).execute(subject, newComponent);
Mockito.verify(action).accept(subject, newComponent);
}

@Nested
Expand All @@ -71,14 +76,14 @@ void newEntity() {

@Test
void doesNotCallbackOnNewEntity() {
Mockito.verify(action, never()).execute(any(), any());
Mockito.verify(action, never()).accept(any(), any());
}

@Test
void callsBackOnNewEntityWhenInputMatches() {
val component = new MyComponent();
newEntity.addComponent(component);
Mockito.verify(action).execute(newEntity, component);
Mockito.verify(action).accept(newEntity, component);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import dev.nokee.model.internal.core.DescendantNodes;
import dev.nokee.model.internal.core.ModelAction;
import dev.nokee.model.internal.core.ModelActionWithInputs;
import dev.nokee.model.internal.core.ModelComponentReference;
import dev.nokee.model.internal.core.ModelNode;
import dev.nokee.model.internal.core.ModelNodeUtils;
import dev.nokee.model.internal.core.ModelPath;
Expand Down Expand Up @@ -118,11 +117,13 @@ void failsLookupForInitializedNode() {
assertThrows(IllegalArgumentException.class, () -> modelLookup.get(path("foo")));
return null;
}).when(action).execute(any());
subject.configure(ModelActionWithInputs.of(ModelComponentReference.of(ModelState.class), (node, state) -> {
if (state.equals(ModelState.Initialized)) {
action.execute(node);
subject.configure(new ModelActionWithInputs.ModelAction1<ModelState>() {
protected void execute(ModelNode node, ModelState state) {
if (state.equals(ModelState.Initialized)) {
action.execute(node);
}
}
}));
});
register("foo");
verify(action, times(1)).execute(any());
}
Expand All @@ -135,11 +136,13 @@ void succeedLookupForRegisteredNode() {
assertDoesNotThrow(() -> modelLookup.get(path("bar")));
return null;
}).when(action).execute(any());
subject.configure(ModelActionWithInputs.of(ModelComponentReference.of(ModelPathComponent.class), ModelComponentReference.of(ModelState.class), (node, path, state) -> {
if (state.equals(ModelState.Registered) && path.get().equals(path("bar"))) {
action.execute(node);
subject.configure(new ModelActionWithInputs.ModelAction2<ModelPathComponent, ModelState>() {
protected void execute(ModelNode node, ModelPathComponent path, ModelState state) {
if (state.equals(ModelState.Registered) && path.get().equals(path("bar"))) {
action.execute(node);
}
}
}));
});
register("bar");
verify(action, times(1)).execute(any());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import dev.nokee.ide.visualstudio.internal.rules.CreateNativeComponentVisualStudioIdeProject;
import dev.nokee.model.internal.ModelElementFactory;
import dev.nokee.model.internal.core.ModelActionWithInputs;
import dev.nokee.model.internal.core.ModelComponentReference;
import dev.nokee.model.internal.core.ModelNode;
import dev.nokee.model.internal.registry.ModelConfigurer;
import dev.nokee.model.internal.tags.ModelTags;
import dev.nokee.model.internal.tags.ModelComponentTag;
import dev.nokee.platform.base.internal.IsComponent;
import dev.nokee.platform.base.internal.plugins.ComponentModelBasePlugin;
import dev.nokee.platform.base.internal.plugins.OnDiscover;
Expand All @@ -44,9 +44,11 @@ private Action<ComponentModelBasePlugin> mapComponentToVisualStudioIdeProjects(P
public void execute(ComponentModelBasePlugin appliedPlugin) {
val modelConfigurer = project.getExtensions().getByType(ModelConfigurer.class);
val action = new CreateNativeComponentVisualStudioIdeProject(extension, project.getLayout(), project.getObjects(), project.getProviders());
modelConfigurer.configure(new OnDiscover(ModelActionWithInputs.of(ModelTags.referenceOf(IsComponent.class), ModelComponentReference.of(ModelElementFactory.class), (entity, tag, factory) -> {
action.execute(factory.createElement(entity));
})));
modelConfigurer.configure(new OnDiscover(new ModelActionWithInputs.ModelAction2<ModelComponentTag<IsComponent>, ModelElementFactory>() {
protected void execute(ModelNode entity, ModelComponentTag<IsComponent> tag, ModelElementFactory factory) {
action.execute(factory.createElement(entity));
}
}));
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import dev.nokee.model.internal.ModelElementFactory;
import dev.nokee.model.internal.ProjectIdentifier;
import dev.nokee.model.internal.core.ModelActionWithInputs;
import dev.nokee.model.internal.core.ModelComponentReference;
import dev.nokee.model.internal.core.ModelNode;
import dev.nokee.model.internal.registry.ModelConfigurer;
import dev.nokee.model.internal.registry.ModelLookup;
import dev.nokee.model.internal.tags.ModelTags;
import dev.nokee.model.internal.tags.ModelComponentTag;
import dev.nokee.platform.base.internal.IsComponent;
import dev.nokee.platform.base.internal.plugins.ComponentModelBasePlugin;
import dev.nokee.platform.base.internal.plugins.OnDiscover;
Expand Down Expand Up @@ -121,9 +121,11 @@ private Action<ComponentModelBasePlugin> mapComponentToXcodeIdeProjects(Project
public void execute(ComponentModelBasePlugin appliedPlugin) {
val modelConfigurer = project.getExtensions().getByType(ModelConfigurer.class);
val action = new CreateNativeComponentXcodeIdeProject(extension, project.getProviders(), project.getObjects(), project.getLayout(), project.getTasks(), ProjectIdentifier.of(project), project.getExtensions().getByType(ModelLookup.class));
modelConfigurer.configure(new OnDiscover(ModelActionWithInputs.of(ModelTags.referenceOf(IsComponent.class), ModelComponentReference.of(ModelElementFactory.class), (entity, tag, factory) -> {
action.execute(factory.createElement(entity));
})));
modelConfigurer.configure(new OnDiscover(new ModelActionWithInputs.ModelAction2<ModelComponentTag<IsComponent>, ModelElementFactory>() {
protected void execute(ModelNode entity, ModelComponentTag<IsComponent> tag, ModelElementFactory factory) {
action.execute(factory.createElement(entity));
}
}));
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import dev.nokee.model.internal.core.IdentifierComponent;
import dev.nokee.model.internal.core.ModelActionWithInputs;
import dev.nokee.model.internal.core.ModelComponentReference;
import dev.nokee.model.internal.core.ModelNode;
import dev.nokee.model.internal.core.ModelNodeContext;
import dev.nokee.model.internal.core.ModelNodeUtils;
import dev.nokee.model.internal.core.ModelPathComponent;
Expand All @@ -35,6 +36,7 @@
import dev.nokee.model.internal.registry.ModelConfigurer;
import dev.nokee.model.internal.registry.ModelRegistry;
import dev.nokee.model.internal.state.ModelState;
import dev.nokee.model.internal.tags.ModelComponentTag;
import dev.nokee.model.internal.tags.ModelTags;
import dev.nokee.model.internal.type.ModelType;
import dev.nokee.model.internal.type.TypeOf;
Expand Down Expand Up @@ -70,12 +72,14 @@ public void apply(Project project) {

val elementsPropertyFactory = new ComponentElementsPropertyRegistrationFactory();

// ComponentFromEntity<DisplayNameComponent> read-only self
project.getExtensions().getByType(ModelConfigurer.class).configure(ModelActionWithInputs.of(ModelTags.referenceOf(IsLanguageSourceSet.class), ModelComponentReference.of(ModelState.IsAtLeastCreated.class), (entity, ignored1, ignored2) -> {
if (!entity.has(DisplayNameComponent.class)) {
entity.addComponent(new DisplayNameComponent("sources"));
project.getExtensions().getByType(ModelConfigurer.class).configure(new ModelActionWithInputs.ModelAction2<ModelComponentTag<IsLanguageSourceSet>, ModelState.IsAtLeastCreated>() {
// ComponentFromEntity<DisplayNameComponent> read-only self
protected void execute(ModelNode entity, ModelComponentTag<IsLanguageSourceSet> ignored1, ModelState.IsAtLeastCreated ignored2) {
if (!entity.has(DisplayNameComponent.class)) {
entity.addComponent(new DisplayNameComponent("sources"));
}
}
}));
});

// ComponentFromEntity<ParentComponent> read-only self
project.getExtensions().getByType(ModelConfigurer.class).configure(ModelActionWithInputs.of(ModelTags.referenceOf(IsLanguageSourceSet.class), ModelComponentReference.of(ModelPathComponent.class), ModelComponentReference.of(DisplayNameComponent.class), ModelComponentReference.of(ElementNameComponent.class), ModelComponentReference.of(ModelState.IsAtLeastCreated.class), (entity, ignored1, path, displayName, elementName, ignored2) -> {
Expand Down
Loading

0 comments on commit f7a4dcd

Please sign in to comment.