You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
crate/wireexample/cmd/example_01/provider.go

65 lines
989 B
Go

2 years ago
package main
import (
"fmt"
"math/rand"
"github.com/google/wire"
)
type Rander interface {
Rand() int
}
type DefaultRander struct{}
func (r *DefaultRander) Rand() int {
return rand.Int()
}
func NewRander() *DefaultRander {
return &DefaultRander{}
}
type MockRander struct {
Value int
}
func (r *MockRander) Rand() int {
return r.Value
}
type Guess struct {
rander Rander
}
func (g *Guess) Guess(guess int) string {
return fmt.Sprintf("you guess %d, real number is %d", guess, g.rander.Rand())
}
func NewGuess(rander Rander) *Guess {
return &Guess{
rander: rander,
}
}
type GuessWithMock struct {
Guess *Guess
Mock *MockRander
}
var GuessProviderSetWithoutMock = wire.NewSet(NewGuess)
var GuessProviderSet = wire.NewSet(
NewRander,
NewGuess,
wire.Bind(new(Rander), new(*DefaultRander)),
)
var MockGuessProviderSet = wire.NewSet(
wire.Struct(new(MockRander)),
NewGuess,
wire.Bind(new(Rander), new(*MockRander)),
wire.Struct(new(GuessWithMock), "*"),
)