Skip to content

Commit

Permalink
Merge pull request #15 from Voiteh/issue/12
Browse files Browse the repository at this point in the history
Issue/12
  • Loading branch information
Voiteh authored Jun 22, 2019
2 parents 6554624 + f25fe81 commit b2c0dff
Show file tree
Hide file tree
Showing 30 changed files with 273 additions and 98 deletions.
16 changes: 11 additions & 5 deletions source/herd/depin/api/Injectable.ceylon
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import ceylon.language.meta.model {
Type
}
import ceylon.language.meta.declaration {
Declaration
}
Expand All @@ -12,8 +9,17 @@ shared abstract class Injectable(Declaration declaration){



shared class Error(Declaration declaration,Type<> type,Dependency description,Throwable? cause=null)
extends Exception("Can't inject ``declaration`` of type ``type`` described as ``description``",cause){}
shared class Error extends Exception{
shared new member(Throwable? cause=null,Anything container=null,{Anything*} parameters={})
extends Exception("Can't inject into ``container else "null"`` ``declaration`` with available parameters: ``parameters``",cause){}
shared new (Throwable? cause=null,{Anything*} parameters={})
extends Exception("Can't inject ``declaration`` with available parameters: ``parameters``",cause){}



}



string => declaration.string;
}
Expand Down
3 changes: 0 additions & 3 deletions source/herd/depin/api/Provider.ceylon
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

