canonical / sqlair

Friendly type mapping for SQL databases
Apache License 2.0
17 stars 8 forks source link

bugfix: Fix duplicate type members in query catch #98

Closed Aflynn50 closed 7 months ago

Aflynn50 commented 11 months ago

Add a test for duplicate map keys and check for duplicates using equality of the string representation of the typeMember rather than equality of the pointer.

In #75 a bug was unwittingly introduced. mapKey and structField are both structs that implemented the typeMember interface. #75 change it so their a pointer to them implemented the interface so that the types could be passed around by reference rather than by value. The only problem is, that the map typeMemberPresent is indexed by typeMembers. This was therefore now indexed by pointers to structs rather than the structs themselves.

An error is thrown if two of the same typeMember appear in the outputs as this could create ambiguity. The test for this error still passed because in the case of structFields if two fields of the same struct are present then both will have the same pointer (as they are added from tagToField in structInfo which was a map[string]*structField. This is not the case for maps however.

Aflynn50 commented 8 months ago

This bugfix has been changed to use the approach detailed above. The map typeMemberPresent is now indexed by a string representation of the type member. This avoids the need to replace the mapKey methods with value receivers which would introduce a bad smell (even if there was a justification) into the code.