Skip to content

Commit

Permalink
💦 feat: Add a new example to inform how to deal with constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jictyvoo committed Jul 11, 2024
1 parent 2608c9c commit 152c329
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/dynamiconstructor/go.mod
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
)
4 changes: 4 additions & 0 deletions examples/dynamiconstructor/go.sum
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=
30 changes: 30 additions & 0 deletions examples/dynamiconstructor/main.go
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))
}
34 changes: 34 additions & 0 deletions examples/dynamiconstructor/newtypes.go
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
}
10 changes: 10 additions & 0 deletions examples/go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
go 1.20

use (
basic
bindlogger
dynamiconstructor
guessing_types

../remy
)

0 comments on commit 152c329

Please sign in to comment.