Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export convertors #51

Closed
themihai opened this issue Oct 14, 2015 · 6 comments
Closed

Export convertors #51

themihai opened this issue Oct 14, 2015 · 6 comments
Labels

Comments

@themihai
Copy link

Is there any reason not to export convertors? I need to decode a payload which is composed either by structs or /and non-struct types (e.g. map[string]float64). Currently the decoder errors out if the destination is not a struct so I've forked the repo and just exported convertors.

@kisielk
Copy link
Contributor

kisielk commented Oct 14, 2015

It's an implementation detail, so best not to export it. Does RegisterConverter not work for your case?

@themihai
Copy link
Author

I don't think RegisterConverter helps. Decode errors out[0] if the value provided is not struct, isn't it? An alternative would be to provide a new function DecodeValue(valStr string, kind reflect.Kind)reflect.Value which decodes a single value.

[0] https://github.com/gorilla/schema/blob/master/decoder.go#L72

@kisielk
Copy link
Contributor

kisielk commented Oct 14, 2015

Hm maybe I'm not clear on what you are looking for. You want to decode the values in to a map directly, one which isn't a member of a struct? I'm not sure why you would even need the schema package for that.

@themihai
Copy link
Author

I need to unmarshal a map[string][]string into two variables . One is a struct and the other is of a type supported by schema (bool, int etc) but unknown at compile time. So I use schema.Decoder to unmarshal the map into the struct and then I need to parse the value of the 2nd variable(I know its key) thus the reason why I need the Convertors/parsing function.
Another option is to merge the unknown variable with the struct in a map using reflect (make a new map, copy all the struct fields as keys -> pointers) but Decoder doesn't support maps either(I fail to see the reason why).

    form := r.FormValue()
   var1 := schema.NewDecoder().Decode(val.Interface(), form)

   var2 := schema.Convertors[paramTyp.Elem().Kind()](form.Get(paramName))

@themihai
Copy link
Author

Shortly said schema package has an internal function(convertors) which marshals a string into various types/kinds and I need just that thus the proposal to export it.

@stale
Copy link

stale bot commented Dec 9, 2018

This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.

@stale stale bot added the stale label Dec 9, 2018
@stale stale bot closed this as completed Dec 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants