Skip to content

LegacyStringToSliceHookFunc and related Issues #100

@KrzysztofKornalewski-Reply

Description

Lack of old StringToSliceHookFunc causes 2 unexpected behaviors.

  1. An error converting input "1,2,3" to []int{1, 2, 3}.
  2. An error occurred while converting the input "". I expected to get the result []int{}, but I got []int{0}.

The issue number 1 is directly related with changing reflect.Kind to reflect.Type and it was also mentioned in the following issue:
#69

The issue number 2 occurs because with the new approach, the data received from the StringToSliceHookFunc is not converted from the interface{}(string)"" to interface {}([]string)[], as it was in the legacy approach.

Is it an expected behavior for "" -> []int{0}?

This conversion takes place in the method:

func (d *Decoder) decode(name string, input interface{}, outVal reflect.Value) error {

and
#L523C1-L529C2

After this step, the data should have interface {}([]string)[] type to avoid the following “if” block in the method:

func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value) error {

And following line gives us a slice with len=1 and cap=1, which is incorrect because the default value of 0 is assigned to it later.

return d.decodeSlice(name, []interface{}{data}, val)

A possible solution is:
Store two versions of StringToSliceHookFunc in the mapstructure /decode_hooks.go
One is StringToSliceHookFunc, and the other is LegacyStringToSliceHookFunc. The legacy function is as follows:
https://github.com/mitchellh/mapstructure/blob/8508981c8b6c964e6986dd8aa85490e70ce3c2e2/decode_hooks.go#L104

Link to solution PR:
#99

This solution provides upstream compatibility for the dependent repository.
It's worth mentioning that the hook indirectly fixes this, but if the behavior is unexpected, it can be fixed in decodeSlice.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy