Skip to content

Commit 064a3a5

Browse files
committed
Add map decoration across modules test
This test verifies that map value group decoration chains properly across module boundaries, demonstrating the correct behavior for named value groups as implemented in uber-go/dig#381.
1 parent 7c4e9b8 commit 064a3a5

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

.claude/settings.local.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"WebFetch(domain:github.com)",
5+
"Bash(gh issue view:*)",
6+
"Bash(go test:*)",
7+
"Bash(go run:*)",
8+
"Bash(go mod why:*)",
9+
"Bash(go list:*)",
10+
"Read(//Users/qjeremy/go/pkg/mod/github.com/jquirke/[email protected]/**)",
11+
"Read(//Users/qjeremy/github/jquirke/**)",
12+
"Bash(git checkout:*)",
13+
"Bash(sed:*)",
14+
"Bash(go vet:*)"
15+
],
16+
"deny": [],
17+
"ask": []
18+
}
19+
}

decorate_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,4 +853,87 @@ func TestMapValueGroupsDecoration(t *testing.T) {
853853
}
854854
assert.ElementsMatch(t, []string{"alpha", "beta", "gamma"}, finalKeys)
855855
})
856+
857+
t.Run("map decoration across modules", func(t *testing.T) {
858+
t.Parallel()
859+
860+
type DecorationInput struct {
861+
fx.In
862+
Processors map[string]testProcessor `group:"processors"`
863+
}
864+
865+
type DecorationOutput struct {
866+
fx.Out
867+
Processors map[string]testProcessor `group:"processors"`
868+
}
869+
870+
var outerProcessors map[string]testProcessor
871+
var innerProcessors map[string]testProcessor
872+
873+
app := fxtest.New(t,
874+
fx.Provide(
875+
fx.Annotate(
876+
func() testProcessor { return &testBasicProcessor{name: "auth"} },
877+
fx.ResultTags(`name:"auth" group:"processors"`),
878+
),
879+
fx.Annotate(
880+
func() testProcessor { return &testBasicProcessor{name: "billing"} },
881+
fx.ResultTags(`name:"billing" group:"processors"`),
882+
),
883+
),
884+
fx.Decorate(func(input DecorationInput) DecorationOutput {
885+
enhanced := make(map[string]testProcessor)
886+
for name, processor := range input.Processors {
887+
enhanced[name] = &testEnhancedProcessor{
888+
wrapped: processor,
889+
prefix: "[OUTER]",
890+
}
891+
}
892+
return DecorationOutput{Processors: enhanced}
893+
}),
894+
fx.Invoke(fx.Annotate(
895+
func(processors map[string]testProcessor) {
896+
outerProcessors = processors
897+
},
898+
fx.ParamTags(`group:"processors"`),
899+
)),
900+
fx.Module("mymodule",
901+
fx.Decorate(func(input DecorationInput) DecorationOutput {
902+
enhanced := make(map[string]testProcessor)
903+
for name, processor := range input.Processors {
904+
enhanced[name] = &testEnhancedProcessor{
905+
wrapped: processor,
906+
prefix: "[INNER]",
907+
}
908+
}
909+
return DecorationOutput{Processors: enhanced}
910+
}),
911+
fx.Invoke(fx.Annotate(
912+
func(processors map[string]testProcessor) {
913+
innerProcessors = processors
914+
},
915+
fx.ParamTags(`group:"processors"`),
916+
)),
917+
),
918+
)
919+
defer app.RequireStart().RequireStop()
920+
921+
t.Logf("Outer processors:")
922+
for name, p := range outerProcessors {
923+
t.Logf(" %s: %s", name, p.Process("data"))
924+
}
925+
t.Logf("Inner processors:")
926+
for name, p := range innerProcessors {
927+
t.Logf(" %s: %s", name, p.Process("data"))
928+
}
929+
930+
// Test that map decoration chains across modules
931+
require.Len(t, outerProcessors, 2)
932+
assert.Equal(t, "[OUTER] auth: data", outerProcessors["auth"].Process("data"))
933+
assert.Equal(t, "[OUTER] billing: data", outerProcessors["billing"].Process("data"))
934+
935+
require.Len(t, innerProcessors, 2)
936+
assert.Equal(t, "[INNER] [OUTER] auth: data", innerProcessors["auth"].Process("data"))
937+
assert.Equal(t, "[INNER] [OUTER] billing: data", innerProcessors["billing"].Process("data"))
938+
})
856939
}

0 commit comments

Comments
 (0)