How do I get the following working?
To the lifecycle function, I need to pass 2 different implementations of Foo.
package main
import (
"context"
"fmt"
"go.uber.org/fx"
)
type Foo interface {
Print()
}
type Bar struct{}
func (b *Bar) Print() {
fmt.Println("I'm bar")
}
type Baz struct{}
func (b *Baz) Print() {
fmt.Println("I'm baz")
}
func foo() Foo {
return &Bar{}
}
func anotherFoo() Foo {
return &Baz{}
}
func main() {
workingStart() //This works
//nonWorkingStart() //This does not
}
func nonWorkingStart() {
app := fx.New(
fx.Provide(fx.Annotate(foo, fx.ResultTags(`name:"bar"`))),
fx.Provide(fx.Annotate(anotherFoo, fx.ResultTags(`name:"baz"`))),
//How to configure nonWorkingRun having 2 different foo's in its arguments?
fx.Invoke(nonWorkingRun),
)
if app.Err() != nil {
panic(fmt.Errorf("unable to bootstrap app: %w", app.Err()))
}
app.Run()
}
func workingStart() {
app := fx.New(
fx.Provide(foo),
fx.Invoke(workingRun),
)
if app.Err() != nil {
panic(fmt.Errorf("unable to bootstrap app: %w", app.Err()))
}
app.Run()
}
func nonWorkingRun(lifecycle fx.Lifecycle, foo Foo, anotherFoo Foo) {
lifecycle.Append(
fx.Hook{
OnStart: func(context.Context) error {
foo.Print()
anotherFoo.Print()
return nil
},
},
)
}
func workingRun(lifecycle fx.Lifecycle, foo Foo) {
lifecycle.Append(
fx.Hook{
OnStart: func(context.Context) error {
foo.Print()
return nil
},
},
)
}
I got it working implementing it the following way. Not sure if there are other ways other than using a struct holding a slice of Foo's and building that struct using fx.ParamTags
type FooSlices struct {
fooSlices []Foo
}
func fooSlices(foo, anotherFoo Foo) FooSlices {
return FooSlices{fooSlices: []Foo{foo, anotherFoo}}
}
func main() {
//workingStart()
nonWorkingStart()
}
func nonWorkingStart() {
app := fx.New(
fx.Provide(fx.Annotate(foo, fx.ResultTags(`name:"bar"`))),
fx.Provide(fx.Annotate(anotherFoo, fx.ResultTags(`name:"baz"`))),
fx.Provide(fx.Annotate(fooSlices, fx.ParamTags(`name:"bar"`, `name:"baz"`))),
fx.Invoke(nonWorkingRun),
)
if app.Err() != nil {
panic(fmt.Errorf("unable to bootstrap app: %w", app.Err()))
}
app.Run()
}
func nonWorkingRun(lifecycle fx.Lifecycle, fooSlices FooSlices) {
lifecycle.Append(
fx.Hook{
OnStart: func(context.Context) error {
for _, foo := range fooSlices.fooSlices {
foo.Print()
}
return nil
},
},
)
}
Collecting a slice of inputs is fairly hard to reason about, also not very extensible and goes against the spirit of what fx is "meant" to do (hide boilerplate code).
Just a small amount of refactors first
fx.Provide can take in a range of inputs and just needs to be called once
n implementations of foo requires only n-1 annotations, as the first one can be the raw type, and the remaining being the annotated types
avoid wrapping the panic and let it happen naturally if it happens; this would imply a syntax issue which can be mitigated prior to pushing the code
fx.New(
fx.Provide(
foo,
fx.Annotate(foo, fx.ResultTags(`name:"baz"`)),
),
fx.Invoke(toBeInvoked),
).Run()
type Params struct {
fx.In
Lifecycle fx.Lifecycle
Bar foo
Baz foo `name:"baz"`
}
func toBeInvoked(p Params) {
p.Lifecycle.Append(
fx.Hook{
OnStart: func(_ context.Context) error {
p.Bar.Print()
p.Baz.Print()
return nil
},
},
)
}
There are other ways to do this as well in fx, this one is a fairly common pattern.
Related
Suppose I have some structs defined in package pkg:
package pkg
type Foo struct {
FieldA string
FieldB string
}
type Bar struct {
FieldA string
FieldB string
}
func (Foo) Show() {
fmt.Println("Foo")
}
func (Bar) Show() {
fmt.Println("Bar")
}
type Showable interface {
Show()
}
Registry := map[string]Showable{
//not sure about value type^
"Foo": Foo, // staticcheck shows: not a type
"Bar": Bar, //
}
And I want to instantiate the structs dynamically; something like this:
package main
import "url/user/pkg"
func main() {
foo := pkg.Registry["Foo"]{
FieldA: "A",
FieldB: "B",
}
bar := pkg.Registry["Bar"]{
FieldA: "X",
FieldB: "Y",
}
foo.Show()
bar.Show()
}
The above clearly doesn't work.
Is it possible to achieve this? I am new to go. I have looked at reflect, I have tried to build the Registry with pointers, pointers of empty instance, but couldn't figure out a way to do this.
Ultimately, I am trying to write a command line utility to change themes of certain programs. I have written program specific methods (like Show in above example), and I am trying to read the program specific params from a config.json file, and create the instances dynamically.
If I correctly understand what you are trying to achieve, here is
the way to do that:
registry.go:
package pkg
import (
"fmt"
"io"
)
type NewShowable func(r io.Reader) Showable
type Showable interface {
Show()
}
type Foo struct {
FieldA string
FieldB string
}
func newFoo(r io.Reader) Showable {
// Read config from r and construct Foo
return Foo{}
}
func (Foo) Show() {
fmt.Println("Foo")
}
type Bar struct {
FieldA string
FieldB string
}
func newBar(r io.Reader) Showable {
// Read config from r and construct Bar
return Bar{}
}
func (Bar) Show() {
fmt.Println("Bar")
}
var Registry = map[string]NewShowable{
"Foo": newFoo,
"Bar": newBar,
}
main.go:
package main
import (
"log"
"os"
"url/user/pkg"
)
func main() {
f, err := os.Open("config.json")
if err != nil {
log.Fatalln(err)
}
defer f.Close()
foo := pkg.Registry["Foo"](f)
f2, err := os.Open("config2.json")
if err != nil {
log.Fatalln(err)
}
defer f2.Close()
bar := pkg.Registry["Bar"](f2)
foo.Show()
bar.Show()
}
I have a dilemma to store some data in an unstructured field.
Let's say I have these to store:
package main
type FoodType string
const (
BeefFood FoodType = "beef"
ChickenFood FoodType = "chicken"
)
type FoodI interface {
Prepare()
GetType() FoodType
}
type FoodContainer struct {
FoodI
}
type Chicken struct {
Way string
}
func (c *Chicken) Prepare() {}
func (c *Chicken) GetType() FoodType {
return ChickenFood
}
type Beef struct {
Way string
}
func (b *Beef) Prepare() {}
func (b *Beef) GetType() FoodType {
return BeefFood
}
Let's say I choose json as a serializer (I will probably go for msgpack, but that's another story).
Which way do you think is the most appropriate:
Solution 1
one container that contains all the message types, and simply use the default marshal/unmarshal:
package main
type FoodContainer struct {
Chicken *Chicken `json:"chicken"`
Beef *Beef `json:"beef"`
}
And then, I just need to check which of the field is nil to know what to do with it.
Solution 2
A more "typed" solution, with interfaces and FoodType
package main
import (
"encoding/json"
"fmt"
)
type FoodContainer struct {
FoodType
FoodI
}
func (fc *FoodContainer) MarshalJSON() ([]byte, error) {
return json.Marshal(&FoodContainerT{
FoodType: fc.FoodI.GetType(),
FoodI: fc.FoodI,
})
}
func (fc *FoodContainer) UnmarshalJSON(b []byte) error {
var fct struct {
FoodType
}
if err := json.Unmarshal(b, &fct); err != nil {
return fmt.Errorf("fc UnmarshalJSON: %v", err)
}
switch fct.FoodType {
case ChickenFood:
fmt.Printf("Yeah, Chickenfood: %v\n", fct.FoodType)
var fi struct {
FoodI Chicken
}
if err := json.Unmarshal(b, &fi); err != nil {
return fmt.Errorf("chicken UnmarshalJSON: %v", err)
}
fmt.Printf("%#v", fi.FoodI)
default:
return fmt.Errorf("no known FoodType found")
}
return nil
}
I benchmarked both, they have around the same processing time and same mem usage, but I'm wondering which of them could be more idiomatic?
I'm in the process of learning go and am adapting a Java Game of Life example from testdouble. However, the test spy I have written incorrectly compares equality of my World struct - the test passes when it should fail, since output(world) is not being called. What am I doing incorrectly?
Test:
package gameoflife
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestZeroGenerations(t *testing.T) {
generatesSeedWorldStub := GeneratesSeedWorldStub{}
outputsWorldSpy := OutputsWorldSpy{}
conway := NewSimulatesConway(&generatesSeedWorldStub, &outputsWorldSpy)
seedWorld := World{}
conway.simulate()
correctWorld := outputsWorldSpy.wasOutputCalledWithWorld(seedWorld)
if !correctWorld {
t.Errorf("Output called with seed world, expected: %t, got: %t", true, correctWorld)
}
}
type GeneratesSeedWorldStub struct{}
func (gsw *GeneratesSeedWorldStub) generate() World {
return World{}
}
type OutputsWorldSpy struct {
outputCalledWithWorld World
}
func (ow *OutputsWorldSpy) output(world World) {
ow.outputCalledWithWorld = world
}
func (ow *OutputsWorldSpy) wasOutputCalledWithWorld(world World) bool {
return cmp.Equal(world, ow.outputCalledWithWorld)
}
Implementation:
package gameoflife
type SimulatesConway struct {
generatesSeedWorld GeneratesSeedWorld
outputsWorld OutputsWorld
}
func NewSimulatesConway(generatesSeedWorld GeneratesSeedWorld, outputsWorld OutputsWorld) SimulatesConway {
return SimulatesConway{generatesSeedWorld: generatesSeedWorld, outputsWorld: outputsWorld}
}
func (sc *SimulatesConway) simulate() {
// seedWorld := sc.generatesSeedWorld.generate()
// sc.outputsWorld.output(seedWorld)
}
type GeneratesSeedWorld interface {
generate() World
}
type OutputsWorld interface {
output(world World)
}
type World struct{}
When called outputsWorldSpy := OutputsWorldSpy{} golang assigned default value in outputsWorldSpy.outputCalledWithWorld = World{} and you assigned seedWorld := World{}. So they are same that's why test passed. If you want to handle that case, i suggest to use pointer.
type OutputsWorldSpy struct {
outputCalledWithWorld *World
}
func (ow *OutputsWorldSpy) output(world World) {
ow.outputCalledWithWorld = &world
}
func (ow *OutputsWorldSpy) wasOutputCalledWithWorld(world World) bool {
if ow.outputCalledWithWorld == nil {
return false
}
return cmp.Equal(world, *ow.outputCalledWithWorld)
}
Should I check for nil values in the constructor and then set an unexported struct field, or make the default struct value useful by checking for nil at method level?
type Foo struct{}
func (f *Foo) Baz() {}
var DefaultFoo = new(Foo)
type Bar struct {
Foo *Foo
}
func (b *Bar) Baz() {
if b.Foo == nil {
DefaultFoo.Baz()
} else {
b.Foo.Baz()
}
}
or
type Foo struct{}
func (f *Foo) Baz() {}
var DefaultFoo = new(Foo)
type Bar struct {
foo *Foo
}
func NewBar(foo *Foo) *Bar {
if foo == nil {
foo = DefaultFoo
}
return &Bar{foo}
}
func (b *Bar) Baz() {
b.foo.Baz()
}
I don't think there is a "right" answer for this.
Having said this, the approach usually seen in the Go base libraries is letting the objects be created without any constructor, with nil or zero values in its fields, and then make the methods have logic to use or return useful defaults.
Take a look at the http.Client implementation for example:
https://github.com/golang/go/blob/master/src/net/http/client.go
It will basically let you create a new client by just doing:
client := &http.Client{}
You can then populate the fields of the object if you want to override defaults, otherwise it'll check for nil in different methods to provide default behaviour, for example:
https://github.com/golang/go/blob/master/src/net/http/client.go#L195
func (c *Client) transport() RoundTripper {
if c.Transport != nil {
return c.Transport
}
return DefaultTransport
}
I'm not quite sure how to address this question, please feel free to edit.
With the first code block below, I am able to check if a all fields of a struct are nil.
In reality however, the values injected in the struct, are received as args.Review (see second code block below).
In the second code block, how can I check if all fields from args.Review are nil?
Try it on Golang Playground
package main
import (
"fmt"
"reflect"
)
type review struct {
Stars *int32 `json:"stars" bson:"stars,omitempty" `
Commentary *string `json:"commentary" bson:"commentary,omitempty"`
}
func main() {
newReview := &review{
Stars: nil,
// Stars: func(i int32) *int32 { return &i }(5),
Commentary: nil,
// Commentary: func(i string) *string { return &i }("Great"),
}
if reflect.DeepEqual(review{}, *newReview) {
fmt.Println("Nothing")
} else {
fmt.Println("Hello")
}
}
Try the second code on Golang Playground
This code below gets two errors:
prog.go:32:14: type args is not an expression
prog.go:44:27: args.Review is not a type
package main
import (
"fmt"
"reflect"
)
type review struct {
Stars *int32 `json:"stars" bson:"stars,omitempty" `
Commentary *string `json:"commentary" bson:"commentary,omitempty"`
}
type reviewInput struct {
Stars *int32
Commentary *string
}
type args struct {
PostSlug string
Review *reviewInput
}
func main() {
f := &args {
PostSlug: "second-post",
Review: &reviewInput{
Stars: func(i int32) *int32 { return &i }(5),
Commentary: func(i string) *string { return &i }("Great"),
},
}
createReview(args)
}
func createReview(args *struct {
PostSlug string
Review *reviewInput
}) {
g := &review{
Stars: args.Review.Stars,
Commentary: args.Review.Commentary,
}
if reflect.DeepEqual(args.Review{}, nil) {
fmt.Println("Nothing")
} else {
fmt.Println("Something")
}
}
If you're dealing with a small number of fields you should use simple if statements to determine whether they are nil or not.
if args.Stars == nil && args.Commentary == nil {
// ...
}
If you're dealing with more fields than you would like to manually spell out in if statements you could use a simple helper function that takes a variadic number of interface{} arguments. Just keep in mind that there is this: Check for nil and nil interface in Go
func AllNil(vv ...interface{}) bool {
for _, v := range vv {
if v == nil {
continue
}
if rv := reflect.ValueOf(v); !rv.IsNil() {
return false
}
}
return true
}
if AllNil(args.Stars, args.Commentary, args.Foo, args.Bar, args.Baz) {
// ...
}
Or you can use the reflect package to do your bidding.
func NilFields(x interface{}) bool {
rv := reflect.ValueOf(args)
rv = rv.Elem()
for i := 0; i < rv.NumField(); i++ {
if f := rv.Field(i); f.IsValid() && !f.IsNil() {
return false
}
}
return true
}
if NilFields(args) {
// ...
}