commit
a1aeeaf6b4
5 changed files with 251 additions and 0 deletions
@ -0,0 +1,46 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"io" |
|||
"os" |
|||
|
|||
lib "github.com/nmasse-itix/golang-fx" |
|||
"go.uber.org/fx" |
|||
) |
|||
|
|||
func childs() ([]lib.Child, error) { |
|||
c1, err := lib.NewChild(2) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
c2, err := lib.NewChild(5) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return []lib.Child{c1, c2}, nil |
|||
} |
|||
|
|||
func main() { |
|||
fx.New( |
|||
fx.Supply( |
|||
fx.Annotated{Name: "house_name", Target: "New-York"}, |
|||
fx.Annotated{Name: "john_name", Target: "John"}, |
|||
fx.Annotated{Name: "jane_name", Target: "Jane"}, |
|||
fx.Annotated{Name: "cat1_name", Target: "Gros Minet"}, |
|||
fx.Annotated{Name: "cat2_name", Target: "Isidore"}, |
|||
), |
|||
fx.Provide( |
|||
fx.Annotate(lib.NewCat, fx.ParamTags(`name:"cat1_name"`), fx.ResultTags(`group:"cats"`)), |
|||
fx.Annotate(lib.NewCat, fx.ParamTags(`name:"cat2_name"`), fx.ResultTags(`group:"cats"`)), |
|||
fx.Annotate(lib.NewAdult, fx.ParamTags(`name:"john_name"`, `group:"childs"`), fx.ResultTags(`group:"adults"`)), |
|||
fx.Annotate(lib.NewAdult, fx.ParamTags(`name:"jane_name"`, `group:"childs"`), fx.ResultTags(`group:"adults"`)), |
|||
fx.Annotate(childs, fx.ResultTags(`group:"childs"`)), |
|||
fx.Annotate(lib.NewHouse, fx.ParamTags(`name:"house_name"`, `group:"adults"`, `group:"cats"`)), |
|||
func() io.Writer { return os.Stdout }, |
|||
), |
|||
fx.Invoke(lib.House.Present), |
|||
fx.NopLogger, |
|||
).Run() |
|||
} |
|||
@ -0,0 +1,5 @@ |
|||
module github.com/nmasse-itix/golang-fx |
|||
|
|||
go 1.16 |
|||
|
|||
require go.uber.org/fx v1.17.0 // indirect |
|||
@ -0,0 +1,67 @@ |
|||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= |
|||
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= |
|||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
|||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
|||
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= |
|||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= |
|||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= |
|||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= |
|||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= |
|||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= |
|||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |
|||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= |
|||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= |
|||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= |
|||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= |
|||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= |
|||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= |
|||
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk= |
|||
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= |
|||
go.uber.org/dig v1.14.0 h1:VmGvIH45/aapXPQkaOrK5u4B5B7jxZB98HM/utx0eME= |
|||
go.uber.org/dig v1.14.0/go.mod h1:jHAn/z1Ld1luVVyGKOAIFYz/uBFqKjjEEdIqVAqfQ2o= |
|||
go.uber.org/fx v1.17.0 h1:e65QHcKzyD58oP6UaA7aYF96XRvnN0pF/rHnwSeRc6I= |
|||
go.uber.org/fx v1.17.0/go.mod h1:SPbDO5WX0JpqR7C/JW09j7WC+gWI8kRG1uGM9mGsIH0= |
|||
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= |
|||
go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A= |
|||
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= |
|||
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= |
|||
go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM= |
|||
go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ= |
|||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= |
|||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= |
|||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= |
|||
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= |
|||
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= |
|||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= |
|||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= |
|||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= |
|||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= |
|||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= |
|||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= |
|||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= |
|||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= |
|||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
|||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
|||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
|||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
|||
golang.org/x/sys v0.0.0-20210903071746-97244b99971b h1:3Dq0eVHn0uaQJmPO+/aYPI/fRMqdrVDbu7MQcku54gg= |
|||
golang.org/x/sys v0.0.0-20210903071746-97244b99971b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
|||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= |
|||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= |
|||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= |
|||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |
|||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= |
|||
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= |
|||
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= |
|||
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= |
|||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= |
|||
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= |
|||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
|||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
|||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
|||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
|||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
|||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= |
|||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
|||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
|||
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= |
|||
@ -0,0 +1,108 @@ |
|||
package lib |
|||
|
|||
import ( |
|||
"fmt" |
|||
"io" |
|||
"log" |
|||
"strings" |
|||
) |
|||
|
|||
type Cat interface { |
|||
String() string |
|||
} |
|||
|
|||
type MyCat struct { |
|||
Name string |
|||
} |
|||
|
|||
func (c *MyCat) String() string { |
|||
return fmt.Sprintf("Cat: %s", c.Name) |
|||
} |
|||
|
|||
func NewCat(name string) Cat { |
|||
log.Default().Printf("NewCat(%s)", name) |
|||
return &MyCat{Name: name} |
|||
} |
|||
|
|||
type Child interface { |
|||
String() string |
|||
} |
|||
|
|||
type MyChild struct { |
|||
Age int |
|||
} |
|||
|
|||
func NewChild(age int) (Child, error) { |
|||
log.Default().Printf("NewChild(%d)", age) |
|||
if age < 0 { |
|||
return nil, fmt.Errorf("wrong age") |
|||
} |
|||
|
|||
return &MyChild{Age: age}, nil |
|||
} |
|||
|
|||
func (c *MyChild) String() string { |
|||
return fmt.Sprintf("%d year old child", c.Age) |
|||
} |
|||
|
|||
type Adult interface { |
|||
String() string |
|||
} |
|||
|
|||
type MyAdult struct { |
|||
Name string |
|||
Childs []Child |
|||
} |
|||
|
|||
func NewAdult(name string, childs []Child) Adult { |
|||
log.Default().Printf("NewAdult(%s, %d childs)", name, len(childs)) |
|||
return &MyAdult{Name: name, Childs: childs} |
|||
} |
|||
|
|||
func (a *MyAdult) String() string { |
|||
var b strings.Builder |
|||
b.WriteString(a.Name) |
|||
if len(a.Childs) > 0 { |
|||
b.WriteString(" (with ") |
|||
for i, c := range a.Childs { |
|||
if i > 0 { |
|||
b.WriteString(", ") |
|||
} |
|||
b.WriteString(c.String()) |
|||
} |
|||
b.WriteString(")") |
|||
} |
|||
return b.String() |
|||
} |
|||
|
|||
type House interface { |
|||
Present(w io.Writer) |
|||
} |
|||
|
|||
type MyHouse struct { |
|||
Address string |
|||
Adults []Adult |
|||
Cats []Cat |
|||
} |
|||
|
|||
func (h *MyHouse) Present(w io.Writer) { |
|||
fmt.Fprintf(w, "House at: %s\n", h.Address) |
|||
fmt.Fprintln(w, "with:") |
|||
for _, a := range h.Adults { |
|||
fmt.Fprintf(w, "- %s\n", a) |
|||
} |
|||
fmt.Fprintln(w, "and:") |
|||
for _, c := range h.Cats { |
|||
fmt.Fprintf(w, "- %s\n", c) |
|||
} |
|||
fmt.Fprintln(w) |
|||
} |
|||
|
|||
func NewHouse(address string, adults []Adult, cats []Cat) House { |
|||
log.Default().Printf("NewHouse(%s, %d adults, %d cats)", address, len(adults), len(cats)) |
|||
return &MyHouse{ |
|||
Address: address, |
|||
Adults: adults, |
|||
Cats: cats, |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"os" |
|||
|
|||
lib "github.com/nmasse-itix/golang-fx" |
|||
) |
|||
|
|||
func main() { |
|||
gm := lib.NewCat("Gros Minet") |
|||
isidore := lib.NewCat("Isidore") |
|||
c1, err := lib.NewChild(2) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
c2, err := lib.NewChild(5) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
john := lib.NewAdult("John", []lib.Child{c1, c2}) |
|||
jane := lib.NewAdult("Jane", []lib.Child{c1, c2}) |
|||
house := lib.NewHouse("New-York", []lib.Adult{john, jane}, []lib.Cat{gm, isidore}) |
|||
|
|||
house.Present(os.Stdout) |
|||
} |
|||
Loading…
Reference in new issue