import ceylon.language.meta.model {
Type
}
import ceylon.language.meta.declaration {
Declaration
}
Expand Down
11 changes: 5 additions & 6 deletions source/herd/depin/engine/DefaultCreator.ceylon
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import herd.depin.api {
import ceylon.language.meta.declaration {
Declaration
}

shared class DefaultCreator(Registry registry,Resolver resolver) satisfies Creator{


shared actual Anything create(Declaration declaration) {
value dependency = resolver.resolve(declaration);
if(exists injectable = registry.get(dependency)){
return injectable.inject(this);
}
throw Exception("Dependency [``dependency``] not found in registry did You made a scan ? Available dependencies:\n +``registry``");
return registry.get(dependency)?.inject(this);
}





}
50 changes: 7 additions & 43 deletions source/herd/depin/engine/DefaultProvider.ceylon
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import ceylon.language.meta.declaration {
Declaration,
FunctionOrValueDeclaration,
FunctionDeclaration,
ValueDeclaration,
CallableConstructorDeclaration,
ClassDeclaration,
NestableDeclaration,
ConstructorDeclaration,
GenericDeclaration
}
Expand All @@ -14,8 +12,12 @@ import herd.depin.api {
Provider,
Injectable,
Dependency,
Registry,
Creator
Registry
}
import herd.depin.engine.injectable {
ConstructorInjectable,
FunctionInjectable,
ValueInjectable
}
shared class DefaultProvider(Registry registry) satisfies Provider{
shared actual Injectable provide(Declaration declaration, Dependency dependency) {
Expand All @@ -36,7 +38,7 @@ shared class DefaultProvider(Registry registry) satisfies Provider{
if(exists defaultConstructor= declaration.defaultConstructor){
return ConstructorInjectable(defaultConstructor);
}
value constructors=declaration.constructorDeclarations().select((ConstructorDeclaration element) => element.annotated<TargetAnnotation>());
ConstructorDeclaration[] constructors=declaration.constructorDeclarations().select((ConstructorDeclaration element) => element.annotated<TargetAnnotation>());
if(constructors.size==0){
throw Exception("Can't select injection target, no default constructor or annotated `` `class TargetAnnotation` ``");
}
Expand All @@ -52,42 +54,4 @@ shared class DefaultProvider(Registry registry) satisfies Provider{
}

}
class ConstructorInjectable(CallableConstructorDeclaration declaration) extends Injectable(declaration){
shared actual Anything inject(Creator injector) {
value parameters = declaration.parameterDeclarations.collect((FunctionOrValueDeclaration element) => injector.create(element));
if(declaration.container.container is NestableDeclaration){
assert(is Object container = injector.create(declaration.container));
return declaration.memberInvoke(container,[], parameters);
}

return declaration.invoke([],*parameters);
}


}
class FunctionInjectable(FunctionDeclaration declaration) extends Injectable(declaration){
shared actual Anything inject(Creator injector) {
value parameters = declaration.parameterDeclarations.collect((FunctionOrValueDeclaration element) => injector.create(element));
if(is NestableDeclaration containerDeclaration=declaration.container){
assert(exists container = injector.create(containerDeclaration));
return declaration.memberInvoke(container,[],parameters);
}else{
return declaration.invoke([],*parameters);
}
}


}
class ValueInjectable(ValueDeclaration declaration) extends Injectable(declaration){
shared actual Anything inject(Creator injector) {
if(is NestableDeclaration containerDeclaration=declaration.container){
assert(exists container = injector.create(containerDeclaration));
return declaration.memberGet(container);
}else{
return declaration.get();
}
}



}
3 changes: 1 addition & 2 deletions source/herd/depin/engine/DefaultScanner.ceylon
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import herd.depin.api {
Scope,
Scanner,
Dependency
Scanner
}
import ceylon.language.meta.declaration {
Declaration,
Expand Down
32 changes: 32 additions & 0 deletions source/herd/depin/engine/injectable/ConstructorInjectable.ceylon
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import ceylon.language.meta.declaration {
NestableDeclaration,
CallableConstructorDeclaration,
FunctionOrValueDeclaration
}
import herd.depin.api {
Injectable,
Creator
}
shared class ConstructorInjectable(CallableConstructorDeclaration declaration) extends Injectable(declaration){
shared actual Anything inject(Creator injector) {
value parameters = declaration.parameterDeclarations
.map((FunctionOrValueDeclaration element) => element->injector.create(element))
.filter((FunctionOrValueDeclaration elementKey -> Anything elementItem) => !elementKey.defaulted || elementItem exists)
.collect((FunctionOrValueDeclaration elementKey -> Anything elementItem) => elementItem);
if(declaration.container.container is NestableDeclaration){
assert(is Object container = injector.create(declaration.container));
try{
return declaration.memberInvoke(container,[], parameters);
}catch(Exception x){
throw Error.member(x,container,parameters);
}
}
try{
return declaration.invoke([],*parameters);
}catch(Exception x){
throw Error(x,parameters);
}
}


}
31 changes: 31 additions & 0 deletions source/herd/depin/engine/injectable/FunctionInjectable.ceylon
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import ceylon.language.meta.declaration {
NestableDeclaration,
FunctionOrValueDeclaration,
FunctionDeclaration
}
import herd.depin.api {
Injectable,
Creator
}
shared class FunctionInjectable(FunctionDeclaration declaration) extends Injectable(declaration){
shared actual Anything inject(Creator injector) {
value parameters = declaration.parameterDeclarations
.map((FunctionOrValueDeclaration element) => element->injector.create(element))
.filter((FunctionOrValueDeclaration elementKey -> Anything elementItem) => !elementKey.defaulted || elementItem exists)
.collect((FunctionOrValueDeclaration elementKey -> Anything elementItem) => elementItem);
if(is NestableDeclaration containerDeclaration=declaration.container){
assert(exists container = injector.create(containerDeclaration));
try{
return declaration.memberInvoke(container,[],parameters);
}catch(Exception x){
throw Error.member(x,container,parameters);
}
}else{
try{
return declaration.invoke([],*parameters);
}catch(Exception x){
throw Error(x,parameters);
}
}
}
}
29 changes: 29 additions & 0 deletions source/herd/depin/engine/injectable/ValueInjectable.ceylon
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import ceylon.language.meta.declaration {
NestableDeclaration,
ValueDeclaration
}
import herd.depin.api {
Injectable,
Creator
}
shared class ValueInjectable(ValueDeclaration declaration) extends Injectable(declaration){
shared actual Anything inject(Creator injector) {
if(is NestableDeclaration containerDeclaration=declaration.container){
assert(exists container = injector.create(containerDeclaration));
try{
return declaration.memberGet(container);
}catch(Exception x){
throw Error.member(x,container);
}
}else{
try{
return declaration.get();
}catch(Exception x){
throw Error(x);
}
}
}



}
1 change: 1 addition & 0 deletions source/herd/depin/engine/injectable/package.ceylon
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package herd.depin.engine.injectable;
39 changes: 39 additions & 0 deletions test/test/herd/depin/engine/integration/ClassInjectionTest.ceylon
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import ceylon.test {
test
}

import herd.depin.engine {
Depin
}

import test.herd.depin.engine.integration.model {
Person,
fixture,
DataSource,
DefaultParametersConstructor,
DefaultedParametersByFunction,
DefaultedParameterFunction
}
shared class ClassInjectionTest() {

Depin depin=Depin().include{
inclusions = {`package test.herd.depin.engine.integration.dependencies`};
};

shared test void shouldInjectJohnPerson(){
assert(depin.inject(`Person`)==fixture.person.john);
}

shared test void shouldInjectMysqlDataSource(){
assert(depin.inject(`DataSource`)==fixture.dataSouce.mysqlDataSource);
}
shared test void shouldInjectNonDefaultParameters(){
assert(depin.inject(`DefaultParametersConstructor`)==fixture.defaultParameter.instance);
}
shared test void shouldInjectDefaultedParameterFromFunction(){
assert(depin.inject(`DefaultedParametersByFunction`)==fixture.defaultedParameterByFunction.instance);
}
shared test void shouldInjectDefaultedParameterClassFunction(){
assert(depin.inject(`DefaultedParameterFunction`).fun()==fixture.defaultedParameterFunction.param);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import test.herd.depin.engine.integration.model {
fixture
}
import herd.depin.engine {
dependency
}
shared dependency String nonDefault=fixture.defaultParameter.nonDefault;


shared dependency String defaultedByFunction(String defaulted=fixture.defaultedParameterByFunction.param){
return defaulted;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
shared class DefaultParametersConstructor(shared String nonDefault,shared String defaultedParameter=fixture.defaultParameter.text) {


shared actual Boolean equals(Object that) {
if (is DefaultParametersConstructor that) {
return nonDefault==that.nonDefault &&
defaultedParameter==that.defaultedParameter;
}
else {
return false;
}
}

shared actual Integer hash {
variable value hash = 1;
hash = 31*hash + nonDefault.hash;
hash = 31*hash + defaultedParameter.hash;
return hash;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
shared class DefaultedParameterFunction(shared String fun()=> fixture.defaultedParameterFunction.param) {

shared actual Boolean equals(Object that) {
if (is DefaultedParameterFunction that) {
return true;
}
else {
return false;
}
}

shared actual Integer hash {
variable value hash = 1;
return hash;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
shared class DefaultedParametersByFunction(shared String defaultedByFunction) {


shared actual Boolean equals(Object that) {
if (is DefaultedParametersByFunction that) {
return defaultedByFunction==that.defaultedByFunction;
}
else {
return false;
}
}

shared actual Integer hash => defaultedByFunction.hash;

}
Loading

0 comments on commit b2c0dff

Please sign in to comment.