Skip to content

Allow non-string-keyed maps by converting the keys#331

Closed
birneee wants to merge 3 commits intotinylib:masterfrom
birneee:master
Closed

Allow non-string-keyed maps by converting the keys#331
birneee wants to merge 3 commits intotinylib:masterfrom
birneee:master

Conversation

@birneee
Copy link

@birneee birneee commented Aug 7, 2023

Non-string keys can be automatically converted to and from string by providing a NonStrMapKey interface implementation for that type.
Related to #232

klauspost added a commit that referenced this pull request Aug 22, 2025
* Allows for binary map keys
* Allows for non-string map keys to be shimmed as well as cast from string type aliases.
* Allows automatic conversion of some map key types to/from string.

Adds directive `maps`

* `//msgp:maps binkeys|shim|autoshim`

Not adding the directive will retain current behaviour where maps without string keys are ignored.

Adding `binkeys` will allow for binary marshaling of map keys. Types are inferred with standard rules. Binary encoding can be shimmed and can return any standard type. See `_generated/map_bin_key.go` for examples.

Adding `shim` will require you to add shims to custom map key types. Shims must be with string type. See `_generated/map_shim_key.go` for examples.

Adding `autoshim` will add automatic shimming to map keys that are `uint, uint8, uint16, uint32, uint64, int, int8, int16, int32, int64, bool, float32, float64, byte` - so these are written as a string. Standard `strconv.Parse/Format` functions are used. Aliased types are not supported, but `msgp:replace MyInt with:int` directive can be used and this will be picked up.

Adds `witherr:true|false` to `//msgp:shim` directives, so shim de-serializing can return errors.

Updates/Resolves: #331 #345 #305 #257 #232

Quirks:

* Sizing is not perfect and can sometimes be collapsed from a range to constant.
* These maps are not currently readable by "Interface" functions.
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.

1 participant