-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🌋 Merge pull request #14 from Wrapped-Owls/develop
Remy 1.8.2 - Improve DuckType Get for factory + lazy singletone injections
- Loading branch information
Showing
14 changed files
with
367 additions
and
33 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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/wrapped-owls/goremy-di/examples/dynamiconstructor | ||
|
||
go 1.20 | ||
|
||
require github.com/wrapped-owls/goremy-di/remy v1.8.0 |
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,2 @@ | ||
github.com/wrapped-owls/goremy-di/remy v1.8.0 h1:dcCV+acizHoa1DJDjMN2nrH7oxi6Ftx7eda8wy2H+tY= | ||
github.com/wrapped-owls/goremy-di/remy v1.8.0/go.mod h1:u3y4TeiYnQNaEhKRbl3tyKPFpH8m/bV5wRenf4KmaDs= |
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,29 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/wrapped-owls/goremy-di/remy" | ||
) | ||
|
||
func registerInjections(ij remy.Injector) { | ||
remy.RegisterInstance(ij, "That's a nice test") | ||
remy.RegisterConstructor(ij, remy.Factory[time.Time], time.Now) | ||
remy.RegisterConstructorArgs2(ij, remy.Factory[Note], NewAnnotation) | ||
remy.RegisterConstructorArgs1(ij, remy.LazySingleton[FolderChecker], NewFolderChecker) | ||
} | ||
|
||
func main() { | ||
inj := remy.NewInjector(remy.Config{UseReflectionType: false, DuckTypeElements: true}) | ||
// Registering injections | ||
registerInjections(inj) | ||
|
||
folderChecker := remy.GetGenFunc[FolderChecker](inj, func(injector remy.Injector) error { | ||
remy.RegisterInstance(injector, "Trying to retrieve program current folder") | ||
return nil | ||
}) | ||
absPath := folderChecker.RunningAbsolute() | ||
log.Println(absPath) | ||
log.Println(remy.Get[string](inj)) | ||
} |
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,34 @@ | ||
package main | ||
|
||
import "time" | ||
|
||
// Note is a simple structure to hold an annotation | ||
type Note struct { | ||
Message string | ||
Time time.Time | ||
} | ||
|
||
// NewAnnotation is a constructor for Note | ||
func NewAnnotation(message string, currentTime time.Time) Note { | ||
return Note{ | ||
Message: message, | ||
Time: currentTime, | ||
} | ||
} | ||
|
||
// FolderChecker is an implementation of FileFolderChecker | ||
type FolderChecker struct { | ||
Path string | ||
} | ||
|
||
// NewFolderChecker is a constructor for FolderChecker | ||
func NewFolderChecker(note Note) FolderChecker { | ||
return FolderChecker{ | ||
Path: "/absolute/path/" + note.Message, | ||
} | ||
} | ||
|
||
// RunningAbsolute returns the absolute path | ||
func (fc FolderChecker) RunningAbsolute() string { | ||
return fc.Path | ||
} |
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,10 @@ | ||
go 1.20 | ||
|
||
use ( | ||
basic | ||
bindlogger | ||
dynamiconstructor | ||
guessing_types | ||
|
||
../remy | ||
) |
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,35 @@ | ||
package binds | ||
|
||
import "github.com/wrapped-owls/goremy-di/remy/internal/types" | ||
|
||
type bindWrapper[T any] struct { | ||
types.Bind[T] | ||
} | ||
|
||
func (b bindWrapper[T]) PointerValue() any { | ||
return new(T) | ||
} | ||
|
||
func (b bindWrapper[T]) GenAsAny(retriever types.DependencyRetriever) (any, error) { | ||
return b.Generates(retriever) | ||
} | ||
|
||
func Singleton[T any](binder types.Binder[T]) types.Bind[T] { | ||
return bindWrapper[T]{&SingletonBind[T]{binder: binder}} | ||
} | ||
|
||
func LazySingleton[T any](binder types.Binder[T]) types.Bind[T] { | ||
return bindWrapper[T]{&SingletonBind[T]{binder: binder, IsLazy: true}} | ||
} | ||
|
||
func Factory[T any](binder types.Binder[T]) types.Bind[T] { | ||
return bindWrapper[T]{FactoryBind[T]{binder: binder, IsFactory: true}} | ||
} | ||
|
||
func Instance[T any](element T) types.Bind[T] { | ||
return bindWrapper[T]{FactoryBind[T]{ | ||
binder: func(retriever types.DependencyRetriever) (T, error) { | ||
return element, nil | ||
}, | ||
}} | ||
} |
File renamed without changes.
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
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package remy | ||
|
||
import ( | ||
"github.com/wrapped-owls/goremy-di/remy/internal/types" | ||
) | ||
|
||
type ( | ||
// ConstructorEmpty defines a constructor function with no arguments that returns a value of type T and an error. | ||
ConstructorEmpty[T any] func() (T, error) | ||
|
||
// ConstructorArg1 defines a constructor function with one argument of type K that returns a value of type T and an error. | ||
ConstructorArg1[T, K any] func(K) (T, error) | ||
|
||
// ConstructorArg2 defines a constructor function with two arguments of types K and P that returns a value of type T and an error. | ||
ConstructorArg2[T, K, P any] func(K, P) (T, error) | ||
) | ||
|
||
// Binder calls the constructor function for ConstructorEmpty and returns the constructed value and any error encountered. | ||
func (cons ConstructorEmpty[T]) Binder(types.DependencyRetriever) (T, error) { | ||
return cons() | ||
} | ||
|
||
// Binder retrieves the dependency of type K, then calls the constructor function for ConstructorArg1 and returns the constructed value and any error encountered. | ||
func (cons ConstructorArg1[T, K]) Binder(retriever types.DependencyRetriever) (value T, err error) { | ||
var ( | ||
first K | ||
) | ||
if first, err = DoGet[K](retriever); err != nil { | ||
return | ||
} | ||
return cons(first) | ||
} | ||
|
||
// Binder retrieves the dependencies of types K and P, then calls the constructor function for ConstructorArg2 and returns the constructed value and any error encountered. | ||
func (cons ConstructorArg2[T, K, P]) Binder(retriever types.DependencyRetriever) (value T, err error) { | ||
var ( | ||
first K | ||
second P | ||
) | ||
if first, err = DoGet[K](retriever); err != nil { | ||
return | ||
} | ||
if second, err = DoGet[P](retriever); err != nil { | ||
return | ||
} | ||
|
||
return cons(first, second) | ||
} | ||
|
||
// RegisterConstructorErr registers a constructor function that returns a value of type T and an error. | ||
func RegisterConstructorErr[T any]( | ||
i Injector, bindFunc func(binder types.Binder[T]) Bind[T], | ||
constructor func() (T, error), keys ...string, | ||
) { | ||
var generator = ConstructorEmpty[T](constructor) | ||
Register(mustInjector(i), bindFunc(generator.Binder), keys...) | ||
} | ||
|
||
// RegisterConstructor registers a constructor function that returns a value of type T without an error. | ||
func RegisterConstructor[T any]( | ||
i Injector, bindFunc func(binder types.Binder[T]) Bind[T], | ||
constructor func() T, keys ...string, | ||
) { | ||
var generator ConstructorEmpty[T] = func() (T, error) { | ||
return constructor(), nil | ||
} | ||
RegisterConstructorErr(i, bindFunc, generator, keys...) | ||
} | ||
|
||
// RegisterConstructorArgs1Err registers a constructor function with one argument that returns a value of type T and an error. | ||
func RegisterConstructorArgs1Err[T, K any]( | ||
i Injector, bindFunc func(binder types.Binder[T]) Bind[T], | ||
constructor func(K) (T, error), keys ...string, | ||
) { | ||
generator := ConstructorArg1[T, K](constructor) | ||
Register(mustInjector(i), bindFunc(generator.Binder), keys...) | ||
} | ||
|
||
// RegisterConstructorArgs1 registers a constructor function with one argument that returns a value of type T without an error. | ||
func RegisterConstructorArgs1[T, K any]( | ||
i Injector, bindFunc func(binder types.Binder[T]) Bind[T], | ||
constructor func(K) T, keys ...string, | ||
) { | ||
generator := func(arg K) (T, error) { | ||
return constructor(arg), nil | ||
} | ||
RegisterConstructorArgs1Err(i, bindFunc, generator, keys...) | ||
} | ||
|
||
// RegisterConstructorArgs2Err registers a constructor function with two arguments that returns a value of type T and an error. | ||
func RegisterConstructorArgs2Err[T, K, P any]( | ||
i Injector, bindFunc func(binder types.Binder[T]) Bind[T], | ||
constructor func(K, P) (T, error), keys ...string, | ||
) { | ||
generator := ConstructorArg2[T, K, P](constructor) | ||
Register(mustInjector(i), bindFunc(generator.Binder), keys...) | ||
} | ||
|
||
// RegisterConstructorArgs2 registers a constructor function with two arguments that returns a value of type T without an error. | ||
func RegisterConstructorArgs2[T, K, P any]( | ||
i Injector, bindFunc func(binder types.Binder[T]) Bind[T], | ||
constructor func(K, P) T, keys ...string, | ||
) { | ||
generator := func(arg1 K, arg2 P) (T, error) { | ||
return constructor(arg1, arg2), nil | ||
} | ||
RegisterConstructorArgs2Err(i, bindFunc, generator, keys...) | ||
} |
Oops, something went wrong.