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

refactor(context): refactor Keys type to map[any]any #3963

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

flc1125
Copy link
Contributor

@flc1125 flc1125 commented May 10, 2024

  • Support for backward-compatible adjustments.
  • Enable Keys to support more types.
package main

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

func main() {
	var ctx *gin.Context
	type key struct{}
	
	
	ctx.Set(111, "111")
	ctx.Get(111)
	ctx.Get(key{})
	ctx.Get("111")
	// ……
}

The first step in solving #1123 (comment)

Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>
@flc1125 flc1125 changed the title refactor(context): refactor Keys to map[any]any refactor(context): refactor Keys type to map[any]any May 10, 2024
Copy link

codecov bot commented May 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.19%. Comparing base (3dc1cd6) to head (42a712b).
Report is 52 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3963      +/-   ##
==========================================
- Coverage   99.21%   99.19%   -0.03%     
==========================================
  Files          42       43       +1     
  Lines        3182     2722     -460     
==========================================
- Hits         3157     2700     -457     
+ Misses         17       12       -5     
- Partials        8       10       +2     
Flag Coverage Δ
?
-tags "sonic avx" 99.18% <100.00%> (?)
-tags go_json 99.18% <100.00%> (?)
-tags nomsgpack 99.17% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 99.19% <100.00%> (-0.03%) ⬇️
go-1.22 99.19% <100.00%> (?)
macos-latest 99.18% <100.00%> (-0.03%) ⬇️
ubuntu-latest 99.19% <100.00%> (-0.03%) ⬇️

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.

@jarrodhroberson
Copy link

Why not just make it comparable, or just any?

@flc1125
Copy link
Contributor Author

flc1125 commented May 14, 2024

Why not just make it comparable, or just any?

I think it's just like the key in context.WithValue(ctx, key, value).

It can be supported, so why not?

Moreover, when the key is set to struct{}, it saves more memory, ex:

type customKey struct{}

ctx.Set(customKey{}, "xxx")

package main

import (
	"fmt"
	"unsafe"
)

func main() {
	var a int
	var b string
	var c struct{}

	fmt.Println("a:", unsafe.Sizeof(a))
	fmt.Println("b:", unsafe.Sizeof(b))
	fmt.Println("c:", unsafe.Sizeof(c))
}

output:

a: 8
b: 16
c: 0

@jarrodhroberson
Copy link

jarrodhroberson commented May 20, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants