Skip to content

Commit

Permalink
#24 Makes the type resolver asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
kmruiz committed Nov 11, 2019
1 parent 69bb975 commit 671c053
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

package io.vlingo.schemata.codegen.processor.types;

import io.vlingo.common.Completes;
import io.vlingo.schemata.codegen.ast.types.TypeDefinition;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import io.vlingo.schemata.codegen.ast.types.TypeDefinition;

public class CacheTypeResolver implements TypeResolver {
private final Map<String, TypeDefinition> types;

Expand All @@ -21,13 +22,13 @@ public CacheTypeResolver() {
}

@Override
public Optional<TypeDefinition> resolve(String fullQualifiedTypeName, final String simpleTypeName) {
public Completes<Optional<TypeDefinition>> resolve(String fullQualifiedTypeName, final String simpleTypeName) {
for (final TypeDefinition type : types.values()) {
if (type.fullyQualifiedTypeName.equals(fullQualifiedTypeName) || type.typeName.equals(simpleTypeName)) {
return Optional.of(type);
return Completes.withSuccess(Optional.of(type));
}
}
return Optional.empty();
return Completes.withSuccess(Optional.empty());
}

public void produce(TypeDefinition typeDefinition, String version) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

package io.vlingo.schemata.codegen.processor.types;

import java.util.Optional;

import io.vlingo.common.Completes;
import io.vlingo.schemata.codegen.ast.types.TypeDefinition;

import java.util.Optional;

public interface TypeResolver {
Optional<TypeDefinition> resolve(final String fullQualifiedTypeName, final String simpleTypeName);
Completes<Optional<TypeDefinition>> resolve(final String fullQualifiedTypeName, final String simpleTypeName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

package io.vlingo.schemata.codegen.processor.types;

import java.util.List;
import java.util.stream.Collectors;

import io.vlingo.actors.Actor;
import io.vlingo.common.Completes;
import io.vlingo.schemata.Schemata;
Expand All @@ -20,6 +17,11 @@
import io.vlingo.schemata.codegen.ast.types.TypeDefinition;
import io.vlingo.schemata.codegen.processor.Processor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

public class TypeResolverProcessor extends Actor implements Processor {
private final TypeResolver resolver;

Expand All @@ -28,35 +30,41 @@ public TypeResolverProcessor(TypeResolver resolver) {
}

@Override
@SuppressWarnings("unchecked")
public Completes<Node> process(Node node, final String fullyQualifiedTypeName) {
TypeDefinition type = Processor.requireBeing(node, TypeDefinition.class);

List<Node> processedTypes = type.children.stream()
List<Completes<Node>> processedTypeList = type.children.stream()
.map(e -> (FieldDefinition) e)
.map(this::resolveType)
.map(e -> (Completes) e)
.map(e -> (Completes<Node>) e)
.collect(Collectors.toList());

completesEventually().with(new TypeDefinition(type.category, fullyQualifiedTypeName, type.typeName, processedTypes));
Completes<List<Node>> eventuallyProcessedTypes = unwrap(processedTypeList);
eventuallyProcessedTypes.andFinallyConsume(processedTypes -> {
completesEventually().with(new TypeDefinition(type.category, fullyQualifiedTypeName, type.typeName, processedTypes));
});

return completes();

}

private FieldDefinition resolveType(FieldDefinition fieldDefinition) {
private Completes<FieldDefinition> resolveType(FieldDefinition fieldDefinition) {
final Type typeNode = fieldDefinition.type;

if (typeNode instanceof BasicType) {
final BasicType basicType = (BasicType) typeNode;

final Type type =
resolver
.resolve(basicType.typeName, simple(basicType.typeName))
.map(definition -> (Type) definition)
.orElse(basicType);
Completes<Type> resolvedType = resolver.resolve(basicType.typeName, simple(basicType.typeName))
.andThen(foundType -> foundType.map(definition -> (Type) definition).orElse(basicType));

return new FieldDefinition(type, fieldDefinition.version, fieldDefinition.name, fieldDefinition.defaultValue);
return resolvedType.andThen(type ->
new FieldDefinition(type, fieldDefinition.version, fieldDefinition.name, fieldDefinition.defaultValue)
);
}

return fieldDefinition;
return Completes.withSuccess(fieldDefinition);
}

private String simple(final String typeName) {
Expand All @@ -66,4 +74,22 @@ private String simple(final String typeName) {
}
return typeName;
}

private <T> Completes<List<T>> unwrap(List<Completes<T>> completes) {
CountDownLatch latch = new CountDownLatch(completes.size());
List<T> result = new ArrayList<>(completes.size());
completes.forEach(complete -> {
complete.andThenConsume(result::add)
.andFinallyConsume(e -> latch.countDown());
});

return Completes.withSuccess(result)
.andThenConsume(i -> {
try {
latch.await();
} catch (InterruptedException e) {
logger().error("TypeResolverProcessor could not unwrap list of Completes<T> " + e.getMessage(), e);
}
});
}
}

0 comments on commit 671c053

Please sign in to comment.