Skip to content

Commit

Permalink
Rename fx.Inject to fx.Extract (uber-go#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grayson Koonce authored Jul 27, 2017
1 parent 524726e commit e5aaf54
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## v1.0.0-rc3 (unreleased)

- `fx.Inject` now supports `fx.In` tags on target structs.
- **[Breaking]** Rename `fx.Inject` to `fx.Extract`.
- `fx.Extract` now supports `fx.In` tags on target structs.

## v1.0.0-rc2 (21 Jul 2017)

Expand Down
6 changes: 3 additions & 3 deletions inject.go → extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ import (

var _typeOfIn = reflect.TypeOf(In{})

// Inject fills the given struct with values from the dependency injection
// Extract fills the given struct with values from the dependency injection
// container on application start.
//
// The target MUST be a pointer to a struct. Only exported fields will be
// filled.
func Inject(target interface{}) Option {
func Extract(target interface{}) Option {
v := reflect.ValueOf(target)

if t := v.Type(); t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
return Invoke(func() error {
return fmt.Errorf("Inject expected a pointer to a struct, got a %v", t)
return fmt.Errorf("Extract expected a pointer to a struct, got a %v", t)
})
}

Expand Down
42 changes: 21 additions & 21 deletions inject_test.go → extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"go.uber.org/dig"
)

func TestInject(t *testing.T) {
func TestExtract(t *testing.T) {
type type1 struct{}
type type2 struct{}
type type3 struct{}
Expand All @@ -53,11 +53,11 @@ func TestInject(t *testing.T) {
t.Run(fmt.Sprintf("%T", tt), func(t *testing.T) {
app := fxtest.New(t,
Provide(func() *bytes.Buffer { return &bytes.Buffer{} }),
Inject(&tt),
Extract(&tt),
)
err := app.Start(context.Background())
require.Error(t, err)
assert.Contains(t, err.Error(), "Inject expected a pointer to a struct")
assert.Contains(t, err.Error(), "Extract expected a pointer to a struct")
})
}
})
Expand All @@ -69,12 +69,12 @@ func TestInject(t *testing.T) {
var out struct{}
app := fxtest.New(t,
Provide(new1, new2),
Inject(&out),
Extract(&out),
)
app.MustStart().MustStop()
})

t.Run("StructIsInjected", func(t *testing.T) {
t.Run("StructIsExtracted", func(t *testing.T) {
var gave1 *type1
new1 := func() *type1 {
gave1 = &type1{}
Expand All @@ -94,7 +94,7 @@ func TestInject(t *testing.T) {

app := fxtest.New(t,
Provide(new1, new2),
Inject(&out),
Extract(&out),
)

defer app.MustStart().MustStop()
Expand All @@ -117,7 +117,7 @@ func TestInject(t *testing.T) {

app := fxtest.New(t,
Provide(new1),
Inject(&out),
Extract(&out),
)

defer app.MustStart().MustStop()
Expand All @@ -129,7 +129,7 @@ func TestInject(t *testing.T) {
new1 := func() *type1 { return &type1{} }
var out struct{ *type1 }

app := fxtest.New(t, Provide(new1), Inject(&out))
app := fxtest.New(t, Provide(new1), Extract(&out))
defer app.MustStart().MustStop()

// Unexported fields are left unchanged.
Expand All @@ -141,7 +141,7 @@ func TestInject(t *testing.T) {
new4 := func() type4 { return type4{"foo"} }
var out struct{ type4 }

app := fxtest.New(t, Provide(new4), Inject(&out))
app := fxtest.New(t, Provide(new4), Extract(&out))
defer app.MustStart().MustStop()

// Unexported fields are left unchanged.
Expand All @@ -163,7 +163,7 @@ func TestInject(t *testing.T) {

app := fxtest.New(t,
Provide(new1),
Inject(&out),
Extract(&out),
)

defer app.MustStart().MustStop()
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestInject(t *testing.T) {

app := fxtest.New(t,
Provide(new1, new2, new3),
Inject(&out),
Extract(&out),
)

defer app.MustStart().MustStop()
Expand Down Expand Up @@ -225,7 +225,7 @@ func TestInject(t *testing.T) {

app := fxtest.New(t,
Provide(new1, new2),
Inject(&out),
Extract(&out),
)

defer app.MustStart().MustStop()
Expand All @@ -237,7 +237,7 @@ func TestInject(t *testing.T) {

t.Run("TopLevelDigIn", func(t *testing.T) {
var out struct{ dig.In }
app := fxtest.New(t, Inject(&out))
app := fxtest.New(t, Extract(&out))
defer app.MustStart().MustStop()
})

Expand All @@ -248,7 +248,7 @@ func TestInject(t *testing.T) {
var out struct{ In }
app := fxtest.New(t,
Provide(new1, new2),
Inject(&out),
Extract(&out),
)

defer app.MustStart().MustStop()
Expand All @@ -272,7 +272,7 @@ func TestInject(t *testing.T) {

app := fxtest.New(t,
Provide(new1),
Inject(&out),
Extract(&out),
)

defer app.MustStart().MustStop()
Expand All @@ -294,7 +294,7 @@ func TestInject(t *testing.T) {

app := fxtest.New(t,
Provide(func() int { return 42 }),
Inject(&out),
Extract(&out),
)
defer app.MustStart().MustStop()
assert.Equal(t, 42, out.B.C, "B.C must match")
Expand All @@ -314,7 +314,7 @@ func TestInject(t *testing.T) {

app := fxtest.New(t,
Provide(new1),
Inject(&out),
Extract(&out),
)

defer app.MustStart().MustStop()
Expand All @@ -325,22 +325,22 @@ func TestInject(t *testing.T) {
})
}

func ExampleInject() {
func ExampleExtract() {
var target struct {
Logger *log.Logger
}

app := New(
Provide(func() *log.Logger { return log.New(os.Stdout, "", 0) }),
Inject(&target),
Extract(&target),
)

if err := app.Start(context.Background()); err != nil {
log.Fatal(err)
}

target.Logger.Print("Injected!")
target.Logger.Print("Extracted!")

// Output:
// Injected!
// Extracted!
}

0 comments on commit e5aaf54

Please sign in to comment.