Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struct Tags need to be copied by ProviderComponentConstructor #28137

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/util/fxutil/provide_comp.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ func replaceStructEmbeds(typ, oldEmbed, newEmbed reflect.Type, assumeEmbed bool)
continue
}
if field.Type.Kind() == reflect.Struct && oldEmbed != nil && newEmbed != nil && hasEmbed {
field = reflect.StructField{Name: field.Name, Type: replaceStructEmbeds(field.Type, oldEmbed, newEmbed, false)}
field = reflect.StructField{Name: field.Name, Type: replaceStructEmbeds(field.Type, oldEmbed, newEmbed, false), Tag: field.Tag}
}
newFields = append(newFields, reflect.StructField{Name: field.Name, Type: field.Type})
newFields = append(newFields, reflect.StructField{Name: field.Name, Type: field.Type, Tag: field.Tag})
}

if hasEmbed && newEmbed != nil {
Expand Down
81 changes: 81 additions & 0 deletions pkg/util/fxutil/provide_comp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"encoding/json"
"fmt"
"reflect"
"sort"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -495,6 +497,45 @@ func TestFxShutdowner(t *testing.T) {
// see: https://github.com/uber-go/fx/blob/45af511c27eebb3b9e02abe4a35e1f978ad61bdc/app.go#L748
}

func TestFxValueGroups(t *testing.T) {
// Create two functions that provide value groups, and one that collects those value groups
// NOTE: These three functions are non-fx constructors, and all need to be upgraded with
// ProviderComponentConstructor in order for their value groups to be collected
v1Ctor := func() stringProvider {
return newStringProvider("abc")
}
v2Ctor := func() stringProvider {
return newStringProvider("def")
}
collectionCtor := func(deps depsCollection) collectionProvides {
texts := []string{}
for _, s := range deps.Strings {
texts = append(texts, s.String())
}
sort.Strings(texts)
return collectionProvides{
Result: collectionResult{
Texts: texts,
},
}
}

// An invocation that will concat all of the value group results
concatResult := ""
concatValueGroupResult := func(res collectionResult) {
concatResult = strings.Join(res.Texts, ",")
}

// Create the application, ensure that the value groups get properly concat'd
_ = fx.New(
fx.Invoke(concatValueGroupResult),
ProvideComponentConstructor(v1Ctor),
ProvideComponentConstructor(v2Ctor),
ProvideComponentConstructor(collectionCtor),
)
assert.Equal(t, concatResult, "abc,def")
}

func assertNoCtorError(t *testing.T, arg fx.Option) {
t.Helper()
app := fx.New(arg)
Expand Down Expand Up @@ -680,3 +721,43 @@ type fxAwareProvides struct {
fx.Out
B Banana
}

// Non-fx types, using value groups, that can be upgraded to fx-aware

type Stringer interface {
String() string
}

type stringProvider struct {
compdef.Out
Str Stringer `group:"test_value"`
}

type textStringer struct {
text string
}

func (v *textStringer) String() string {
return v.text
}

func newStringProvider(text string) stringProvider {
return stringProvider{
Str: &textStringer{
text: text,
},
}
}

type depsCollection struct {
compdef.In
Strings []Stringer `group:"test_value"`
}

type collectionProvides struct {
Result collectionResult
}

type collectionResult struct {
Texts []string
}
Loading