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

binding: support map for map form #1387

Closed
wants to merge 10 commits into from
Closed

binding: support map for map form #1387

wants to merge 10 commits into from

Conversation

thinkerou
Copy link
Member

@thinkerou thinkerou commented Jun 10, 2018

Please @javierprovecho @appleboy review the pull request, thanks a lot!

ref: #1362 #398

Idea: declare one in map[string]interface{} (MUST), and user inputs -F 'in={"i":1}'.

Support map for map form, can use the curl command:

curl -v http://localhost:8080/loginForm -F 'user={"a":"hello","b":2, "c": 3.14}' -F 'password=123'

and the following golang code:

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

// Binding from JSON
type Login struct {
	User     map[string]interface{} `form:"user"`
	Password string                 `form:"password"`
}

func main() {
	router := gin.Default()

	router.POST("/loginForm", func(c *gin.Context) {
		var form Login
		// This will infer what binder to use depending on the content-type header.
		if err := c.ShouldBind(&form); err == nil {
			if form.User["a"].(string) == "hello" && form.User["b"].(float64) == 2 && form.User["c"].(float64) == 3.14 && form.Password == "123" {
				c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
			} else {
				c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			}
		}
	})

	// Listen and serve on 0.0.0.0:8080
	router.Run(":8080")
}

it will output the result:

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> POST /loginForm HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Length: 269
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------02819574d8de733f
>
< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Date: Sun, 10 Jun 2018 05:05:13 GMT
< Content-Length: 30
<
* Connection #0 to host localhost left intact
{"status":"you are logged in"}

@codecov
Copy link

codecov bot commented Jun 10, 2018

Codecov Report

Merging #1387 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1387      +/-   ##
==========================================
+ Coverage   98.48%   98.49%   +<.01%     
==========================================
  Files          41       41              
  Lines        2046     2057      +11     
==========================================
+ Hits         2015     2026      +11     
  Misses         19       19              
  Partials       12       12
Impacted Files Coverage Δ
binding/form_mapping.go 100% <100%> (ø) ⬆️

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 a58a2f9...ad17949. Read the comment docs.

@thinkerou thinkerou changed the title WIP: Support map for map form binding: support map for map form Jun 10, 2018
if err := setTimeField(inputValue[0], typeField, structField); err != nil {
} else if structFieldKind == reflect.Map {
m := make(map[string]interface{})
err := json.Unmarshal([]byte(inputValue[0]), &m)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think better to use this ptr to real map. It's give you the opportunity to unmarshal from any types of map.

Suggested change
err := json.Unmarshal([]byte(inputValue[0]), &m)
err := json.Unmarshal([]byte(inputValue[0]), val.Field(i).Addr().Interface())

binding/binding_test.go Outdated Show resolved Hide resolved
@thinkerou thinkerou added this to the 1.4 milestone Feb 21, 2019
@thinkerou thinkerou self-assigned this Feb 21, 2019
@thinkerou
Copy link
Member Author

@appleboy please help me review the pr, thanks!

@vkd
Copy link
Contributor

vkd commented Feb 21, 2019

I have already added support of map in binding and these tests in my PR #1749

@thinkerou thinkerou modified the milestones: 1.4, 1.5 Mar 1, 2019
@thinkerou thinkerou closed this Mar 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants