fix(gconv): route map-slice elements through MapToMap in Structs (#4787) - #4815
Open
waterWang wants to merge 1 commit into
Open
fix(gconv): route map-slice elements through MapToMap in Structs (#4787)#4815waterWang wants to merge 1 commit into
waterWang wants to merge 1 commit into
Conversation
…f#4787) When Result.Structs / Model.Scan is called with a *[]map[string]interface{} pointer, the destination element type is reflect.Map, but converter.Structs routes it through c.Struct which expects a struct type. This causes GetCachedStructInfo to return nil for non-struct types, silently producing empty maps. Fix: add a reflect.Map branch that calls c.MapToMap for each element instead of c.Struct. This matches the behavior of gconv.Scan which already handles map-slice conversion via MapToMaps.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix:
Result.Structs/Model.Scaninto*[]map[string]interface{}silently returns empty mapsDescription
When
db.Model().Scan(&rows)orResult.Structs(&rows)is called with*[]map[string]interface{}, it returns a slice of the correct length but every element is an empty map, with no error returned. Switching to a typed struct slice, or usingResult.List()/gconv.Scan(), works correctly.Root Cause
In
converter.Structs(converter_structs.go), the destination element type is checked:c.StructcallsGetCachedStructInfo()which returnsnilfor non-struct types (likereflect.Map), causing a silent no-op. The pre-allocated slice (correct length) is returned with each map left at its zero value.By contrast,
gconv.Scan→converter.Scan→doScanForComplicatedTypesdoes have the map-slice branch (sliceElemKind == reflect.Map→MapToMaps), which is whygconv.ScanandResult.List()work correctly.Fix
Added a
reflect.Mapbranch inconverter.Structsthat routes map-typed elements throughc.MapToMapinstead ofc.Struct:This matches the existing behavior of
gconv.Scan'sdoScanForComplicatedTypesmethod, which already handles map-slice conversion viaMapToMaps.Testing
The reproduction case from the issue now produces correct output:
Fixes #4787