Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(util/gconv): fix nested map conversion data loss in MapToMap#4612

Merged
hailaz merged 2 commits intogogf:masterfrom
lingcoder:hotfix/issue4542
Jan 15, 2026
Merged

fix(util/gconv): fix nested map conversion data loss in MapToMap#4612
hailaz merged 2 commits intogogf:masterfrom
lingcoder:hotfix/issue4542

Conversation

@lingcoder
Copy link
Contributor

Summary

  • Fix nested map conversion data loss when using gconv.Scan() or MapToMap()
  • When converting map[string]any to map[string]map[string]float64, the nested data was lost
  • Root cause: MapToMap incorrectly called Struct() for map value types
  • Solution: Separate reflect.Map handling from reflect.Struct, use recursive MapToMap() for nested maps

Test plan

  • Added test case reproducing original bug (nested map conversion)
  • Added test cases for deep nesting (3-5 levels)
  • Added test case for different key types
  • Added test case for empty nested map
  • Verified struct conversion still works (no regression)
  • Verified no infinite recursion with timeout tests
  • All gconv tests pass

Closes #4542

When converting nested maps like map[string]map[string]float64,
the data was lost because MapToMap incorrectly called Struct()
for map value types. This fix separates the handling of reflect.Map
from reflect.Struct, using recursive MapToMap calls for nested maps.

Closes gogf#4542
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a data loss bug in nested map conversions when using gconv.Scan() or MapToMap(). The issue occurred when converting map[string]any to map[string]map[string]float64, where nested map data was incorrectly handled by calling Struct() instead of recursively calling MapToMap().

Changes:

  • Separated reflect.Map handling from reflect.Struct in the type switch
  • Added recursive MapToMap() call for nested map types
  • Added comprehensive test coverage for nested maps, deep nesting, edge cases, and regression testing

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
util/gconv/internal/converter/converter_maptomap.go Split the combined case reflect.Map, reflect.Struct: into separate cases, with reflect.Map now recursively calling MapToMap()
util/gconv/gconv_z_unit_issue_test.go Added Test_Issue4542 with 7 test cases covering nested maps, deep nesting, different key types, empty maps, struct regression, and recursion safety

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@hailaz hailaz merged commit ce3599a into gogf:master Jan 15, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

util/gconv: gconv.Scan is not able to convert two maps with different types.

2 participants