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

Allow bind with a map[string]string #2484

Merged
merged 4 commits into from Oct 30, 2020
Merged

Conversation

@ItalyPaleAle
Copy link
Contributor

@ItalyPaleAle ItalyPaleAle commented Aug 29, 2020

It's always been possible to bind using an object of type map[string]string. For example:

obj := make(map[string]string)
c.Bind(&obj)

If the request body was formatted as JSON (or YAML), this worked fine. You'd get a dictionary with key-values. For example, with input:

{
    "foo": "bar",
    "hello": "world"
}

You'd get in the object:

  • obj["foo"] = "bar"
  • obj["hello"] = "world"

However, this did not work if the request's body was form-encoded (application/x-www-form-urlencoded or multipart/form-data).

This PR adds support for passing a pointer to map[string]string to Bind that works with: Forms (POST Forms, Multipart forms), JSON, YAML. It also works with query strings. (However, it does NOT work with XML, because the XML parser doesn't allow that).

Why is this useful? Sometimes, there are situations in which I'm expecting a key-value dictionary, but I do not know what keys I'll receive.

This PR includes tests.

@codecov
Copy link

@codecov codecov bot commented Aug 29, 2020

Codecov Report

Merging #2484 into master will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2484      +/-   ##
==========================================
+ Coverage   98.46%   98.48%   +0.01%     
==========================================
  Files          41       41              
  Lines        1952     1974      +22     
==========================================
+ Hits         1922     1944      +22     
  Misses         17       17              
  Partials       13       13              
Impacted Files Coverage Δ
binding/form_mapping.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 16cd8cd...0a6aad1. Read the comment docs.

@ItalyPaleAle
Copy link
Contributor Author

@ItalyPaleAle ItalyPaleAle commented Sep 16, 2020

Hi is there any update on this? @thinkerou or someone else?

@ItalyPaleAle
Copy link
Contributor Author

@ItalyPaleAle ItalyPaleAle commented Oct 10, 2020

Ping on this 😄 @thinkerou

@thinkerou thinkerou requested review from appleboy and thinkerou Oct 11, 2020
@thinkerou thinkerou added this to the 1.7 milestone Oct 11, 2020
Copy link
Member

@thinkerou thinkerou left a comment

lgtm

@appleboy appleboy merged commit 65ed60e into gin-gonic:master Oct 30, 2020
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants