-
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.
💦 feat: Add a new example to inform how to deal with constructors
- Loading branch information
Showing
5 changed files
with
86 additions
and
0 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 @@ | ||
module github.com/wrapped-owls/goremy-di/examples/dynamiconstructor | ||
|
||
go 1.20 | ||
|
||
require ( | ||
github.com/mattn/go-sqlite3 v1.14.22 | ||
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,4 @@ | ||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= | ||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= | ||
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,30 @@ | ||
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.Factory[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 | ||
) |