Repository: uber-go/fx
Pull request #741 (2021-06-04 00:29:08)

fx.In: Support unexported fields
This adds support for `ignore-unexported` (uber-go/dig#273,
uber-go/dig#274) to Fx.

To add support, we merely need to bump the minimum version constraint to
a version of dig that has this feature.

Refs GO-374


codecov[bot]: # [Codecov](https://codecov.io/gh/uber-go/fx/pull/741?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=uber-go) Report
> Merging [#741](https://codecov.io/gh/uber-go/fx/pull/741?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=uber-go) (ecc3dd6) into [inout-alias](https://codecov.io/gh/uber-go/fx/commit/63eedd29a3f333bbbe5bb4010d66a1d59c429f22?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=uber-go) (63eedd2) will **not change** coverage.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/uber-go/fx/pull/741/graphs/tree.svg?width=650&height=150&src=pr&token=PYY9PSfaC6&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=uber-go)](https://codecov.io/gh/uber-go/fx/pull/741?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=uber-go)

```diff
@@             Coverage Diff              @@
##           inout-alias     #741   +/-   ##
============================================
  Coverage        97.66%   97.66%           
============================================
  Files               19       19           
  Lines              513      513           
============================================
  Hits               501      501           
  Misses              10       10           
  Partials             2        2           
```



------

[Continue to review full report at Codecov](https://codecov.io/gh/uber-go/fx/pull/741?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=uber-go).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=uber-go)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/uber-go/fx/pull/741?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=uber-go). Last update [63eedd2...ecc3dd6](https://codecov.io/gh/uber-go/fx/pull/741?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=uber-go). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=uber-go).


Patch:
diff --git a/go.mod b/go.mod
index af4c959..23415cd 100644
--- a/go.mod
+++ b/go.mod
@@ -4,7 +4,7 @@ go 1.13
 
 require (
 	github.com/stretchr/testify v1.4.0
-	go.uber.org/dig v1.10.0
+	go.uber.org/dig v1.11.0
 	go.uber.org/goleak v0.10.0
 	go.uber.org/multierr v1.5.0
 	go.uber.org/zap v1.16.0
diff --git a/go.sum b/go.sum
index caafc8c..b65bdb3 100644
--- a/go.sum
+++ b/go.sum
@@ -21,8 +21,8 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
 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.10.0 h1:yLmDDj9/zuDjv3gz8GQGviXMs9TfysIUMUilCpgzUJY=
-go.uber.org/dig v1.10.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw=
+go.uber.org/dig v1.11.0 h1:tGTnPJE8TIozwn2VdsAsK7yr7sxtI5IHFYHujIeLf1w=
+go.uber.org/dig v1.11.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw=
 go.uber.org/goleak v0.10.0 h1:G3eWbSNIskeRqtsN/1uI5B+eP73y3JUuBsv9AZjehb4=
 go.uber.org/goleak v0.10.0/go.mod h1:VCZuO8V8mFPlL0F5J5GK1rtHV3DrFcQ1R8ryq7FK0aI=
 go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
diff --git a/inout.go b/inout.go
index 5dd6a57..6b0155b 100644
--- a/inout.go
+++ b/inout.go
@@ -162,6 +162,29 @@ import "go.uber.org/dig"
 //
 // Note that values in a value group are unordered. Fx makes no guarantees
 // about the order in which these values will be produced.
+//
+// Unexported fields
+//
+// By default, a type that embeds fx.In may not have any unexported fields. The
+// following will return an error if used with Fx.
+//
+//   type Params struct {
+//     fx.In
+//
+//     Logger *zap.Logger
+//     mu     sync.Mutex
+//   }
+//
+// If you have need of unexported fields on such a type, you may opt-into
+// ignoring unexported fields by adding the ignore-unexported struct tag to the
+// fx.In. For example,
+//
+//   type Params struct {
+//     fx.In `ignore-unexported:"true"`
+//
+//     Logger *zap.Logger
+//     mu     sync.Mutex
+//   }
 type In = dig.In
 
 // Out is the inverse of In: it can be embedded in result structs to take


Test patch:
diff --git a/inout_test.go b/inout_test.go
index 864f91a..8ff0d80 100644
--- a/inout_test.go
+++ b/inout_test.go
@@ -129,3 +129,34 @@ func TestNamedTypes(t *testing.T) {
 	app.RequireStart().RequireStop()
 	assert.True(t, ran, "expected invoke to run")
 }
+
+func TestIgnoreUnexported(t *testing.T) {
+	type A struct{ ID int }
+	type B struct{ ID int }
+
+	type Params struct {
+		fx.In `ignore-unexported:"true"`
+
+		A A
+		b B // will be ignored
+	}
+
+	ran := false
+	run := func(in Params) {
+		defer func() { ran = true }()
+
+		assert.Equal(t, A{1}, in.A, "A must be set")
+
+		// We provide a B to the container, but because the "b" field
+		// is unexported, we don't expect it to be set.
+		assert.Equal(t, B{0}, in.b, "b must be unset")
+	}
+	defer func() {
+		assert.True(t, ran, "run was never called")
+	}()
+
+	fxtest.New(t,
+		fx.Supply(A{1}, B{2}),
+		fx.Invoke(run),
+	).RequireStart().RequireStop()
+}