fx icon indicating copy to clipboard operation
fx copied to clipboard

Implement map value groups support in fx

Open jquirke opened this issue 7 months ago • 1 comments

Summary

Implements support for map value groups consumption via map[string]T for named value groups, enabling both individual named access and map-based access to the same providers.

This implements feature requests #1036 #998 by integrating the map value groups functionality from uber-go/dig#381 into fx with comprehensive test coverage.

Changes

  • map_groups_test.go: Comprehensive tests for map consumption, interfaces, pointer types, name+group combinations, edge cases including invalid key type validation, and mixed consumption patterns
  • module_test.go: Module scoping tests for map value groups across fx.Module boundaries
  • decorate_test.go: Enhanced with map decoration tests and validation of slice decorator restrictions for named value groups
  • go.mod: Updated to use dig fork with map value groups support
  • CHANGELOG.md: Updated to document the new functionality

Key Features

  • Consume value groups as map[string]T indexed by provider names
  • Combine name and group annotations for dual access patterns
  • Map-based decoration support for value groups
  • Proper validation that blocks slice decorators for named value groups
  • Module scoping behavior consistent with existing value groups
  • Mixed consumption patterns (same group as both map and slice)
  • Backward compatibility with existing slice-based consumption

Test Coverage

  • [x] Basic map consumption with interfaces and pointer types
  • [x] Name+Group combination scenarios
  • [x] Map decoration functionality
  • [x] Module scoping across fx.Module boundaries
  • [x] Mixed consumption patterns (map and slice of same group)
  • [x] Edge cases (empty groups, duplicate names, invalid key types, validation errors)
  • [x] Slice decorator blocking for named value groups
  • [x] All existing fx tests continue to pass

Dependencies

Important: This PR currently uses a forked dig dependency (github.com/jquirke/dig v0.0.0-20250929003136-0b0022552f09) that implements the map value groups functionality. This PR cannot be merged until:

  1. uber-go/dig#381 is reviewed, approved, and merged into the upstream dig repository
  2. A new dig release is published containing the map value groups functionality
  3. This PR is updated to use the official dig release instead of the fork

The fx implementation and tests are complete and ready, but the underlying dig dependency must be officially released first.

Implements #1036

jquirke avatar Sep 29 '25 01:09 jquirke

Documentation Update

Added comprehensive documentation and examples for map value groups:

New Documentation

  • docs/src/value-groups/maps/feed.md: How to feed values into map groups with name+group tags
  • docs/src/value-groups/maps/consume.md: Map vs slice consumption patterns and best practices

Demo Code

  • docs/ex/value-groups/maps/: Complete working examples with handlers, tests, and usage patterns
  • Updated mkdocs.yml navigation to include the new documentation

Key Documentation Topics

  • Basic map feeding and consumption syntax
  • Mixed consumption (map + slice from same group)
  • Decoration restrictions for map groups
  • Error conditions and validation requirements
  • Performance benefits and when to use maps vs slices

The documentation follows the existing fx docs pattern with code snippets and comprehensive examples. Ready for review alongside the implementation!

jquirke avatar Sep 29 '25 15:09 jquirke