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

Support slice and array of BindUnmarshaler #3988

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jmfederico
Copy link

@jmfederico jmfederico commented Jun 8, 2024

Allows form binding of []BindUnmarshaler and [x]BindUnmarshaler

Example usage:

package main

import (
	"strconv"

	"github.com/gin-gonic/gin"
)

type customUnmarshalParamHex int

func (f *customUnmarshalParamHex) UnmarshalParam(param string) error {
	v, err := strconv.ParseInt(param, 16, 64)
	if err != nil {
		return err
	}
	*f = customUnmarshalParamHex(v)
	return nil
}

func main() {
	route := gin.Default()
	var request struct {
		HexSlice []customUnmarshalParamHex `form:"[]hex_slice"`
	}
	route.GET("/test", func(ctx *gin.Context) {
		_ = ctx.BindQuery(&request)
		ctx.JSON(200, request.HexSlice)
	})
	route.Run(":8088")
}

Test with:

curl 'localhost:8088/test?[]hex_slice=f5&[]hex_slice=f6'

Result:

"[245,246]"

Allows form binding of []BindUnmarshaler and [x]BindUnmarshaler
Copy link

codecov bot commented Jun 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.06%. Comparing base (3dc1cd6) to head (de9a9eb).
Report is 65 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3988      +/-   ##
==========================================
- Coverage   99.21%   99.06%   -0.15%     
==========================================
  Files          42       44       +2     
  Lines        3182     2785     -397     
==========================================
- Hits         3157     2759     -398     
+ Misses         17       15       -2     
- Partials        8       11       +3     
Flag Coverage Δ
?
-tags "sonic avx" 99.05% <100.00%> (?)
-tags go_json 99.05% <100.00%> (?)
-tags nomsgpack 99.05% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 99.06% <100.00%> (-0.15%) ⬇️
go-1.22 99.06% <100.00%> (?)
macos-latest 99.06% <100.00%> (-0.15%) ⬇️
ubuntu-latest 99.06% <100.00%> (-0.15%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@appleboy
Copy link
Member

testing failed

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.

2 participants