-
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.
Issue #8: convert Container to an interface.
- Loading branch information
Showing
6 changed files
with
192 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/fgm/izidic/examples/di" | ||
) | ||
|
||
func main() { | ||
dic := di.Resolve(os.Stdout, os.Args[0], os.Args[1:]) | ||
app := dic.MustService("app").(di.App) | ||
log.Printf("app: %#v\n", app) | ||
if err := app(); err != nil { | ||
os.Exit(1) | ||
} | ||
} |
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,12 @@ | ||
package di | ||
|
||
import "log" | ||
|
||
type App func() error | ||
|
||
func makeAppFeature(name string, logger *log.Logger) App { | ||
return func() error { | ||
logger.Println(name) | ||
return nil | ||
} | ||
} |
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,49 @@ | ||
package di | ||
|
||
import ( | ||
"io" | ||
"log" | ||
|
||
"github.com/fgm/izidic" | ||
) | ||
|
||
type Container struct { | ||
izidic.Container | ||
} | ||
|
||
// Logger is a typed service accessor. | ||
func (c *Container) Logger() *log.Logger { | ||
return c.MustService("logger").(*log.Logger) | ||
} | ||
|
||
// Name is a types parameter accessor. | ||
func (c *Container) Name() string { | ||
return c.MustParam("name").(string) | ||
} | ||
|
||
// loggerService is an izidic.Service also containing a one-time initialization action. | ||
func loggerService(dic izidic.Container) (any, error) { | ||
w := dic.MustParam("writer").(io.Writer) | ||
log.SetOutput(w) // Support dependency code not taking an injected logger. | ||
logger := log.New(w, "", log.LstdFlags) | ||
return logger, nil | ||
} | ||
|
||
func appService(dic izidic.Container) (any, error) { | ||
wdic := Container{dic} // wrapped Container with typed accessors | ||
logger := wdic.Logger() // typed service instance | ||
name := wdic.Name() // typed parameter value | ||
appFeature := makeAppFeature(name, logger) | ||
return appFeature, nil | ||
} | ||
|
||
func Resolve(w io.Writer, name string, args []string) izidic.Container { | ||
dic := izidic.New() | ||
dic.Store("name", name) | ||
dic.Store("writer", w) | ||
dic.Register("logger", loggerService) | ||
dic.Register("app", appService) | ||
// others... | ||
dic.Freeze() | ||
return dic | ||
} |
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.