Skip to content

Commit

Permalink
more work on new schema manager api
Browse files Browse the repository at this point in the history
  • Loading branch information
Sophos-Elias-Vasylenko committed Nov 23, 2015
1 parent 29ed2b0 commit 7819319
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,21 @@
import java.util.function.Predicate;
import java.util.function.Supplier;

import uk.co.strangeskies.modabi.io.DataTarget;
import uk.co.strangeskies.modabi.io.structured.DataInterface;
import uk.co.strangeskies.modabi.io.structured.StructuredDataSource;
import uk.co.strangeskies.modabi.io.structured.StructuredDataTarget;
import uk.co.strangeskies.modabi.io.structured.StructuredDataTargetImpl;
import uk.co.strangeskies.modabi.processing.BindingFuture;
import uk.co.strangeskies.modabi.processing.BindingState;
import uk.co.strangeskies.modabi.processing.UnbindingState;
import uk.co.strangeskies.modabi.schema.ComplexNode;
import uk.co.strangeskies.modabi.schema.DataNode;
import uk.co.strangeskies.modabi.schema.Model;
import uk.co.strangeskies.modabi.schema.SchemaNode;
import uk.co.strangeskies.reflection.Reified;
import uk.co.strangeskies.reflection.TypeToken;
import uk.co.strangeskies.reflection.TypedObject;

public interface SchemaManager {
interface Binder<T> {
Expand Down Expand Up @@ -92,16 +98,22 @@ default BindingFuture<T> from(URL input) {
BindingFuture<T> from(InputStream input);

BindingFuture<T> from(String extension, InputStream input);
}

interface Unbinder {
<U> Binder<T> supply(TypeToken<U> type,
Supplier<TypedObject<? extends U>> action);

<U> Binder<T> supply(TypeToken<U> type,
Function<BindingState, TypedObject<? extends U>> action);

/*
* TODO If errors are rethrown, deal with as normal. Otherwise. best effort
* at unbinding, outputting comments on errors instead of throwing
* exceptions.
* Errors which are rethrown will be passed to the next error handler if
* present, or dealt with as normal. Otherwise, a best effort is made at
* binding.
*/
// Unbinder with(Consumer<Exception> errorHandler);
Binder<T> with(Consumer<Exception> errorHandler);
}

interface Unbinder {
<U extends StructuredDataTarget> U to(U output);

default void to(File output) {
Expand Down Expand Up @@ -136,23 +148,16 @@ default void to(URL output) {

<U extends OutputStream> U to(String extension, U output);

Unbinder filter(ReturningSchemaProcessor<Boolean> filter);
Unbinder consume(Predicate<UnbindingState> filter);

<T> Unbinder filter(TypeToken<T> type, Predicate<T> action);
Unbinder consume(BiPredicate<UnbindingState, TypedObject<?>> filter);

<T> Unbinder filterComplex(TypeToken<T> type,
BiPredicate<ComplexNode<T>, T> action);

<T> Unbinder filterData(TypeToken<T> type,
BiPredicate<DataNode<T>, T> action);

<T> Unbinder consume(TypeToken<T> type, Consumer<T> action);

<T> Unbinder consumeComplex(TypeToken<T> type,
BiConsumer<ComplexNode<T>, T> action);

<T> Unbinder consumeData(TypeToken<T> type,
BiConsumer<DataNode<T>, T> action);
/*
* Errors which are rethrown will be passed to the next error handler if
* present, or dealt with as normal. Otherwise, a best effort is made at
* unbinding.
*/
Unbinder with(Consumer<Exception> errorHandler);
}

void registerDataInterface(DataInterface handler);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package uk.co.strangeskies.modabi.io;

public class DiscardingDataTarget extends DataTargetDecorator {
public DiscardingDataTarget() {
super(new DataTarget() {
@Override
public void terminate() {}

@Override
public <T> DataTarget put(DataItem<T> item) {
return this;
}

@Override
public DataStreamState currentState() {
return null;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package uk.co.strangeskies.modabi.io.structured;

import uk.co.strangeskies.modabi.Namespace;
import uk.co.strangeskies.modabi.QualifiedName;
import uk.co.strangeskies.modabi.io.DataTarget;
import uk.co.strangeskies.modabi.io.DiscardingDataTarget;

public class DiscardingStructuredDataTarget
extends StructuredDataTargetImpl<DiscardingStructuredDataTarget> {
@Override
protected void registerDefaultNamespaceHintImpl(Namespace namespace) {}

@Override
protected void registerNamespaceHintImpl(Namespace namespace) {}

@Override
protected void commentImpl(String comment) {}

@Override
protected void nextChildImpl(QualifiedName name) {}

@Override
protected DataTarget writePropertyImpl(QualifiedName name) {
return new DiscardingDataTarget();
}

@Override
protected DataTarget writeContentImpl() {
return new DiscardingDataTarget();
}

@Override
protected void endChildImpl() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,25 @@ protected ComplexNode.Effective<? extends U> getExactNode(
"Cannot find model '" + nextElement + "' to bind to", context);
}

if (!node.getDataType().isAssignableFrom(extension.getDataType()))
if (!node.getDataType().isAssignableFrom(extension.getDataType())) {
throw new BindingException(
"Named input node '" + nextElement + "' of type '"
+ extension.getDataType() + "' does not match type '"
+ node.getDataType() + "' of extention point",
context);
}

exactNode = context.getComplexNodeOverrides(node)
.putGet((Effective<? extends U>) extension);
} else if (node.isInline() || Objects.equals(nextElement, node.getName()))
} else if (node.isInline()
|| Objects.equals(nextElement, node.getName())) {
exactNode = node;
else
} else {
exactNode = null;
} else
}
} else {
exactNode = null;
}

return exactNode;
}
Expand Down

0 comments on commit 7819319

Please sign in to comment.