Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

bruceNu1l
Copy link
Contributor

@bruceNu1l bruceNu1l commented May 16, 2024

Resolves #3970

package main

import (
	"fmt"
	"net/http"
	"strings"

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

type CustomPath []string

func (b *CustomPath) UnmarshalParam(param string) error {
	elems := strings.Split(param, "/")
	n := len(elems)
	if n < 2 {
		return fmt.Errorf("invalid path: %s", param)
	}

	*b = elems
	return nil
}

type PathRequest struct {
	Paths CustomPath `form:"path"`
}

func main() {
	g := gin.Default()
	g.GET("", func(c *gin.Context) {
		var request PathRequest
		if err := c.ShouldBind(&request); err != nil {
			c.String(http.StatusBadRequest, "request parse err: %v", err)
			return
		}

		c.String(200, "Hello %s", request.Paths)
	})
	g.Run(":9000")
}



Calling this server with a string parameters path will get the expected slices.

Example: 
$ curl 'http://127.0.0.1:9000?path=hello/world' 
CustomPath:  [hello world]

@bruceNu1l bruceNu1l marked this pull request as draft May 16, 2024 09:16
@bruceNu1l bruceNu1l closed this May 16, 2024
@bruceNu1l bruceNu1l reopened this May 16, 2024
@bruceNu1l
Copy link
Contributor Author

format the code for better review.

@bruceNu1l bruceNu1l marked this pull request as ready for review May 16, 2024 09:33
@codecov
Copy link

codecov bot commented May 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.05%. Comparing base (3dc1cd6) to head (5e4591f).
Report is 59 commits behind head on master.

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

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.

@RedCrazyGhost
Copy link
Contributor

please add test case

@bruceNu1l
Copy link
Contributor Author

please add test case

Test cases have been added, please review them again. Thank you.

@Apipa169
Copy link

Is it possible to add this for arrays as well? A use case for me would be converting a UUID to a binary uuid, which is a [16]byte

@bruceNu1l
Copy link
Contributor Author

Is it possible to add this for arrays as well? A use case for me would be converting a UUID to a binary uuid, which is a [16]byte

Yes. There will be some custom arrays for business purposes, such as Mongo's ObjectID, which is a [12]byte.

@appleboy appleboy added this to the v1.11 milestone May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can UnmarshalParam support custom slice of string

5 participants