-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Very basic implemetation, can inject Values and Classes, fixes to sca…
…n and dependency identifications. Unit tests
- Loading branch information
Showing
25 changed files
with
354 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import ceylon.language.meta.declaration { | ||
Declaration | ||
} | ||
|
||
shared interface Creator{ | ||
shared formal Anything create(Declaration declaration); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
shared alias Identification=> {Annotation*}; | ||
shared class Identification(shared Annotation* annotations){ | ||
|
||
|
||
shared actual Boolean equals(Object that) { | ||
if (is Identification that) { | ||
return annotations.containsEvery(that.annotations); | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
|
||
shared actual Integer hash => annotations.hash; | ||
|
||
string => annotations.string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import herd.depin.api { | ||
Registry, | ||
Creator, | ||
Resolver | ||
} | ||
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``"); | ||
} | ||
|
||
|
||
|
||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,93 @@ | ||
import ceylon.language.meta.declaration { | ||
Declaration, | ||
FunctionOrValueDeclaration, | ||
FunctionDeclaration, | ||
ValueDeclaration, | ||
CallableConstructorDeclaration, | ||
ClassDeclaration, | ||
NestableDeclaration, | ||
ConstructorDeclaration, | ||
GenericDeclaration | ||
} | ||
|
||
import herd.depin.api { | ||
Provider, | ||
Injectable, | ||
Dependency, | ||
Registry | ||
Registry, | ||
Creator | ||
} | ||
import ceylon.language.meta.model { | ||
Type | ||
shared class DefaultProvider(Registry registry) satisfies Provider{ | ||
shared actual Injectable provide(Declaration declaration, Dependency dependency) { | ||
if(is GenericDeclaration declaration,!declaration.typeParameterDeclarations.empty){ | ||
throw Exception("Type parameters are not supported yet in dependencies ``declaration``"); | ||
} | ||
switch(declaration) | ||
case (is FunctionDeclaration) { | ||
return FunctionInjectable(declaration); | ||
} | ||
case( is ValueDeclaration){ | ||
return ValueInjectable(declaration); | ||
} | ||
case (is CallableConstructorDeclaration){ | ||
return ConstructorInjectable(declaration); | ||
} | ||
case (is ClassDeclaration){ | ||
if(exists defaultConstructor= declaration.defaultConstructor){ | ||
return ConstructorInjectable(defaultConstructor); | ||
} | ||
value 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` ``"); | ||
} | ||
else if(constructors.size>1){ | ||
throw Exception("Can't select injection target, multiple constructors annotated `` `class TargetAnnotation` ``"); | ||
} | ||
assert(is CallableConstructorDeclaration targetConstructor=constructors.first); | ||
return ConstructorInjectable(targetConstructor); | ||
} | ||
else{ | ||
throw Exception("Declaration ``declaration``not supported "); | ||
} | ||
} | ||
|
||
} | ||
import ceylon.language.meta.declaration { | ||
Declaration | ||
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); | ||
} | ||
|
||
|
||
} | ||
shared class DefaultProvider(Registry registry) satisfies Provider{ | ||
shared actual Injectable provide(Declaration declaration, Dependency dependency) => nothing; | ||
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(); | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,35 @@ | ||
import herd.depin.api { | ||
Resolver, | ||
Dependency, | ||
Registry | ||
Registry, | ||
Identification | ||
} | ||
import ceylon.language.meta.declaration { | ||
Declaration | ||
Declaration, | ||
AnnotatedDeclaration, | ||
TypedDeclaration | ||
} | ||
import ceylon.language.meta { | ||
type | ||
} | ||
|
||
|
||
shared class DefaultResolver(Registry registry) satisfies Resolver{ | ||
shared actual Dependency resolve(Declaration declaration) => nothing; | ||
shared actual Dependency resolve(Declaration declaration) { | ||
if(is TypedDeclaration&AnnotatedDeclaration declaration){ | ||
|
||
value annotations = declaration.annotations<Annotation>() | ||
.select((Annotation element) => registry.controls.contains(type(element))); | ||
return Dependency{ | ||
type = declaration.openType; | ||
identification = if (annotations.empty && registry.controls.contains(`NamedAnnotation`)) | ||
then Identification(NamedAnnotation(declaration.name)) | ||
else Identification(*annotations); | ||
|
||
}; | ||
} | ||
throw Exception("``declaration`` not supported"); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.